在Devise登录期间如何传递参数 [英] How to pass through parameters during Devise login
问题描述
在Devise登录过程中如何传递参数?
How do I pass parameters during the Devise sign-in process?
我有一组用户,每个用户都有自己的个人资料页面。我希望他们能够查看自己的个人资料页面,但不能查看其他用户的个人资料页面,除非他们是管理员,在这种情况下,他们可以访问应用程序中的所有内容。
I have a collection of users, each with their own profile page. I want them to be able to view their own profile page but none of the other users' profile pages, unless they're an admin in which case they have access to everything in the application.
我创建了一个Users控制器,并将以下代码应用于该控制器的开头。我用用户名替换了按ID查找,因此 / users / username
可以访问个人资料:
I created a Users controller, and applied the following code to the beginning of the controller. I replaced looking up by ID with their username, so /users/username
to access a profile:
filter_access_to :all do
current_user == User.find_by_username(params[:id]) or
has_role? :admin
end
而我在route.rb中指定了:
And I specified in routes.rb:
root :to => "users#show"
当用户访问 http:// appli时。 cat.ion
,将提示他们输入登录凭据,然后将其重定向到 root_path
,这是Users控制器的显示操作。除非我可以在登录过程中传递 id
参数,否则这将导致无限的重定向循环。我相信以下两种Devise方法可以帮助您解决这个问题吗?
When the user accessed http://appli.cat.ion
they would be prompted for login credentials, then be redirected to the root_path
which is the show action of the Users controller. Unless I can pass along the id
param during the sign-in process, this will result in an endless redirect loop. Is there something in Devise that helps with this problem?
我相信以下两种Devise方法可以提供帮助:
I believe the following two Devise methods can help:
after_sign_in_path_for(resource)
和
stored_location_for(resource)
我已将 after_sign_in_path
覆盖为以下内容:
I've overriden after_sign_in_path
to the following:
def after_sign_in_path_for(resource)
user_root_path(:id => current_user.username)
end
具有预期的效果:用户登录,重定向到他们的个人资料页面,但前提是我在 stored_location_for $ c $中返回
nil
c>像:
which provides the desired effect: user logs in, gets redirected to their profile page, but only if I return nil
in stored_location_for
like:
def stored_location_for(resource)
nil
end
同样,我的目标是如果导航到(http),则将用户重定向到其个人资料页:/ /appli.cat.ion /
直接登录。如果用户导航到(http)://appli.cat.tion/otherresources
,则我要登录该用户,然后将其重定向到他们请求的页面。如果我在 stored_location_for
中返回 nil
,那么无论如何用户总是会重定向到其个人资料页面,这是不可取的充其量是不好的,最坏的情况是不可用的。我认为,如果我直接导航到应用程序登录,则 stored_location
将返回 nil
,并且在登录后定向到我的个人资料页面,但这只是将我重定向到 root_path
。
Again, my goal is to redirect a user to their profile page if having navigated to (http)://appli.cat.ion/
directly if they've signed-in. If a user navigates to (http)://appli.cat.tion/otherresources
then I want to sign the user in, then redirect them to the page they requested. If I return nil
in stored_location_for
then the user will ALWAYS get redirected to their profile page no matter what, which is undesirable at best, and unusable at worst. I think that if I navigate to the application sign-in directly then stored_location
would return nil
, and upon signing in be directed to my profile page, but it is just redirecting me to the root_path
.
推荐答案
我认为您可以在UsersController中创建一个配置文件操作,该操作会重定向到用户显示页面,然后将其标记为用户root。
I think you can create a profile action within the UsersController which does a redirect to the user show page, then mark it as user root.
在UsersController中:
In UsersController:
class UsersController < ApplicationController
...
def profile
redirect_to user_url(:id => current_user.username)
end
...
end
在路线中:
resources :users do
get 'profile', :on => :collection, :as => :user_root
end
并且不需要使用 after_sign_in_path
钩子。
这篇关于在Devise登录期间如何传递参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!