2

Having spent some time on resolving this, I've run out of options to fix this error. I've reached Ch 8 (Sign In/ Out) from M Hartl's RoR tutorial. There are similar questions to this (Undefined method `remember_token=' 'find_by_remember_token' - Chapter 8 Ruby on Rails Tutorial), and I've tried all the answers given there, to no avail.

The method remember_token is defined in my user.rb file:

class User < ActiveRecord::Base

before_save { self.email = email.downcase }
before_create :create_remember_token
has_secure_password
validates :name, presence: true, length: { maximum: 50 }
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: 4 }
validates :password_confirmation, presence: true

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

My users Controller:

  class UsersController < ApplicationController

  def show
    @user = User.find(params[:id])
  end

  def new
    @user = User.new
  end

  def create
    @user = User.new(user_params)  # not the final implementation
    if @user.save
        # Handle a successful save.
      sign_in @user
      flash[:success] = "Welcome to Story Escape!"
      redirect_to @user
    else
        render 'new'
    end
  end

  private
    def user_params
    params.require(:user).permit(:name, :email, :password,
        :password_confirmation)
    end 
  end

My sessions controller:

  class SessionsController < ApplicationController

  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

  def destroy
  end
    end
    end

And my sessions_helper.rb:

module SessionsHelper

    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
        @current_user ||= User.find_by(remember_token: remember_token)
    end

end

I am new to Ruby and Rails (and coding in general), so any clues would be a great help.

4

2 回答 2

3

In my case, the bug was a silly mistake of not adding:

remember_token = User.encrypt(cookies[:remember_token])

where I defined current_user in my sessions_helper.rb file.

于 2013-10-02T04:47:41.453 回答
1

It sounds like you forgot to generate (or apply) the migration that adds the remember_token attribute to your User model.

rails generate migration add_remember_token_to_users remember_token:string
rake db:migrate
rake db:test:prepare
于 2013-09-28T22:28:36.397 回答