Rails 3.1 fields_for
这是我的 orders
控制器的 edit
视图中的 fields_for
块
<%= form_for @order do |f| %>
<%= f.fields_for :order_fields do |builder| %>
<%= builder.text_field :value %>
<% end %>
<% end %>
我遇到的问题是我想使用 every 循环来显示循环中的信息,但随后我多次显示该表单。这是因为我使用了 accepts_nested_attributes_for :order_fields
语句。
在我的 order_fields
表中,有一个名为 name
的列,我想在表单中显示信息。我该怎么做?我的数据库中 value
列的信息已显示在我的表单中。
如果我这样做
<%= f.fields_for :order_fields do |builder| %>
<%= builder.text_field :name %>
<%= builder.text_field :value %>
<% end %>
,它会显示相邻两列中的信息,但我希望 :name
信息不要出现在文本框或任何内容中。我希望它显示得好像我要做类似的事情:
<% @order.order_fields.each do |field| %>
<%= field.name %>
<% end %>
我希望这有意义:) 谢谢。
Here is my fields_for
block in my edit
view of the orders
controller
<%= form_for @order do |f| %>
<%= f.fields_for :order_fields do |builder| %>
<%= builder.text_field :value %>
<% end %>
<% end %>
The problem I am having is that I would like to use an each loop to display information from the loop, but then i get the form displayed more than once. this is because i used the accepts_nested_attributes_for :order_fields
statement.
Inside my order_fields
table there is a column named name
that I would like to display the information from in the form. How can I do this? The information from the value
column in my database is displayed in my form already.
If i do
<%= f.fields_for :order_fields do |builder| %>
<%= builder.text_field :name %>
<%= builder.text_field :value %>
<% end %>
it displays information from both columns next to each other, but I would like the :name
information not to be in a text box or anything. I would like it to be displayed as if i were to do something like:
<% @order.order_fields.each do |field| %>
<%= field.name %>
<% end %>
I hope this make some sense :)
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以从 |builder| 访问该对象通过调用 .object 来实现
You can access the object off of |builder| by calling .object on it