【问题标题】:Rails form nested attributes unpermitted parameterRails 表单嵌套属性不允许参数
【发布时间】:2018-05-30 18:25:06
【问题描述】:

以下代码抛出此错误:

不允许的参数::conversation

在post_params 中有conversation_attributes 是否不允许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"}}}}

【问题讨论】:

    标签: ruby-on-rails


    【解决方案1】:

    你可以试试:

    def post_params
      params.require(:post).permit(conversation: [missives: [:content]])
    end
    

    更新: 尝试用哈希显式包装它:

    params.require(:post).permit({ conversation: [{ missives: [:content]}] })
    

    【讨论】:

    • 这会导致这个错误:ActiveRecord::AssociationTypeMismatch (Postconversation expected, got {"missives"=>{"content"=>"Hello"}} 这是 ActiveSupport::HashWithIndifferentAccess 的一个实例) :
    • 尝试用哈希显式包装它。
    【解决方案2】:

    搞定了。必须使用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"}]}}}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多