我遵循了修改后的 railcast,然后决定使用记忆功能升级我的代码。几乎可以工作,但是进入登录页面时出现以下错误
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
路由.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
感谢大家