Этот код успешно создает новое уведомление, как задумано:Rails правильный способ представить вложенные параметры в виде
В форме представить:
<%= form_tag({controller: "notices", action: "create"}, method: "post", class: "comment_form", html: { multipart: true }) do %>
.
<%= hidden_field_tag "notice[supernotice][commentee_id]", notice.id %>
.
<% end %>
В notices_controller.rb:
def create
@character = Character.find_by(callsign: params[:callsign])
@notice = @character.notices.build(notice_params)
if @notice.save
if !params[:notice][:supernotice][:commentee_id].nil?
@notice.create_comment(params[:notice][:supernotice][:commentee_id]) # hits the create_comment method in notice.rb
end
end
def notice_params
params.require(:notice).permit(:content, :picture, :latitude, :longitude, supernotice_attributes: [:commentee_id])
end
Класс notice.rb:
has_one :supernotice, through: :active_comment_relationship, source: :commentee
accepts_nested_attributes_for :supernotice
def create_comment(other_notice_id)
create_active_comment_relationship(commentee_id: other_notice_id)
end
Однако в журналах отображается ошибка: Unpermitted parameter: supernotice
. Как я могу избавиться от этой ошибки? Что не так с тем, как я подаю вложенный параметр?
Попробуйте изменить разрешение от 'supernotice_attributes' до' supernotices_attributes'. Кроме того, вы должны получить доступ к 'notice_params' в своем контроллере и не использовать' params' напрямую. –
Использование 'supernotices_attributes' не имеет никакого отношения к сожалению. – Bazley