Rails формируют вложенные атрибуты, недопустимый параметр - PullRequest
0 голосов
/ 30 мая 2018

Следующий код выдает эту ошибку:

Недопустимый параметр:: разговор

Не разрешено ли conversation_attributes in 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"}}}}

Ответы [ 2 ]

0 голосов
/ 04 июня 2018

Получил это работает.Надо было использовать 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"}]}}}
0 голосов
/ 30 мая 2018

Вы можете попробовать:

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

Обновление: попробуйте явно обернуть его хешем:

params.require(:post).permit({ conversation: [{ missives: [:content]}] })
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...