简体   繁体   English

登录后重定向到原始目的地

[英]Redirect to original destination after logging in

Rails 2.3.11导轨 2.3.11

If a user tries to go to /photos when they aren't logged in, they are directed to [site]/admin/login?route=[site]/photos .如果用户在未登录时尝试 go 到 /photos,他们将被定向到[site]/admin/login?route=[site]/photos After they log in, I would like them to be sent to whatever was defined in "route" instead of the default home page.他们登录后,我希望他们被发送到“路由”中定义的任何内容,而不是默认主页。

In /app/controllers/admin_controller.rb:在 /app/controllers/admin_controller.rb 中:

def login
    if session[:user_id] #already logged in
      redirect_to ''
    @destination = request.url
    end
    if request.post?
      if [authentication code]
        if user.activated? #check to see whether the user has activated their account
          session[:user_id] = user.id
          if params[:route] # ********
            redirect_to "#{params[:route]}"
          else
            redirect_to :controller => 'home'
          end
        else
          flash.now[:notice] = "Your account hasn't been activated yet.  Check your emails!"
        end
      else
        flash.now[:notice] = "Invalid email/password combination"
      end
    end
  end

The " * "ed line is the one that isn't working correctly. * ” ed 行是无法正常工作的行。 When I check to see the parameters, :route isn't among them, so the argument isn't being passed in with the login post.当我检查参数时, :route 不在其中,因此参数没有与登录帖子一起传递。 Can anyone explain to me why it isn't and how I could fix it?谁能向我解释为什么它不是以及我该如何解决它?

Thank you!谢谢!

You could do something like:您可以执行以下操作:

session[:return_to] ||= request.referer

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM