为什么会出现此错误?:
TypeError (不隐式将符号转换为整数)
在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"} ] } ] }}
但是我如何在post表单中实现这一点呢?
发布于 2018-05-30 21:53:48
因为missives_attributes
包含content
对于missives_attributes
,您应该使用<%= fff.text_area :content %>
,而不是<%= 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 %>
发布于 2018-06-04 05:17:08
我终于让它工作了:
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)斯蒂芬·维尔是对的,missives_attributes
必须以数组的形式提交。感谢Kartikey Tanna为this解答了如何实现这一点。
https://stackoverflow.com/questions/50606032
复制相似问题