vendredi 31 juillet 2015

Rails correct way to submit nested parameters in form

This code successfully creates a new Notice, as intended:

In the submit form:

<%= hidden_field_tag "notice[supernotice][commentee_id]", notice.id %>

In the 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

Class 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

However, the logs show the error: Unpermitted parameter: supernotice. How do I get rid of this error? What is wrong with the way I am submitting the nested parameter?

Aucun commentaire:

Enregistrer un commentaire