从 Devise、Ruby on Rails 获取用户 ID
我使用 Devise 和基本 HTTP 身份验证从移动应用程序访问用户区域。当用户在应用程序中输入其详细信息并尝试“登录”时(当您随每个请求发送凭据时,您实际上并没有使用基本身份验证登录),我希望收到包含用户信息的响应,例如用户 ID和电子邮件。
当我尝试获取资源时,我得到如下响应:
[
-
{
email: "[email protected]"
username: "Example"
}
-
[
-
{
created_at: "2011-07-26T11:30:55Z"
id: 1
title: "Test"
updated_at: "2011-07-26T11:30:55Z"
user_id: 1
}
-
{
created_at: "2011-07-26T16:53:26Z"
id: 2
title: "Test2"
updated_at: "2011-07-26T16:53:26Z"
user_id: 1
}
]
]
所以我一开始就获取了用户对象。但是,我还需要添加用户 ID。我无法在项目中使用 user_id,因为如果用户没有项目,则不会呈现该 user_id。
有什么建议吗?
I use Devise and Basic HTTP Authentication to access user areas from a mobile app. When the user enters its details in the app and it tries to "login" (you don't actually login with basic auth as you send the credentials with each request) I want to get a response back with the user info, like user ID and email.
When I try to get a resource I get a response like this:
[
-
{
email: "[email protected]"
username: "Example"
}
-
[
-
{
created_at: "2011-07-26T11:30:55Z"
id: 1
title: "Test"
updated_at: "2011-07-26T11:30:55Z"
user_id: 1
}
-
{
created_at: "2011-07-26T16:53:26Z"
id: 2
title: "Test2"
updated_at: "2011-07-26T16:53:26Z"
user_id: 1
}
]
]
So I get the user object at the beginning. However, I need to add the user ID as well. I can't use the user_id in the items because that won't be rendered if the user has no items.
Any tips?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可能需要扩展
Devise::SessionsConroller
。在您的控制器文件夹中,添加一个用户文件夹,然后添加一个名为sessions_controller.rb
的文件,其中包含以下内容:在
您的routes.rb
中:编辑
尝试此操作。从
routes.rb
中取出该行。在application_controller.rb
中添加以下内容:You probably need to extend
Devise::SessionsConroller
. In your controllers folder, add a users folder and then add a file calledsessions_controller.rb
that contains this:In
your routes.rb
:EDIT
Try this. Take that line out of your
routes.rb
. Inapplication_controller.rb
add this:经过一个下午的努力寻找一个好的答案,我发现了这个:
希望有帮助!
after an aftternoon to try to find a good answer, i've found this:
Hope it Helps!