我如何使用fields_for遍历对象数组(所有相同模型)?
该数组包含由current_user创建的对象。
我目前有:
<%= f.fields_for :descriptionsbyuser do |description_form| %>
<p class="fields">
<%= description_form.text_area :entry, :rows => 3 %>
<%= description_form.link_to_remove "Remove this description" %>
<%= description_form.hidden_field :user_id, :value => current_user.id %>
</p>
<% end %>
但是我想用我在 Controller 中创建的数组替换:descriptionsbyuser-@descriptionsFromCurrentUser
这也在Ryan Bate的“nested_form_for”中
任何指针将不胜感激!
亚当
最佳答案
Docs for fields_for
清楚地向您展示了使用数组的方式:
Or a collection to be used:
<%= form_for @person do |person_form| %> ... <%= person_form.fields_for :projects, @active_projects do |project_fields| %> Name: <%= project_fields.text_field :name %> <% end %> ... <% end %>
@active_projects
这是您的数组。
关于ruby-on-rails - 使用带有fields_for的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11212631/