first.user = current_user 是什么意思/做什么以及为什么它有效?

发布于 2024-11-18 20:13:00 字数 994 浏览 2 评论 0原文

首先,我有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

旧街凉风 2024-11-25 20:13:00

定义 current_user 的最简单方法是

class ApplicationController < ActionController::Base  
  protect_from_forgery  
  helper_method :current_user  

private  
  def current_user  
    @current_user ||= User.find(session[:user_id]) if session[:user_id]  
  end  
end 

ApplicationController 类中定义 current_user
每当您尝试执行此操作时,

@location = Location.new(params[:location])
@location.current_user 

您都会尝试调用其他类中定义的方法作为 Location 实例的方法

The simplest way to define current_user is

class ApplicationController < ActionController::Base  
  protect_from_forgery  
  helper_method :current_user  

private  
  def current_user  
    @current_user ||= User.find(session[:user_id]) if session[:user_id]  
  end  
end 

so current_user is defined inside ApplicationController class
whenever you trying to do

@location = Location.new(params[:location])
@location.current_user 

you're trying to call method defined in other class as a method of the Location instance

握住你手 2024-11-25 20:13:00

在此语句中

@location.products.first.user = current_user

,Rails 默认将 current_user 的 id 分配给左侧。这是通过内部调用 to_param 实现的。 它不会有帮助:)

您甚至可以覆盖该方法以返回其他内容,甚至是随机的内容,但对于 @location.current_user.products.build 来说, ,
这是行不通的,因为位置和用户不相关,如果你想做到这一点,你可以通过在新方法中构建产品并将当前用户 ID 作为参数传递给该方法来完成。

def new
  @location = Location.new
  @location.products.build
  respond_to do|format|
    format.html
  end
end

更新:我接受 tumtu 的评论,不建议将用户 ID 作为隐藏元素。

During this statement

@location.products.first.user = current_user

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.

def new
  @location = Location.new
  @location.products.build
  respond_to do|format|
    format.html
  end
end

UPDATE: I accept tumtu's comment, it is not advisable to put the user id as a hidden element.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文