0

我正在关注http://ruby.railstutorial.org上的教程

当用户在访问受限页面时被提示登录然后在登录后被重定向回受限页面时,我已经设法获得了部分。

但是,登录后,我已经注销并与另一个用户登录,我将被重定向回页面,而不是新会话的默认主页。

我的会话控制器

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_back_or user
  # Sign the user in and redirect to the user's show page.
else
  # Create an error message and re-render the signin form.
  flash.now[:error] = 'Invalid email/password combination'
    render 'new'
end
end
....
end

我的会话助手

module SessionsHelper
....
def redirect_back_or(default)
  redirect_to(session[:return_to] || default)
  session.delete(:return_to)
end

def store_location
  session[:return_to] = request.fullpath
end

end
4

1 回答 1

0

添加session.delete(:return_to)到创建方法

def create
  user = User.find_by_email(params[:session][:email])
  if user && user.authenticate(params[:session][:password])
    sign_in user
    session.delete(:return_to)
    redirect_back_or user
    # Sign the user in and redirect to the user's show page.
  else
    # Create an error message and re-render the signin form.
    flash.now[:error] = 'Invalid email/password combination'
    render 'new'
  end
end
于 2012-07-26T07:34:20.153 回答