我试图完成对迈克尔·哈特尔Rails的教程Ruby和我被困在第八章我收到相同的两个错误,当我跑我的测试:
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`