0

the Event does get saved, but with "artists":[] blank, even though I selected multiple artists in the new form. the relevant part of the new form looks like this:

  <%= f.fields_for :event_artists do |fea| %>
    <%= fea.collection_select :artist_id, Artist.all, "id", "name", {include_blank: true}, {multiple: true} %>
  <% end %>

the log:

  Parameters: {"utf8"=>"✓", "authenticity_token"=>"OEh0j/cp35s/FABhsETxeQKnqZCKXrbZMpeeEE6P+KSM3QVF94zIluB1rqAD65ci5CP+R6tQS8V1f3SXIQ6Vtw==", "event"=>{"name"=>"", "date(1i)"=>"2016", "date(2i)"=>"7", "date(3i)"=>"22", "date(4i)"=>"01", "date(5i)"=>"55", "description"=>"", "venue_id"=>"1", "event_artists_attributes"=>{"0"=>{"artist_id"=>["", "1", "2"]}}}, "commit"=>"Create"}
Unpermitted parameter: artist_id

here is this parameter is permitted in the controller

def event_params
  params.require(:event).permit(:id, :name, :date, :venue_id, :description, { event_artists_attributes: [:artist_id] })
end

event_artist model looks like this:

class EventArtist < ApplicationRecord
  belongs_to :event, optional: true
  belongs_to :artist
end

event model:

class Event < ApplicationRecord
  belongs_to :venue
  has_many :event_artists
  has_many :artists, through: :event_artists
  accepts_nested_attributes_for :event_artists, reject_if: :all_blank, allow_destroy: true

end

event controller:

  def create
    @event = Event.new(event_params)

    if @event.save
      render json: @event, status: :created, location: @event
    else
      render json: @event.errors, status: :unprocessable_entity
    end
  end

  def new
    @event = Event.new
    @artist = @event.event_artists.build
  end

2 Answers 2

1

You have event_artists_attributes inside of an unnecessary hash. Instead, use:

  params.require(:event).permit(:id, :name, :date, :venue_id, :description, event_artists_attributes: [artist_id])

But you have another problem. You're trying to set multiple artist ids on an EventArtist that belongs_to only one Artist. Because you have a has_many artists, through: :event_artists on your Event model, you can change the following:

Controller:

def event_params
  params.require(:event).permit(:id, :name, :date, 
                                :venue_id, :description, artist_ids: [])
end

And in the form, remove the <%= fields_for… block and replace it with:

<%= f.collection_select :artist_ids, Artist.all, "id", "name", 
                          {include_blank: true}, {multiple: true} %>
Sign up to request clarification or add additional context in comments.

8 Comments

Can you dump your full params hash?
Can you update the post with your models? Is there an EventArtist join model that belongs_to :artist, and belongs_to :event? Can you include the accepts_nested_attribtues_for?
yep, check the post
Updated answer to use Event#artist_ids = […] instead of accepts_nested_attributes.
Unpermitted parameter: artist_ids, I need to get rid of the accepts_nested_attributes line completely? the result is the same whether with or without it.
|
0

Try this:

  params.require(:event).permit(:id, :name, :date, :venue_id, :description, { event_artists_attributes: [artist_id: []] })

2 Comments

not working. in the browser: {"event_artists.artist":["must exist"]}, in the console (0.2ms) ROLLBACK [active_model_serializers] Rendered ActiveModel::Serializer::Null with ActiveModel::Errors (0.35ms) Completed 422 Unprocessable Entity
That would be due to your blank element in the id array. You should remove {include_blank: true} in your collection select.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.