Chapter 8 Rails Tutorial Remember Token Errors

This chapter describes how to add storage tokens to ensure that the user's login status is saved and that the session is cleared only if the user explicitly displays a message. I understand the importance of having this feature in my application, so I want to make sure that it works correctly. I get a bunch of errors though when I run

$ bundle exec rspec spec/ 

and I suspect they are related to my user model, as they all contain:

 NoMethodError: undefined method `remember_token=' for #<User:...> 

and the last one contains

 Failure/Error: it { should respond_to(:remember_token) } 

and then specify my files user_spec.rb, user.rb and authentication_pages_spec.rb, which I have included most of (the relevant parts) here.

user.rb:

 # == Schema Information # # Table name: users # # id :integer not null, primary key # name :string(255) # email :string(255) # created_at :datetime not null # updated_at :datetime not null # class User < ActiveRecord::Base attr_accessible :name, :email, :password, :password_confirmation has_secure_password before_save { |user| user.email = email.downcase } before_save :create_remember_token validates :name, presence: true, length: { maximum: 50 } VALID_EMAIL_REGEX = /\A[\w+\-.] +@ [az\d\-.]+\.[az]+\z/i validates :email, presence: true, format: { with: VALID_EMAIL_REGEX }, uniqueness: { case_sensitive: false } validates :password, length: { minimum: 6 } validates :password_confirmation, presence: true private def create_remember_token self.remember_token = SecureRandom.urlsafe_base64 end end 

authentication_pages_spec.rb:

 require 'spec_helper' describe "Authentication" do subject {page} describe "signin page" do before { visit signin_path } it {should have_selector('h1', text: 'Sign in')} it {should have_selector('title', text: 'Sign in')} end describe "signin" do before {visit signin_path} describe "with invalid information" do before {click_button "Sign in"} it {should have_selector('title', text: 'Sign in')} it {should have_selector('div.alert.alert-error', text: 'Invalid')} describe "after visiting another page" do before { click_link "Home" } it { should_not have_selector('div.alert.alert-error') } end end describe "with valid information" do let(:user) { FactoryGirl.create(:user) } before do fill_in "Email", with: user.email fill_in "Password", with: user.password click_button "Sign in" end it { should have_selector('title', text: user.name) } it { should have_link('Profile', href: user_path(user)) } it { should have_link('Sign out', href: signout_path) } it { should_not have_link('Sign in', href: signin_path) } end end end 

and start user_spec.rb:

 # == Schema Information # # Table name: users # # id :integer not null, primary key # name :string(255) # email :string(255) # created_at :datetime not null # updated_at :datetime not null # require 'spec_helper' describe User do before do @user = User.new(name: "Example User", email: " user@example.com ", password: "foobar", password_confirmation: "foobar") end subject { @user } it { should respond_to(:name) } it { should respond_to(:email) } it { should be_valid } it { should respond_to(:password_digest) } it { should respond_to(:password) } it { should respond_to(:password_confirmation) } it { should respond_to(:authenticate) } it { should respond_to(:remember_token) } describe "remember token" do before { @user.save } its(:remember_token) { should_not be_blank } end . . . 

Any help would be greatly appreciated!

+6
source share
5 answers

If you get this production error on Heroku, after launch:

 heroku run rake db:migrate 

you need to restart the application:

 heroku restart 
+4
source

Have you created a migration to add a column to the user model?

 $ rails generate migration add_remember_token_to_users 

After that, edit the migration file to add a new remember_token field

and

After that you need to do

 $ bundle exec rake db:migrate $ bundle exec rake db:test:prepare 

Your model annotations do not show a column. Make sure you run the above commands.

+2
source

I got a similar error with a test error. I did to generate a remember_token column with a string type.

 rails generate migration add_remember_token_to_users remember_token:string --force rake db:migrate RAILS_ENV=test 

after that, the test passes.

+2
source

I met the same problem while studying the Mhartl tutorial. I solved it. this problem manifests itself when some user data is already in the database and then the remember_token migrate will not work. you should clear the firt date and then db: migrate.like:

rake db: drop db: create rake db: migrate (be careful: this will erase all your data)

help hope

0
source

I had the same problem getting the undefined find_by_remember_token method.

Here is what I did to fix this:

 heroku run rake db:migrate 

Then click on the hero again.

 git push heroku 
-1
source

Source: https://habr.com/ru/post/914468/


All Articles