Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 在您调用它的地方,我谈论的是rails控制台,如果您在用户类中运行它,只需直接调用save,尽管我不建议使用绕过模型内部验证的save。感谢您对代码进行的少量编辑。应该是self而不是user self.changes.include?:电子邮件。我计划最_Ruby On Rails_Rails Console - Fatal编程技术网

Ruby on rails 在您调用它的地方,我谈论的是rails控制台,如果您在用户类中运行它,只需直接调用save,尽管我不建议使用绕过模型内部验证的save。感谢您对代码进行的少量编辑。应该是self而不是user self.changes.include?:电子邮件。我计划最

Ruby on rails 在您调用它的地方,我谈论的是rails控制台,如果您在用户类中运行它,只需直接调用save,尽管我不建议使用绕过模型内部验证的save。感谢您对代码进行的少量编辑。应该是self而不是user self.changes.include?:电子邮件。我计划最,ruby-on-rails,rails-console,Ruby On Rails,Rails Console,在您调用它的地方,我谈论的是rails控制台,如果您在用户类中运行它,只需直接调用save,尽管我不建议使用绕过模型内部验证的save。感谢您对代码进行的少量编辑。应该是self而不是user self.changes.include?:电子邮件。我计划最终使用Desive,但他们自己的文档建议首先使用Desive。这取决于您调用它的位置,我指的是rails控制台,如果您在用户类中运行它,只需直接调用save,尽管我不建议使用绕过模型内验证的save。 create_table "users",


在您调用它的地方,我谈论的是rails控制台,如果您在用户类中运行它,只需直接调用save,尽管我不建议使用绕过模型内部验证的save。感谢您对代码进行的少量编辑。应该是self而不是user self.changes.include?:电子邮件。我计划最终使用Desive,但他们自己的文档建议首先使用Desive。这取决于您调用它的位置,我指的是rails控制台,如果您在用户类中运行它,只需直接调用save,尽管我不建议使用绕过模型内验证的save。
create_table "users", force: :cascade do |t|
  t.string   "name"
  t.string   "email"
  t.datetime "created_at",                          null: false
  t.datetime "updated_at",                          null: false
  t.string   "avatar_file_name"
  t.string   "avatar_content_type"
  t.integer  "avatar_file_size"
  t.datetime "avatar_updated_at"
  t.string   "password_hash"
  t.string   "password_salt"
  t.boolean  "admin",               default: false
end
class User < ActiveRecord::Base
    has_many :recipes
    has_many :comments

    #attr_accessible :email, :password, :password_confirmation
    attr_accessor :password

    before_save :encrypt_password

    validates_confirmation_of :password, :on => :create
    #validates :name, presence: true, uniqueness: true
    #validates :email, presence: true, uniqueness: true
    validates :password, presence: true, length: { in: 6..20 }


      has_attached_file :avatar, :styles => {
        :medium => "300x300>",
        :thumb => "100x100>"
      },
      :default_url => "/images/:style/missing.png",
      :bucket =>'davisrecipebook',
      :storage => :s3,
      :s3_credentials => "#{Rails.root}/config/s3.yml"
      #:s3_credentials => Proc.new{|a| a.instance.s3_credentials }
      #:storage => :dropbox,
      #:dropbox_credentials => Rails.root.join("config/dropbox.yml")

    #def s3_credentials
    #  {:bucket =>'davisrecipebook', :access_key_id => '', :secret_access_key => ''}
    #end


    validates_attachment_content_type :avatar, :content_type => /\Aimage\/.*\Z/
    validates_attachment_size :avatar, :less_than => 10.megabytes

    def self.authenticate(email, password)
        user = find_by_email(email)
        if user && user.password_hash == BCrypt::Engine.hash_secret(password, user.password_salt)
            user
        else
            nil
        end
    end

    def encrypt_password
        if password.present?
            self.password_salt = BCrypt::Engine.generate_salt
            self.password_hash = BCrypt::Engine.hash_secret(password, password_salt)
        end
    end
end
2.1.5 :073 > User.last
  User Load (0.5ms)  SELECT  "users".* FROM "users"  ORDER BY "users"."id" DESC LIMIT 1
 => #<User id: 7, name: "hate", email: "death", created_at: "2014-12-28 07:32:22", updated_at: "2014-12-28 08:24:01", avatar_file_name: nil, avatar_content_type: nil, avatar_file_size: nil, avatar_updated_at: nil, password_hash: "$2a$10$9rEtvSoO0DOzm5u/Vop0O.5matAM8gTI0t6QZ470UyF...", password_salt: "$2a$10$9rEtvSoO0DOzm5u/Vop0O.", admin: false> 
2.1.5 :074 > s = _
 => #<User id: 7, name: "hate", email: "death", created_at: "2014-12-28 07:32:22", updated_at: "2014-12-28 08:24:01", avatar_file_name: nil, avatar_content_type: nil, avatar_file_size: nil, avatar_updated_at: nil, password_hash: "$2a$10$9rEtvSoO0DOzm5u/Vop0O.5matAM8gTI0t6QZ470UyF...", password_salt: "$2a$10$9rEtvSoO0DOzm5u/Vop0O.", admin: false> 
2.1.5 :075 > s.admin = true
 => true 
2.1.5 :076 > s.save
   (0.2ms)  BEGIN
  User Exists (0.6ms)  SELECT  1 AS one FROM "users" WHERE ("users"."name" = 'hate' AND "users"."id" != 7) LIMIT 1
  User Exists (0.3ms)  SELECT  1 AS one FROM "users" WHERE ("users"."email" = 'death' AND "users"."id" != 7) LIMIT 1
   (0.2ms)  ROLLBACK
 => false 
2.1.5 :077 > s.errors

@messages={:password=>["can't be blank", "can't be blank", "is too short (minimum is 6 characters)"]}>
validates :password, on: :create, ...
validates :password, if: :password_required?
def password_required?
  user.changes.include? :email # just an example
end
user.save(validate: false)