导轨重定向页面用户想先查看登录 [英] Rails redirect to page user wanted to view prior to login

查看:150
本文介绍了导轨重定向页面用户想先查看登录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将用户重定向回他们试图先于记录访问的页面(页面只有查看到用户)。

我会用前过滤器,针对我的旅行新的行动开始

的before_filter:身份验证,:只=> [:新:创建,编辑,:更新:摧毁]

  DEF认证
  deny_access除非signed_in?
结束高清deny_access
  store_location
  redirect_to的login_path,:通知=> 请登录访问此页面。
结束高清store_location
  会话[:的return_to] = request.fullpath
结束

下面是我sessions_controller.rb新创建行动

 高清新
  @title =登陆
结束打造高清
  用户= User.authenticate(PARAMS [:会议] [:邮箱] .downcase,
                           PARAMS [:会议] [:密码])
  如果user.nil?
    flash.now [:错误] =无效的电子邮件/密码组合。
    @title =登陆
    使新
  其他
    sign_in用户
    redirect_back_or(root_path)
  结束
结束高清redirect_back_or(默认)
  redirect_to时(会话[:的return_to] ||默认)
  clear_return_to
结束

谢谢!

编辑:删除MaddHacker的版本我的code作为是不是我想要的。


解决方案

我通常使用像

一个的before_filter

 高清login_required
  会话[:的return_to] = request.fullpath
  redirect_to时(login_page_url)
结束

然后当我登录完成后,我期待在会话中的return_to:

  DEF登录
  #用户登录验证这里
  URL =会话[:的return_to] || root_path
  会话[:的return_to] =零
  URL = root_path如果url.eql?('/注销)
  logger.debugURL重定向到:#{URL}
  redirect_to时(URL)
结束

I'm trying to redirect users back to a page that they were trying to access prior to logging in (the page is only viewable to users).

I'll start off with a before filter that targets my trip new action:

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

Here is the new and create actions in my 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

Thanks!

Edit: Removed MaddHacker's version of my code as that is not what I want.

解决方案

I usually use a before_filter like

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

Then when my login completes, I look for the return_to in the session:

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

这篇关于导轨重定向页面用户想先查看登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆