[英]Rails submitting an array with fields_for
How do you get fields_for
to submit an array? 你如何获得
fields_for
提交数组? Everything I've found suggested on SO doesn't work for me. 我发现的所有建议对我都不起作用。
My params are submitting like this: 我的参数是这样提交的:
Parameters: {"utf8"=>"✓", "authenticity_token"=>"mxHD...VoA==", "callsign"=>"baz", "post"=>{"conversation_attributes"=>{"missives_attributes"=>{"content"=>"Hello"}}}}
but I want them to submit like this, with the square brackets around missives: 但我希望他们这样提交,并在方括号周围加上方括号:
Parameters: {"utf8"=>"✓", "authenticity_token"=>"mxHD...VoA==", "callsign"=>"baz", "post"=>{"conversation"=>{"missives"=>[{"content"=>"Hello"}]}}}
I just want to submit a single instance of missive
, but in array form. 我只想提交一个
missive
实例,但是以数组形式。 In other words, an array with single member. 换句话说,一个具有单个成员的数组。
This answer lists all the possible ways to get this working, and I have failed with every single one: 这个答案列出了所有可行的方法来解决这个问题,而我每一个都失败了:
FAIL 1 失败1
_post_form.html.erb _post_form.html.erb
<%= f.fields_for :conversation do |ff| %>
<%= ff.fields_for 'missives[]', [] do |fff| %>
<%= fff.text_area :content %>
...
<% end %>
<% end %>
Result: Error: undefined method `id' for []:Array 结果:错误:[]:Array的未定义方法“ id”
FAIL 2 失败2
It is an ActiveRecord relationship so apparently I do not need getter/setter methods. 这是一个ActiveRecord关系,因此显然我不需要getter / setter方法。
<%= f.fields_for :conversation do |ff| %>
<% @missives = [] %>
<%= ff.fields_for :missives, @missives do |fff| %>
<%= fff.text_area :content %>
...
<% end %>
<% end %>
Result: Error (undefined method `content' for []:Array) 结果:错误([]:Array的未定义方法“ content”)
FAIL 3 失败3
<%= f.fields_for :conversation do |ff| %>
<%= ff.fields_for :missives, @missives do |fff| %>
<%= fff.text_area :content %>
...
<% end %>
<% end %>
Result: does not put square brackets around missives. 结果:没有在方括号周围加上方括号。
FAIL 4 失败4
In the controller: 在控制器中:
@missives = [Postmissive.new]
In the post form: 以帖子形式:
<%= f.fields_for :conversation do |ff| %>
<%= ff.fields_for :missives, @missives do |fff| %>
<%= fff.text_area :content %>
...
<% end %>
<% end %>
Result: Error (undefined method `content' for Array). 结果:错误(数组的未定义方法“ content”)。
Current state of code: 代码的当前状态:
post.rb post.rb
has_one :conversation, class_name: 'Postconversation', dependent: :destroy
accepts_nested_attributes_for :conversation
postconversation.rb postconversation.rb
has_many :missives, class_name: 'Postmissive', dependent: :destroy
accepts_nested_attributes_for :missives
postmissive.rb postmissive.rb
belongs_to :conversation, class_name: 'Postconversation', foreign_key: 'conversation_id'
validates :content, presence: true
posts_controller.rb 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 _post_form.html.erb
<%= f.fields_for :conversation do |ff| %>
<%= ff.fields_for :missives do |fff| %>
<%= fff.text_area :content %>
...
<% end %>
<% end %>
The answer does not target success/failure of the form or what happens after the form is submitted. 答案不以表格的成功/失败或提交表格后的结果为目标。
It just targets sending missives
as an array. 它仅选择发送
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 %>
Please note that Postconversation.new
and Postmissive.new
are just added to make the code work. 请注意,只是添加了
Postconversation.new
和Postmissive.new
以使代码正常工作。 You HAVE to modify it as per your application logic. 您必须根据您的应用程序逻辑对其进行修改。
The params output: 参数输出:
{"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"}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.