Ruby on Rails:“form_for(:product, ...)”是否是“form_for(:product, ...)”?和“form_for(@product,...)”相等的?
是
<%= 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
form_for
帮助器中的@product
附带了更多功能。:product
仅影响输入字段的 id 和名称。例如,您有一个以以下形式提交的文本:生成的 html 如下所示:
id
和name
值由:product.to_s 和文本字段名称。
而如果您使用
@product
,则:url
不是必需的,因为 url 将根据@product
的状态确定:@product
是一条新记录,该 url 将发布到create
update
以及输入字段的 id 和名称受
@product
的类名影响,因此当您使用单表继承时,这一点很重要。输入字段的值会自动分配给@product
的属性值。因此,如果您使用@product
,html 输出将如下所示:假设
@product
的类名称为Item
,则输出将为更改为:当然,您可以同时使用
:product
和@product
::product
控制输入字段的名称和 id,并且@product
控制 url 和输入字段的值。The
@product
in theform_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:The generated html would look like:
The
id
andname
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:@product
is a new record, the url would post tocreate
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:Assume the
@product
's class name isItem
, then the output would change to:And of course you can use both of
:product
and@product
:The
:product
controls input filed's name and id, and the@product
controls the url and input field's value.相似,但不一样。当您使用 @product 时,您的好处是能够自动将模型实例中的值填充到表单字段中。
例如,如果您在控制器的 :new 操作中像这样分配 @product:
那么生成的表单可能不会有任何差异。但是,如果您按照以下方式执行 :create 操作:
然后,如果无法保存 @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:
Then there will probably not be any difference in the generated form. However, if you follow this up in you :create action like this:
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
这是一个相当长的
form_for
。对您的问题的简短回答:@product
对于确定对可以包含许多对象的资源执行什么操作非常有用,这就是听起来的样子。另一方面,:product
将始终执行相同的操作,更新
。这最适合用于单一资源。可以在路由指南中找到有关资源的详细说明。 入门指南中也对此进行了解释。您的第二个
form_for
可以简写为:路由和入门指南中解释了所有内容。
另外,我在你的个人资料中看到你来自墨尔本。 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: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.