Ruby-on-rails 使用 Devise/Rails 获取当前用户的 UserID
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32464224/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get UserID for Current User using Devise/Rails
提问by aishaq11
So I, a rails newbie, am currently trying to get the User ID from the current Devise session, and I am having a bit of trouble.
所以我,一个 rails 新手,目前正在尝试从当前的 Devise 会话中获取用户 ID,但我遇到了一些麻烦。
I have this in my controller now:
我现在在我的控制器中有这个:
def index
@currentUser = current_user.id
end
And I want to make a simple if statement to show/hide fields in my form.
我想做一个简单的 if 语句来显示/隐藏表单中的字段。
<% if @currentUser === @product.user_id %>
<%= link_to "Back", products_path %>
<%= link_to "Edit", edit_product_path(@product) %>
<%= link_to "Delete", product_path(@product), method: :delete, data: { confirm: "Are you sure?"} %>
<% else %>
<span></span>
<% end %>
I have a feeling my syntax in defining my currentUser variable is bad, but I have no idea how to fix this. There were a few similar questions on Stack, but none of them really applied to me or helped me.
我感觉我定义 currentUser 变量的语法很糟糕,但我不知道如何解决这个问题。Stack 上有一些类似的问题,但没有一个真正适用于我或帮助我。
Thanks in advance!
提前致谢!
回答by KensoDev
I see a few problems here
我在这里看到一些问题
def index
@currentUser = current_user.id
end
Other than what @HolyMoly already commented, you should use underscore and not camelcase, if current_useris nil here, .idwill fail on it, resulting in an exception.
除了@HolyMoly 已经评论过的内容之外,您应该使用下划线而不是驼峰式大小写,如果current_user此处为零,.id则会失败,导致异常。
Second, you are checking "ability" by comparing values of ids, I would change your code to do this
其次,您正在通过比较 ids 的值来检查“能力”,我会更改您的代码来执行此操作
<% if allow_product_delete?(@product) %>
In a helper
在帮手
def allow_product_delete?(product)
return false unless current_user
@product.user_id == current_user.id
end
if you are using devise current_userexists in controller and views, you don't need to define it as an instance variable, it's already defined as a controller method on all actions (by default). so by calling current_userin your views you are done.
如果您current_user在控制器和视图中使用设计存在,则不需要将其定义为实例变量,它已经定义为所有操作的控制器方法(默认情况下)。所以通过调用current_user你的观点你就完成了。
If a user is not logged in, current_userwill be nil, you always have to prepare for this and protect against it.
如果用户未登录,则为current_usernil,您必须始终为此做好准备并加以防范。
Last, I would look into ability gems (cancan or cancancan), those provide a very nice DSL for dealing with what you were trying here.
最后,我会研究能力宝石(cancan 或 cancancan),它们提供了一个非常好的 DSL 来处理你在这里尝试的东西。
回答by HolyMoly
Have you set it up to actually be able to user current_user?
in your code you have :
你有没有设置它实际上能够用户current_user?在您的代码中,您有:
@currentUser = current_user.id
@currentUser = current_user.id
But where was current_user defined? I haven't used Devise but with sessions you could define the value of current_user with a helper method (you may be able to also do it right there in the sessions_controller #create method), like this:
但是 current_user 在哪里定义的?我没有使用过 Devise,但是对于会话,您可以使用辅助方法定义 current_user 的值(您也可以在 session_controller #create 方法中进行此操作),如下所示:
def current_user
@current_user ||= User.find_by(id: session[:user_id])
end
then throughout your app you could use current_user.idor current_user.nameor whatever you needed.
然后在您的整个应用程序中,您可以使用current_user.id或current_user.name或任何您需要的东西。
So while sessions is not Devise, I hope this helps .
因此,虽然会话不是设计,但我希望这会有所帮助。
回答by asiniy
First of all, ruby like undescored_rather_than camelCased style.
首先,ruby 喜欢 undescored_rather_than camelCased 风格。
Secondly, you need two equal signs instead of three. @currentUser == @product.user_idshould work
其次,你需要两个等号而不是三个。@currentUser == @product.user_id应该管用
At third, you don't need to compare integer ids, you can compare models, something like that:
第三,你不需要比较整数 id,你可以比较模型,像这样:
<% if current_user == @product.user %>
<%= link_to "Back", products_path %>
<%= link_to "Edit", edit_product_path(@product) %>
<%= link_to "Delete", product_path(@product), method: :delete, data: { confirm: "Are you sure?"} %>
<% end %>
Please note that I omitted the @in current_usermethod, because current_useris helper for devise, else I removed unnecessary <% else %>(in my opinion)
请注意,我省略了@incurrent_user方法,因为它current_user是设计的助手,否则我删除了不必要的<% else %>(在我看来)
回答by Dima Melnik
You should use current_user for this. You can do it everywhere in project. No needs to define variable @currentUser or etc.
您应该为此使用 current_user。您可以在项目中的任何地方进行。无需定义变量@currentUser 等。
You can compare like models
您可以比较类似的模型
if current_user == @product.user
that is the equivalent to
这相当于
if current_user.id == @product.user.id
In some cases you can use
在某些情况下,您可以使用
if current_user.id == @product.user_id
This prevent extra sql query to load user model
这可以防止额外的 sql 查询加载用户模型
If you are using Devise for authorization and you need control access for actions you should see (maybe use) gem https://github.com/CanCanCommunity/cancancan
如果您使用 Devise 进行授权并且需要控制操作的访问权限,您应该看到(也许使用) gem https://github.com/CanCanCommunity/cancancan
https://github.com/airbnb/rubywill help you to stay in tune ;)
https://github.com/airbnb/ruby将帮助您保持同步;)
回答by msdundar
Comparing IDs is not the best practice for Rails. If you have previously set up the associations correctly, then you don't need to make a comparison between IDs. For example:
比较 ID 不是 Rails 的最佳实践。如果您之前已正确设置关联,则无需在 ID 之间进行比较。例如:
User
has_many :group_events
GroupEvent
belongs_to :user
In this scenario, instead of comparing IDs, you can directly compare with association:
在这种情况下,您可以直接与关联进行比较,而不是比较 ID:
Bad
@group_event.user_id == current_user.id
Good
@group_event.user == current_user

