Undefined method `remember_token=' 'find_by_remember_token' - Chapter 8 Ruby on Rails Tutorial

冷暖自知 提交于 2020-01-22 16:22:05

问题


I am trying to complete the Ruby on Rails Tutorial by Michael Hartl and I am stuck on Chapter 8. I am getting the same two errors when I run my tests:

NoMethodError: undefined method remember_token=' ActionView::Template::Error: undefined methodfind_by_remember_token'

I am very new to coding and programming so I'm not sure exactly what to post so people can help with my question. I feel like I've defined both :remember_token and find_by_remember_token. I've included each of the instances where :remember_token appears in my code below:

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`

回答1:


I was having this problem as well. After some googling I found that running these terminal commands...

rake db:drop
rake db:create
rake db:migrate

...got me up and running again.

Hoping it works for you, too. I know how frustrating it can be.




回答2:


Yes, this error is because you don't have the attribute remember_token in User Model.

Check that you have had right database migration files and than do:

rake db:drop
rake db:create
rake db:migrate

or for short:

rake db:reset



回答3:


I had forgotten to include the attr_accessor :remember_token in the User model app/model/user.rb

so the test was giving me the error:

Undefined method `remember_token='



来源:https://stackoverflow.com/questions/15302264/undefined-method-remember-token-find-by-remember-token-chapter-8-ruby-on

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!