Rails Devise - 通过URL登录 [英] Rails Devise - Pass URL to login

查看:156
本文介绍了Rails Devise - 通过URL登录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法让URL传递给Devise登录页面,这样当用户登录时,他/她将被重定向回该网址?

Is there a way for me to pass an URL to the Devise login page, so that when a user logs in, he/she is redirected back to that URL?

如下:

/login?passthru=/somethingawesome

或者更好的设置会话变量?

Or is it better to set a session variable?

推荐答案

这是我做的。

1)在你的模板中设置你的sign_in登录
,如下所示:我将request.fullpath作为一个例子,将其替换为无论你想要什么。

1) In your template set up your sign_in login as follows: Im passing request.fullpath here as an example you can replace this with whatever you want.

<%= link_to "Log in", new_user_session_path(:passthru => request.fullpath %>

2)接下来修改ApplicationController如下:我们添加一个before_filter,如果它在会话中设置passthru存在。然后我们重写after_sign_in_path_for来查看passthru的会话。如果它不在那里将默认为root_path。只要您随时随地使用params处理登录,这应该可以正常工作。虽然可能需要一些调整。

2) Next modify ApplicationController as follows: we add a before_filter which sets passthru in the session if it exists. Then we override after_sign_in_path_for to look in the session for passthru. If its not there it will default to root_path. As long as you handle logins with params consistently everywhere this should work. Although it might need some tweaking.

before_filter :store_location

def store_location
  session[:passthru] = params[:passthru] if params[:passthru]
end

def redirect_back_or_default(default)
  session[:passthru] || root_path
end

def after_sign_in_path_for(resource_or_scope)
  redirect_back_or_default(resource_or_scope)
end

这篇关于Rails Devise - 通过URL登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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