Ruby-on-rails 使用带有 fields_for 的数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11212631/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 03:36:16  来源:igfitidea点击:

using an array with fields_for

ruby-on-railsarraysfields-for

提问by adamteale

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.

如何使用 fields_for 遍历对象数组(所有相同的模型)?该数组包含由 current_user 创建的对象。

I currently have:

我目前有:

<%= 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 %>

But I want to replace the :descriptionsbyuser with an array I created in my controller - @descriptionsFromCurrentUser

但我想用我在控制器中创建的数组替换 :descriptionsbyuser - @descriptionsFromCurrentUser

This is also inside Ryan Bate's "nested_form_for"

这也在 Ryan Bate 的“nested_form_for”里面

Any pointers would be greatly appreciated!

任何指针将不胜感激!

Adam

亚当

回答by barelyknown

To use a collection for fields_forand have it work the way that you expect, the model needs accept nested attributes for the collection. If the collection is an ActiveRecordone-to-many relationship, use the accepts_nested_attributes_forclass macro. If the collection is not an ActiveRecordone-to-many relationship, you'll need to implement a collection getter and a collection attributes setter.

要使用集合fields_for并让它按照您期望的方式工作,模型需要接受集合的嵌套属性。如果集合是ActiveRecord一对多关系,请使用accepts_nested_attributes_for类宏。如果集合不是ActiveRecord一对多关系,则需要实现集合 getter 和集合属性 setter。

If it is an ActiveRecordrelationship:

如果是ActiveRecord关系:

class Person
  has_many :projects

  # this creates the projects_attributes= method
  accepts_nested_attributes_for :projects
end

If it is a non-ActiveRecordrelationship:

如果是非ActiveRecord关系:

class Person
  def projects
    ...
  end

  def projects_attributes=(attributes)
    ...
  end
end

Either way, the form is the same:

无论哪种方式,形式都是相同的:

<%= form_for @person do |f| %>
  ...
  <%= f.fields_for :projects, @active_projects do |f| %>
    Name: <%= f.text_field :name %>
  <% end %>
  ...
<% end %>

回答by jdoe

Docs for fields_forclearly shows you the way of using arrays:

文档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 %>

或者要使用的集合:

<%= 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_projectshere is your array.

@active_projects这是你的阵列。

回答by Richard Kuo

i found this to be the cleanest way

if you are working with straight data and want to send back an array without using any of these @objects

我发现这是最干净的方法,

如果您正在使用直接数据并想在不使用任何这些@objects 的情况下发回数组

<%= form_for :team do |t| %>
  <%= t.fields_for 'people[]', [] do |p| %>
    First Name: <%= p.text_field :first_name %>
    Last Name: <%= p.text_field :last_name %>
  <% end %>
<% end %>

your params data should return like this

你的参数数据应该像这样返回

"team" => {
  "people" => [
    {"first_name" => "Michael", "last_name" => "Jordan"},
    {"first_name" => "Steve", "last_name" => "Jobs"},
    {"first_name" => "Barack", "last_name" => "Obama"}
  ]
}

回答by Sid Krishnan

An addition to barelyknown's answer (wasn't able to add as a comment due to reputation points) -

对几乎不知道的答案的补充(由于声誉点,无法作为评论添加)-

For the non-activerecord case, I also had to define persisted?in my class in addition to *_attributes=(attributes).

对于非 activerecord 情况,persisted?除了*_attributes=(attributes).