遵循Hartl的铁路指导。当我尝试登录时,它会转到正确的页面,但“登录成功!”不会在界面上闪烁。此外,当我尝试退出时,我收到路由错误:No route matches [GET] "/sessions/1"
。
任何帮助将不胜感激!
这是我的sessions_controller.rb:
class SessionsController< ApplicationController中
def new
@title = "Log in"
end
def create
user = User.authenticate(params[:session][:email],
params[:session [:password])
if user.nil?
flash.now[:error] = "Invalid email/password combination."
@title = "Sign in"
render 'new'
else
flash[:success] = "Successful login!"
sign_in user
redirect_back_or user
end
end
def destroy
sign_out
redirect_to root_path
end
end
这是我的routes.rb:
SampleApp::Application.routes.draw do
#resources :users this is the line I removed to get my code to work
resource :user #this is the lined I added to get my code to work
resources :sessions, :only => [:new, :create, :destroy]
match '/sessions', :to => 'users#show'
match '/signup', :to => 'users#new'
match '/login', :to => 'sessions#new'
match '/logout', :to => 'sessions#destroy'
root :to => 'pages#home'
将resources :users
更改为resource :user
似乎允许我“登录”和“退出”,但用户界面上仍未显示Flash成功。这让我相信我的创建方法存在错误。
这是我的sessions_helper.rb:
module SessionsHelper
def sign_in(user)
cookies.permanent.signed[:remember_token] = [user.id, user.salt]
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_from_remember_token
end
def current_user?(user)
user == current_user
end
def sign_out
cookies.delete(:remember_token)
self.current_user = nil
end
def redirect_back_or(default)
redirect_to(session[:return_to] || default)
clear_return_to
end
private
def user_from_remember_token
User.authenticate_with_salt(*remember_token)
end
def remember_token
cookies.signed[:remember_token] || [nil, nil]
end
def store_location
session[:return_to] = request.fullpath
end
def clear_return_to
session[:return_to] = nil
end
end
已编辑:已在代码中发布解决方案。
答案 0 :(得分:3)
我认为您应该只使用flash[:success]
和flash[:error]
而不是flash.now
你用什么链接注销?