我有模特产品。我想在单个模型(产品)中创建多个记录,但字段不在视图中显示
#controller
@products = Array.new(3){ Product.new }
# view
<%= form_tag create_product_path, :method => :post, :class => "form-horizontal", 'role' => "form" do %>
<% @products.each_with_index do |product, index| %>
<% fields_for "products[#{index}]", product do |f| %>
<%= f.text_field :date %>
<%= f.text_field :name %>
<% end %>
<% end %>
<%= submit_tag "Submit", :class => "btn btn-primer" %>
查看此屏幕截图,不显示字段。任何人都可以告诉我,为什么没有出现使用数组的新表单方法?
答案 0 :(得分:1)
不要忘记<%=
fields_for
rails 2.x - 3.0
<% fields_for "products[#{index}]", product do |f| %>
rails&gt; 3.1.X
<%= fields_for "products[#{index}]", product do |f| %>