Ruby on Rails:“form_for(:product, ...)”是否是“form_for(:product, ...)”?和“form_for(@product,...)”相等的?

发布于 2024-10-07 11:10:10 字数 329 浏览 5 评论 0原文

<%= form_for(:product, :url => {:action => 'update', :id => @product.id})) do |f| %>
  ...
<% end %>

<%= form_for(@product, :url => {:action => 'update', :id => @product.id})) do |f| %>
  ...
<% end %>

完全一样吗?

Is

<%= form_for(:product, :url => {:action => 'update', :id => @product.id})) do |f| %>
  ...
<% end %>

and

<%= form_for(@product, :url => {:action => 'update', :id => @product.id})) do |f| %>
  ...
<% end %>

exactly the same ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

冰雪之触 2024-10-14 11:10:10

form_for 帮助器中的 @product 附带了更多功能。

:product 仅影响输入字段的 id 和名称。例如,您有一个以以下形式提交的文本:

<%= form_for :product, :url => {...} do |f| %>
  <%= f.text_field :price %>
<% end %>

生成的 html 如下所示:

<input type="text" id="product_price" name="product[price]" />

idname 值由 :product.to_s 和文本字段名称。

而如果您使用 @product,则 :url 不是必需的,因为 url 将根据 @product 的状态确定:

  • 如果@product 是一条新记录,该 url 将发布到 create
  • 否则,该 url 将发布到 update

以及输入字段的 id 和名称受 @product 的类名影响,因此当您使用单表继承时,这一点很重要。输入字段的值会自动分配给 @product 的属性值。因此,如果您使用 @product,html 输出将如下所示:

<input type="text" id="product_price" name="product[price]" value="some value" />

假设 @product 的类名称为 Item,则输出将为更改为:

<input type="text" id="item_price" name="item[price]" value="some value" />

当然,您可以同时使用 :product@product

<%= form_for :product, @product do |f| %>

:product 控制输入字段的名称和 id,并且@product 控制 url 和输入字段的值。

The @product in the form_for helper ships with more features.

The :product only affects the input field's id and name. For example you have a text filed in the form:

<%= form_for :product, :url => {...} do |f| %>
  <%= f.text_field :price %>
<% end %>

The generated html would look like:

<input type="text" id="product_price" name="product[price]" />

The id and name value is determined by the :product.to_s and the text field name.

While if you use @product, the :url is not necessary because the url would be determined according to the @product's status:

  • if the @product is a new record, the url would post to create
  • otherwise, the url would post to update

And the input filed's id and name is affected by @product's class name, so it's important when you're using single table inheritant. The input filed's value is automatically assigned with the @product's attribute value. So if you use @product, the html output would look like:

<input type="text" id="product_price" name="product[price]" value="some value" />

Assume the @product's class name is Item, then the output would change to:

<input type="text" id="item_price" name="item[price]" value="some value" />

And of course you can use both of :product and @product:

<%= form_for :product, @product do |f| %>

The :product controls input filed's name and id, and the @product controls the url and input field's value.

锦爱 2024-10-14 11:10:10

相似,但不一样。当您使用 @product 时,您的好处是能够自动将模型实例中的值填充到表单字段中。

例如,如果您在控制器的 :new 操作中像这样分配 @product:

@product = Product.new

那么生成的表单可能不会有任何差异。但是,如果您按照以下方式执行 :create 操作:

@product = Product.new(params[:product])
if @product.save
  ...
else
  render :action => :new
end

然后,如果无法保存 @product 实例,那么它将呈现与 :new 中相同的表单,但这次所有字段都填充了发布的值。如果您使用 :product ,这是不可能的

Similar, but not the same. When you use @product you have the benefit of being able to automatically populate values to the form fields from the model instance.

For example, if you in the action :new in your Controller assign @product like this:

@product = Product.new

Then there will probably not be any difference in the generated form. However, if you follow this up in you :create action like this:

@product = Product.new(params[:product])
if @product.save
  ...
else
  render :action => :new
end

Then if it is unable to save the @product instance, then it will render the same form as in :new but this time have all the fields populated with the values posted. That would not have been possible if you used :product

风和你 2024-10-14 11:10:10

这是一个相当长的form_for。对您的问题的简短回答:@product 对于确定对可以包含许多对象的资源执行什么操作非常有用,这就是听起来的样子。另一方面,:product 将始终执行相同的操作,更新。这最适合用于单一资源。可以在路由指南中找到有关资源的详细说明。 入门指南中也对此进行了解释。

您的第二个 form_for 可以简写为:

<%= form_for @product do |f| %>
   ...
<% end %>

路由和入门指南中解释了所有内容。

另外,我在你的个人资料中看到你来自墨尔本。 Ruby on Rails Oceania Google 群组列出了全国各地的聚会。墨尔本每个月都会举办一次活动,您可能想参加,结识志同道合的人。

That's quite a long form_for you've got there. The short answer to your question: @product is useful for determining what action to go to for a resource that can have many objects, which is what this sounds like. :product on the other hand will always go to the same action, update. This is best used for singular resources. A great explanation of the resources can be found in the Routing Guide. It's also explained in the Getting Started guide.

Your second form_for could be shorted right down to this:

<%= form_for @product do |f| %>
   ...
<% end %>

All is explained in the Routing and Getting Started guides.

Also, I saw in your profile you're from Melbourne. There's the Ruby on Rails Oceania Google group which lists the meetups around the country. There's one each month in Melbourne which you may want to attend to meet like minded people.

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