以下代码引发了此错误:
未经许可的参数::对话
conversation_attributes
中的post_params
是否允许conversations
?我做错了什么?
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 do |ff| %>
<%= ff.fields_for :missives 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"}}}}
答案 0 :(得分:0)
您可以尝试:
def post_params
params.require(:post).permit(conversation: [missives: [:content]])
end
更新: 尝试使用hash显式包装它:
params.require(:post).permit({ conversation: [{ missives: [:content]}] })
答案 1 :(得分:0)
搞定了。有必要使用f.fields_for :conversation_attributes
。
<强> 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"}]}}}