为什么我收到此错误?:
TypeError(没有将Symbol隐式转换为整数)
在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"} ] } ] }}
但是我如何在帖子中实现这个?
答案 0 :(得分:1)
由于missives_attributes
包含content
您使用<%= fff.text_area :content %>
代表missives_attributes
而非<%= f.text_area :content %>
<%= 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 %>
答案 1 :(得分:1)
我终于明白了:
<强> 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, inverse_of: :post
accepts_nested_attributes_for :conversation
<强> postconversation.rb 强>
belongs_to :post, inverse_of: :conversation
has_many :missives, class_name: 'Postmissive', dependent: :destroy, foreign_key: 'conversation_id', inverse_of: :conversation
accepts_nested_attributes_for :missives
<强> postmissive.rb 强>
belongs_to :conversation, class_name: 'Postconversation', foreign_key: 'conversation_id', inverse_of: :missives
validates :conversation, presence: true # validates :conversation_id does not work
<强> _post_form.html.erb 强>
<%= f.fields_for :conversation_attributes do |ff| %>
<%= ff.fields_for 'missives_attributes[]', Postmissive.new 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"}]}}}
备注强>
1)注意添加inverse_of:
,以便Rails识别双向关联。没有这个,数据库保存失败。文档说它只需要与has_one
和has_many
一起使用,但是一些Stack Overflow答案说它也应该与belongs_to
一起使用,这样就可以了我做了什么(belongs_to :post, inverse_of: :conversation
和belongs_to :conversation, inverse_of: :missives
)。进一步了解这一欢迎。
2)post_params
很好。无需添加任何括号。
3)在postmissive.rb
中,验证必须是validates :conversation
,而不是validates :conversation_id
。
4)Stephan Wiehr是对的,missives_attributes
必须作为数组提交。感谢Kartikey Tanna关于如何实现这一目标的this答案。