我尝试创建会话,如示例'railstutorial'一书。但是我做错了,找不到错误。 这是我的session_helper
def sign_in(user)
remember_token = User.new_remember_token
cookies.permanent[:remember_token] = remember_token
user.update_attribute(:remember_token, User.encrypt(remember_token))
self.current_user = user
end
def signed_in?
!current_user.nil?
end
def current_user=(user)
@current_user = user
end
def current_user
remember_token = User.encrypt(cookies[:remember_token])
@current_user ||= User.find_by(remember_token: remember_token)
end
end
这是我的Controller sessions_controller
def new
end
def create
user = User.find_by(email: params[:session][:email].downcase)
if user && user.authenticate(params[:session][:password])
sign_in user
redirect_to user
else
flash.now[:error] = 'Invalid email/password combination'
render 'new'
end
end
def destroy
end
end
这是我的Model users.rb
before_save { self.email = email.downcase }
before_create :create_remember_token
validates :name, presence: true, length: { maximum: 50, minimum: 2 }, uniqueness: true
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
validates :email, presence: true,
format: { with: VALID_EMAIL_REGEX },
uniqueness: { case_sensitive: false }
validates :password, length: { minimum: 6 }
has_secure_password
def User.new_remember_token
SecureRandom.urlsafe_base64
end
def User.encrypt(token)
Digest::SHA1.hexdigest(token.to_s)
end
private
def create_remember_token
self.remember_token = User.encrypt(User.new_remember_token)
end
end
来自控制台的完整错误消息
2.1.2 :004 > user.save
(0.2ms) begin transaction
User Exists (0.3ms) SELECT 1 AS one FROM "users" WHERE "users"."name" = 'anna' LIMIT 1
User Exists (0.3ms) SELECT 1 AS one FROM "users" WHERE LOWER("users"."email") = LOWER('qwerty@gmail.com') LIMIT 1
(0.2ms) rollback transaction
NoMethodError: undefined method `remember_token=' for #<User:0x000000026f58b8>
from /home/weare138/.rvm/gems/ruby-2.1.2/gems/activemodel-4.1.6/lib/active_model/attribute_methods.rb:435:in `method_missing'
from /home/weare138/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.6/lib/active_record/attribute_methods.rb:211:in `method_missing'
from /home/weare138/app/myapp/app/models/user.rb:24:in `create_remember_token'
from /home/weare138/.rvm/gems/ruby-2.1.2/gems/activesupport-4.1.6/lib/active_support/callbacks.rb:424:in `block in make_lambda'
from /home/weare138/.rvm/gems/ruby-2.1.2/gems/activesupport-4.1.6/lib/active_support/callbacks.rb:160:in `call'
from /home/weare138/.rvm/gems/ruby-2.1.2/gems/activesupport-4.1.6/lib/active_support/callbacks.rb:160:in `block in halting'
from /home/weare138/.rvm/gems/ruby-2.1.2/gems/activesupport-4.1.6/lib/active_support/callbacks.rb:86:in `call'
from /home/weare138/.rvm/gems/ruby-2.1.2/gems/activesupport-4.1.6/lib/active_support/callbacks.rb:86:in `run_callbacks'
from /home/weare138/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.6/lib/active_record/callbacks.rb:306:in `_create_record'
from /home/weare138/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.6/lib/active_record/timestamp.rb:57:in `_create_record'
from /home/weare138/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.6/lib/active_record/persistence.rb:483:in `create_or_update'
from /home/weare138/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.6/lib/active_record/callbacks.rb:302:in `block in create_or_update'
from /home/weare138/.rvm/gems/ruby-2.1.2/gems/activesupport-4.1.6/lib/active_support/callbacks.rb:113:in `call'
from /home/weare138/.rvm/gems/ruby-2.1.2/gems/activesupport-4.1.6/lib/active_support/callbacks.rb:113:in `call'
from /home/weare138/.rvm/gems/ruby-2.1.2/gems/activesupport-4.1.6/lib/active_support/callbacks.rb:166:in `block in halting'
from /home/weare138/.rvm/gems/ruby-2.1.2/gems/activesupport-4.1.6/lib/active_support/callbacks.rb:86:in `call'
... 17 levels...
from /home/weare138/.rvm/gems/ruby-2.1.2/gems/railties-4.1.6/lib/rails/commands/console.rb:9:in `start'
from /home/weare138/.rvm/gems/ruby-2.1.2/gems/railties-4.1.6/lib/rails/commands/commands_tasks.rb:69:in `console'
from /home/weare138/.rvm/gems/ruby-2.1.2/gems/railties-4.1.6/lib/rails/commands/commands_tasks.rb:40:in `run_command!'
from /home/weare138/.rvm/gems/ruby-2.1.2/gems/railties-4.1.6/lib/rails/commands.rb:17:in `<top (required)>'
from /home/weare138/.rvm/gems/ruby-2.1.2/gems/activesupport-4.1.6/lib/active_support/dependencies.rb:247:in `require'
from /home/weare138/.rvm/gems/ruby-2.1.2/gems/activesupport-4.1.6/lib/active_support/dependencies.rb:247:in `block in require'
from /home/weare138/.rvm/gems/ruby-2.1.2/gems/activesupport-4.1.6/lib/active_support/dependencies.rb:232:in `load_dependency'
from /home/weare138/.rvm/gems/ruby-2.1.2/gems/activesupport-4.1.6/lib/active_support/dependencies.rb:247:in `require'
from /home/weare138/app/myapp/bin/rails:8:in `<top (required)>'
from /home/weare138/.rvm/gems/ruby-2.1.2/gems/activesupport-4.1.6/lib/active_support/dependencies.rb:241:in `load'
from /home/weare138/.rvm/gems/ruby-2.1.2/gems/activesupport-4.1.6/lib/active_support/dependencies.rb:241:in `block in load'
from /home/weare138/.rvm/gems/ruby-2.1.2/gems/activesupport-4.1.6/lib/active_support/dependencies.rb:232:in `load_dependency'
from /home/weare138/.rvm/gems/ruby-2.1.2/gems/activesupport-4.1.6/lib/active_support/dependencies.rb:241:in `load'
from /home/weare138/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from /home/weare138/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from -e:1:in `<main>'2.1.2 :005 >
如何解决? 抱歉我的英文不好
答案 0 :(得分:0)
您需要在用户模型中添加以下内容:
attr_accessor :remember_token
.
.
.
def remember
self.remember_token = ...
update_attribute(:remember_digest, ...)
end
答案 1 :(得分:0)
似乎ActiveRecord::Base
在用户表中找不到列remember_token
,这可能是由于架构未与DB正确对齐。
尝试回滚并重新迁移数据库,看看是否能解决错误。