加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – Rails重定向到登录前想要查看的页面用户

发布时间:2020-12-17 02:17:52 所属栏目:百科 来源:网络整理
导读:我正在尝试将用户重定向回他们在登录之前尝试访问的页面(该页面只能供用户查看). 我将从一个针对我的旅行新动作的前过滤器开始: before_filter:authenticate,:only = [:new,:create,:edit,:update,:destroy] def authenticate deny_access unless si
我正在尝试将用户重定向回他们在登录之前尝试访问的页面(该页面只能供用户查看).

我将从一个针对我的旅行新动作的前过滤器开始:

before_filter:authenticate,:only => [:new,:create,:edit,:update,:destroy]

def authenticate
  deny_access unless signed_in?
end 

def deny_access
  store_location
  redirect_to login_path,:notice => "Please log in to access this page."
end

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

这是我的sessions_controller.rb中的新操作和创建操作

def new
  @title = "Log in"
end

def create
  user = User.authenticate(params[:session][:email].downcase,params[:session][:password])     
  if user.nil?
    flash.now[:error] = "Invalid email/password combination."
    @title = "Log in"
    render 'new'
  else
    sign_in user
    redirect_back_or(root_path)
  end
end

def redirect_back_or(default)
  redirect_to(session[:return_to] || default)
  clear_return_to
end

谢谢!

编辑:删除了MaddHacker的代码版本,因为这不是我想要的.

解决方法

我通常喜欢使用before_filter

def login_required
  session[:return_to] = request.fullpath
  redirect_to(login_page_url)
end

然后当我的登录完成时,我会在会话中查找return_to:

def login
  # user validation of login here
  url = session[:return_to] || root_path
  session[:return_to] = nil
  url = root_path if url.eql?('/logout')
  logger.debug "URL to redirect to: #{url}"
  redirect_to(url)
end

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读