Rails redirect to page user wanted to view prior to login

北城余情 提交于 2019-12-10 17:13:48

问题


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.


回答1:


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



回答2:


Well, at first you need to call the store_return_to method before redirecting from the unauthorised page to the login page. Then I think you need to change the method description to something like def redirect_back_or_default(default = root_path) so the argument becomes optional.

By the way, Ican't realy understand why do you need to pass an argument to that method, if it is only a default value, wich you can hardcode in the method itself.



来源:https://stackoverflow.com/questions/10539197/rails-redirect-to-page-user-wanted-to-view-prior-to-login

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!