使用带有fields_for的数组 [英] using an array with fields_for
本文介绍了使用带有fields_for的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何使用fields_for遍历对象数组(所有相同模型)? 该数组包含由current_user创建的对象.
How can I iterate through an array of objects (all the same model) using fields_for ? The array contains objects created by the 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 %>
但是我想将:descriptionsbyuser替换为我在控制器中创建的数组-@descriptionsFromCurrentUser
But I want to replace the :descriptionsbyuser with an array I created in my controller - @descriptionsFromCurrentUser
这也位于Ryan Bate的"nested_form_for"中
This is also inside Ryan Bate's "nested_form_for"
任何指针将不胜感激!
亚当
推荐答案
文档fields_for
清楚地向您展示了使用数组的方式:
Docs for fields_for
clearly shows you the way of using arrays:
或要使用的集合:
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
这是您的数组.
这篇关于使用带有fields_for的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文