1

改訂されたレールキャストに従い、記憶機能を使用してコードをアップグレードすることにしました。ほとんど動作しますが、ログインページに移動すると、次のエラーが発生します

Couldn't find Customer with auth_token = 
Note I change the table from User to Customer

ここに私のコード、多分私は少し間違えました。データベースをリセットしました

カスタマーコントローラー

class CustomersController < ApplicationController
  def new
    @customer = Customer.new
  end

  def create
    @customer = Customer.new(params[:customer])
    if @customer.save
    session[:customer_id] = @customer.id
        redirect_to root_url, notice: "Thank you for signing up!"
    else
      render "new"
    end
  end

end

セッションコントローラー

class SessionsController < ApplicationController
  def new
  end
  def create
    customer = Customer.find_by_email(params[:email])
      if customer && customer.authenticate(params[:password])
      if params[:remember_me]
         cookies.permanent[:auth_token] = customer.auth_token
      else
        cookies[:auth_token] = customer.auth_token
      end
        redirect_to root_url, :notice => "Logged in!"
    else
      flash.now.alert = "Invalid email or password"
      render "new"
    end
  end

  def destroy
    cookies.delete(:auth_token)
    redirect_to root_url, :notice => "Logged out!"
  end
end

Route.rb

  get 'signup', to: 'customers#new', as: 'signup'
  get 'login', to: 'sessions#new', as: 'login'
  get 'logout', to: 'sessions#destroy', as: 'logout'
  resources :sessions
  resources :customers    
  root :to => 'sessions#new'

アプリケーションコントローラー

class ApplicationController < ActionController::Base
  protect_from_forgery
  private

    def authorize
        redirect_to login_url, alert: "Not authorized" if current_customer.nil?
    end

    def current_customer
      @current_customer ||= Customer.find_by_auth_token!(cookies[:auth_token]) if cookies[:auth_token]
    end
    helper_method :current_customer
end

みんな、ありがとう

4

1 に答える 1

2

私の間違いは次のコードにありました

def current_customer
      @current_customer ||= Customer.find_by_auth_token!(cookies[:auth_token]) if cookies[:auth_token]
    end

そしてそれを次のように修正します

def current_customer
      @current_customer ||= Customer.find_by_auth_token(cookies[:auth_token]) if cookies[:auth_token]
    end
于 2012-07-25T01:45:51.900 に答える