【发布时间】:2018-06-04 21:10:25
【问题描述】:
为什么会出现这个错误?:
TypeError(没有将 Symbol 隐式转换为 Integer)
在posts_controller 的create 操作中:
posts_controller.rb
@post = @character.posts.create(post_params)
...
def post_params
params.require(:post).permit( conversation_attributes: [ missives_attributes: [ :content ] ] )
end
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
_post_form.html.erb
<%= f.fields_for :conversation_attributes do |ff| %>
<%= ff.fields_for :missives_attributes do |fff| %>
<%= hidden_field_tag :callsign, character.callsign %>
<%= fff.text_area :content %>
...
<% end %>
<% end %>
日志
Parameters: {"utf8"=>"✓", "authenticity_token"=>"mxHD...VoA==", "callsign"=>"baz", "post"=>{"conversation_attributes"=>{"missives_attributes"=>{"content"=>"Hello"}}}}
编辑
根据这个问题,看起来我需要在我提交的参数周围加上方括号: Strong parameters with has_many gives "no implicit conversion of Symbol into Integer"
Parameters: {"utf8"=>"✓", "authenticity_token"=>"mxHD...VoA==", "callsign"=>"baz", "post"=>{"conversation_attributes"=> [ {"missives_attributes"=> [ {"content"=>"Hello"} ] } ] }}
但是我如何在帖子表单中实现这一点??
【问题讨论】:
-
这个
<%= f.text_area :content %>不应该是<%= fff.text_area :content %> -
只是猜测:
missives是一个has_many关系,因此在您的参数中它被视为一个数组,:content作为数组索引(它应该是一个整数而不是符号)。 -
不应该是
f.fields_for :conversation和ff.fields_for :missives,没有_attributes吗? -
这就是我最初的想法,但其他 SO 的答案似乎并非如此。没有
_attributes,我得到这个错误:Unpermitted parameter :conversation -
您的 form_for(或 form_with)中有什么?您是为@post 定义它还是为 :post 定义它?
标签: ruby-on-rails