我正在尝试让用户使用创建的登录表单登录,并且我已按照 Tut 中的步骤获取会话 cookie 等(请参见下面的代码),当我提交登录表单时出现此错误: (加号是将页面重定向到--> :3000/sessions)
会话控制器中的名称错误#create
# 未定义的局部变量或方法“用户”
Rails.root: C:/rails_projects/sample_app
app/helpers/sessions_helper.rb:13:in `current_user='
app/helpers/sessions_helper.rb:5:in `sign_in'
app/controllers/sessions_controller.rb:10:in `create'
要求
参数:
{"utf8"=>"✓",
"authenticity_token"=>"9q8ye89fq0394u790qjfq0ngq4t=",
"会话"=>{"电子邮件"=>"example@example.com",
"密码"=>"[过滤]"},
“提交”=>“登录”}
/sessions_helper.rb:
module SessionsHelper
def sign_in(user)
cookies.permanent[:remember_token] = user.remember_token
self.current_user = user
end
def signed_in?
!current_user.nil?
end
def current_user=(uaser)
@current_user = user
end
def current_user
@current_user ||= User.find_by_remember_token(cookies[:remember_token])
end
end
/sessions_controller.rb
class SessionsController < ApplicationController
def new
end
def create
user = User.find_by_email(params[:session][:email])
if user && user.authenticate(params[:session][:password])
sign_in user
redirect_to user
else
flash.now[:error] = 'Invalid email/password combination'
render 'new'
end
end
def destroy
end
end
/routes.rb 文件:
SampleApp::Application.routes.draw do
resources :users
resources :sessions, only: [:new, :create, :destroy]
root :to => 'static_pages#home'
match '/help', to: "static_pages#help"
match '/about', to: "static_pages#about"
match '/contact', to: "static_pages#contact"
match '/signup', to: 'users#new'
match '/signin', to: 'sessions#new'
match '/signout', to: 'sessions#destroy', method: "delete"