我正在使用rails创建我的第一个单独项目,用户似乎无法登录。我在创建用户后立即调用我的登录方法,但如果他们退出应用程序将不会让他们重新登录。这里是提交登录表单时我得到了什么
这是错误消息
nil:NilClass 的未定义方法“[]”
它说它在会话控制器的第 6 行,但我认为还有其他问题
会话控制器
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
end
def destroy
sign_out
redirect_to root_path
end
end
登录表单
<h1>Sign in</h1>
<br />
<%= form_tag sessions_path do %>
<%= label_tag :email %><br />
<%= text_field_tag :email %><br />
<%= label_tag :password %><br />
<%= text_field_tag :password %><br />
<%= submit_tag "Sign in", class: "button" %>
<% end %>
<p>New user? <%= link_to "Sign up now!", signup_path %></p>
路线
Blog::Application.routes.draw do
get "sessions/new"
get "users/new"
resources :posts
resources :static_pages
resources :users
resources :sessions
root to: 'posts#index'
match '/signin', to: 'sessions#new'
match '/signup', to: 'users#new'
match '/signout', to: 'sessions#destroy', via: :delete
match '/about', to: 'static_pages#about'
match '/projects', to: 'static_pages#projects'
end
这里还有 rake:routes
sessions_new GET /sessions/new(.:format) sessions#new
users_new GET /users/new(.:format) users#new
posts GET /posts(.:format) posts#index
POST /posts(.:format) posts#create
new_post GET /posts/new(.:format) posts#new
edit_post GET /posts/:id/edit(.:format) posts#edit
post GET /posts/:id(.:format) posts#show
PUT /posts/:id(.:format) posts#update
DELETE /posts/:id(.:format) posts#destroy
static_pages GET /static_pages(.:format) static_pages#index
POST /static_pages(.:format) static_pages#create
new_static_page GET /static_pages/new(.:format) static_pages#new
edit_static_page GET /static_pages/:id/edit(.:format) static_pages#edit
static_page GET /static_pages/:id(.:format) static_pages#show
PUT /static_pages/:id(.:format) static_pages#update
DELETE /static_pages/:id(.:format) static_pages#destroy
users GET /users(.:format) users#index
POST /users(.:format) users#create
new_user GET /users/new(.:format) users#new
edit_user GET /users/:id/edit(.:format) users#edit
user GET /users/:id(.:format) users#show
PUT /users/:id(.:format) users#update
DELETE /users/:id(.:format) users#destroy
root / posts#index
signin /signin(.:format) sessions#new
signup /signup(.:format) users#new
signout DELETE /signout(.:format) sessions#destroy
about /about(.:format) static_pages#about
projects /projects(.:format) static_pages#projects
会话助手
module SessionsHelper
def sign_in(user)
cookies.permanent[:remember_token] = user.remember_token
self.current_user = user
end
def current_user=(user)
@current_user = user
end
def current_user
@current_user ||= User.find_by_remember_token(cookies[:remember_token])
end
def signed_in?
!current_user.nil?
end
def sign_out
self.current_user = nil
cookies.delete(:remember_token)
end
end