Ruby on rails Rails设计-将URL传递到登录
我是否有办法将URL传递到Desive登录页面,以便在用户登录时将其重定向回该URL 比如:Ruby on rails Rails设计-将URL传递到登录,ruby-on-rails,devise,Ruby On Rails,Devise,我是否有办法将URL传递到Desive登录页面,以便在用户登录时将其重定向回该URL 比如: /login?passthru=/somethingawesome 还是设置会话变量更好?在应用程序\u控制器中有存储重定向位置的方法和访问存储重定向位置的方法: def store_location(path) session[:return_to] = request.request_uri || path end def redirect_back_or_default(default)
/login?passthru=/somethingawesome
还是设置会话变量更好?在
应用程序\u控制器中有存储重定向位置的方法和访问存储重定向位置的方法:
def store_location(path)
session[:return_to] = request.request_uri || path
end
def redirect_back_or_default(default)
redirect_to(session[:return_to] || default)
session[:return_to] = nil
end
重写方法中的after_sign_,以将用户重定向到所需位置:
def after_sign_in_path_for(resource_or_scope)
redirect_back_or_default(resource_or_scope)
end
设计维基:如何:
顺便说一句,上面的方法未经测试,您应该对其进行测试。以下是我所做的
1) 在模板中设置登录名
如下所示:Im passing request.fullpath在这里作为一个例子,你可以用你想要的任何东西替换它
<%= link_to "Log in", new_user_session_path(:passthru => request.fullpath %>
更新更新版本(Ruby 2.2.0、Rails 4.2.0、Desive 3.2.4):
应用程序\u控制器.rb
before_action :store_location
private
def store_location
session[:requestUri] = params[:requestUri] if params[:requestUri].present?
end
# Custom After Sign in Path
def after_sign_in_path_for(resource_name)
if session[:requestUri]
session.delete(:requestUri)
else
super
end
end
<%= link_to ('Get This'), some_require_auth_path(@something.id, requestUri: request.fullpath), class: 'button' %>
设计会话\u Controller.rb
before_action :store_location
private
def store_location
session[:requestUri] = params[:requestUri] if params[:requestUri].present?
end
# Custom After Sign in Path
def after_sign_in_path_for(resource_name)
if session[:requestUri]
session.delete(:requestUri)
else
super
end
end
<%= link_to ('Get This'), some_require_auth_path(@something.id, requestUri: request.fullpath), class: 'button' %>
查看xxxx.html.erb
before_action :store_location
private
def store_location
session[:requestUri] = params[:requestUri] if params[:requestUri].present?
end
# Custom After Sign in Path
def after_sign_in_path_for(resource_name)
if session[:requestUri]
session.delete(:requestUri)
else
super
end
end
<%= link_to ('Get This'), some_require_auth_path(@something.id, requestUri: request.fullpath), class: 'button' %>