我正在尝试完成Michael Hartl的Ruby on Rails教程,我被困在第8章。我在运行测试时得到了同样的两个错误:
NoMethodError:未定义的方法remember_token='
ActionView::Template::Error: undefined method
find_by_remember_token'
我对编码和编程很陌生,所以我不确定要发布什么,以便人们可以帮助解决我的问题。我觉得我已经定义了:remember_token和find_by_remember_token。我已经包含了以下每个实例:remember_token出现在我的代码中:
sample_app/app/helpers/sessions_helper.rb:
2
3 def sign_in(user)
4: cookies.permanent[:remember_token] = user.remember_token
5 self.current_user = user
6 end
.
15
16 def current_user
17: @current_user ||= User.find_by_remember_token(cookies[:remember_token])
18 end
19
20 def sign_out
21 self.current_user = nil
22: cookies.delete(:remember_token)
23 end
24 end
sample_app/app/models/user.rb:
16
17 before_save { |user| user.email = email.downcase }
18: before_save :create_remember_token
19
20 validates :name, presence: true, length: { maximum: 50 }
..
27 private
28
29: def create_remember_token
30: self.remember_token = SecureRandom.urlsafe_base64
31 end
32 end
sample_app/db/migrate/[timestamp]_add_remember_token_to_users.rb:
1 class AddRememberTokenToUsers < ActiveRecord::Migration
2 def change
3: add_column :users, :remember_token, :string
4: add_index :users, :remember_token
5 end
6 end
sample_app/spec/models/user_spec.rb:
27 it { should respond_to(:password) }
28 it { should respond_to(:password_confirmation) }
29: it { should respond_to(:remember_token) }
30 it { should respond_to(:authenticate) }
31
..
125 describe "remember token" do
126 before { @user.save }
127: its(:remember_token) { should_not be_blank }
128 end
129 end
sample_app/spec/support/utilities.rb:
7 click_button "Sign in"
8 # Sign in when not using Capybara as well.
9: cookies[:remember_token] = user.remember_token
10 end`
答案 0 :(得分:7)
我也有这个问题。经过一些谷歌搜索后,我发现运行这些终端命令...
rake db:drop
rake db:create
rake db:migrate
......让我重新振作起来。
希望它也适合你。我知道它是多么令人沮丧。
答案 1 :(得分:1)
是的,此错误是因为您在用户模型中没有remember_token属性。
检查您是否拥有正确的数据库迁移文件,而不是:
rake db:drop
rake db:create
rake db:migrate
或简称:
rake db:reset
答案 2 :(得分:1)
我忘了在用户模型app / model / user.rb中包含attr_accessor:remember_token
所以测试给了我错误:
未定义的方法`remember_token ='