Rails使用fields_for

时间:2018-06-01 16:42:50

标签: ruby-on-rails

如何让fields_for提交数组?我在SO上发现的所有内容对我都不起作用。

我的参数正在提交:

Parameters: {"utf8"=>"✓", "authenticity_token"=>"mxHD...VoA==", "callsign"=>"baz", "post"=>{"conversation_attributes"=>{"missives_attributes"=>{"content"=>"Hello"}}}}

但我希望他们像这样提交,方括号围绕着信件:

Parameters: {"utf8"=>"✓", "authenticity_token"=>"mxHD...VoA==", "callsign"=>"baz", "post"=>{"conversation"=>{"missives"=>[{"content"=>"Hello"}]}}}

我只想提交missive的单个实例,但是以数组形式提交。换句话说,一个包含单个成员的数组。

This answer列出了实现这一目标的所有可能方法,而且每一个都失败了:

失败1

_post_form.html.erb

<%= f.fields_for :conversation do |ff| %>
  <%= ff.fields_for 'missives[]', [] do |fff| %>
    <%= fff.text_area :content %>
    ...
  <% end %>
<% end %>

结果:错误:[]:Array

的未定义方法`id'

失败2

这是一个ActiveRecord关系,所以显然我不需要getter / setter方法。

<%= f.fields_for :conversation do |ff| %>
  <% @missives = [] %>
  <%= ff.fields_for :missives, @missives do |fff| %>
    <%= fff.text_area :content %>
    ...
  <% end %>
<% end %>

结果:错误([]:Array的未定义方法`content')

失败3

<%= f.fields_for :conversation do |ff| %>
  <%= ff.fields_for :missives, @missives do |fff| %>
    <%= fff.text_area :content %>
    ...
  <% end %>
<% end %>

结果:没有在信件周围加上方括号。

失败4

在控制器中:

@missives = [Postmissive.new]

在帖子中:

<%= f.fields_for :conversation do |ff| %>
  <%= ff.fields_for :missives, @missives do |fff| %>
    <%= fff.text_area :content %>
    ...
  <% end %>
<% end %>

结果:错误(数组的未定义方法`content')。

当前代码状态:

post.rb

has_one :conversation, class_name: 'Postconversation', dependent: :destroy
accepts_nested_attributes_for :conversation

postconversation.rb

has_many :missives, class_name: 'Postmissive', dependent: :destroy
accepts_nested_attributes_for :missives

postmissive.rb

belongs_to :conversation, class_name: 'Postconversation', foreign_key: 'conversation_id'
validates :content, presence: true

posts_controller.rb

@post = @character.posts.create(post_params)
...

def post_params
  params.require(:post).permit( conversation_attributes: [ missives_attributes: [ :content ] ] )
end

_post_form.html.erb

<%= f.fields_for :conversation do |ff| %>
  <%= ff.fields_for :missives do |fff| %>
    <%= fff.text_area :content %>
    ...
  <% end %>
<% end %>

1 个答案:

答案 0 :(得分:1)

答案并不针对表单的成功/失败或表单提交后发生的事情。

它只是将missives作为数组发送。

  <%= f.fields_for :conversation, Postconversation.new do |ff| %>
    <%= ff.label :message %>
    <%= ff.text_field :message %>

    <%= ff.fields_for 'missives_attributes[]', Postmissive.new do |fff| %>
      <%= fff.label :content, [] %>
      <%= fff.text_field :content %>
      <%= fff.text_field :content %>
      <%= fff.text_field :content %>
    <% end %>
  <% end %>

请注意,只需添加Postconversation.newPostmissive.new即可使代码正常运行。您必须根据应用程序逻辑对其进行修改。

params输出:

{"utf8"=>"✓",
 "authenticity_token"=>"auth_token",
 "post"=>
  {"title"=>"Lorem Ipsum",
   "conversation_attributes"=>
    {"message"=>"Lorem ipsum",
     "missives_attributes"=>
      [{"content"=>"Dolore labore et eos ut quod"},
       {"content"=>"Lorem ipsum"},
       {"content"=>"Lorem ipsum"}]}},
 "commit"=>"Create Post"}