first.user = current_user 是什么意思/做什么以及为什么它有效?
首先,我有 3 个模型:
Location has_many :products
User has_many :products
Products belongs_to :user, :location
现在我有一个嵌套表单:
<%= nested_form_for @location do |f| %>
<%= f.error_messages %>
.............
<% f.fields_for :products do |product| %>
我试图找到让我的 current_user 关联工作的正确方法,突然出于偶然在这个问题中找到了正确的答案, 嵌套形式中的零外键。
我将这段简单的代码放入我的控制器中,它最终将我的产品提供给当前的 user_id:
@location.products.first.user = current_user
full -
def create
@location = Location.new(params[:location])
@location.products.first.user = current_user
end
我的最终目标完成了。用户在特定位置创建自己的产品。我唯一不明白的是为什么 first.user = current_user
有效,而像 location = @location.current_user.products.build
这样简单的东西却不起作用。有人能给我一个关于这里发生的事情以及前者的含义的很好的解释吗?这是可以接受的还是一种更安全/更好的方法?
谢谢你,我很感激。
To start, I have 3 Models:
Location has_many :products
User has_many :products
Products belongs_to :user, :location
Now i have a nested form:
<%= nested_form_for @location do |f| %>
<%= f.error_messages %>
.............
<% f.fields_for :products do |product| %>
I was trying to find the right way to get my current_user association to work and suddenly out of pure chance found the right answer in this question, Nil foreign key in a nested form.
I put this piece of simple code in my controller and it finally gave my products to the current user_id:
@location.products.first.user = current_user
full -
def create
@location = Location.new(params[:location])
@location.products.first.user = current_user
end
My end goal was accomplished. Users have their own products created at the specific location. Only thing i don't understand is why first.user = current_user
works and something simple like location = @location.current_user.products.build
doesn't. Could someone give me a great explanation on whats going on here and what the former means? Is this OK to have or is their a safer/better way?
Thank you, i do appreciate it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
定义
current_user
的最简单方法是在
ApplicationController
类中定义current_user
每当您尝试执行此操作时,
您都会尝试调用其他类中定义的方法作为
Location
实例的方法The simplest way to define
current_user
isso
current_user
is defined insideApplicationController
classwhenever you trying to do
you're trying to call method defined in other class as a method of the
Location
instance在此语句中
,Rails 默认将 current_user 的 id 分配给左侧。这是通过内部调用 to_param 实现的。 它不会有帮助:)
您甚至可以覆盖该方法以返回其他内容,甚至是随机的内容,但对于
@location.current_user.products.build
来说, ,这是行不通的,因为位置和用户不相关,如果你想做到这一点,你可以通过在新方法中构建产品并将当前用户 ID 作为参数传递给该方法来完成。
更新:我接受 tumtu 的评论,不建议将用户 ID 作为隐藏元素。
During this statement
Rails, by default, assigns the id of the current_user to the left hand side. It is made possible by the internal call to the to_param. You can even override that method to return something else, even random stuff, but it wont be helpful :)
regarding the
@location.current_user.products.build
,this will not work because location and user are not related, if you want to make this work, you can do it by building a product in the new method and passing the current user id as a parameter to the method.
UPDATE: I accept tumtu's comment, it is not advisable to put the user id as a hidden element.