I am following the first part of Ryan Bate's episode #285. Not sure why it is not working. Here is the code:
models:
class Comic < ActiveRecord::Base
has_many :comics_genres
has_many :genres, through: :comics_genres
end
class ComicsGenre < ActiveRecord::Base
belongs_to :genre
belongs_to :comic
end
class Genre < ActiveRecord::Base
has_many :comic_genres
has_many :comics, through: :comics_genre
end
form for creating new comics:
<%= form_for ([@user, @comic]) do |f| %>
<div><%= f.collection_select :genre_ids, Genre.order(:genre), :id, :genre, {}, {multiple: true} %></div>
<%= f.submit class: "btn btn-primary" %>
<% end %>
Comic controller:
def create
@user = current_user
@comic = @user.comics.new(comic_params)
respond_to do |format|
if @comic.save
format.html { redirect_to @comic, notice: 'Comic was successfully created.' }
format.json { render action: 'show', status: :created, location: @comic }
else
format.html { render action: 'new' }
format.json { render json: @comic.errors, status: :unprocessable_entity }
end
end
end
def comic_params
params.require(:comic).permit(:id, :title, :synopsis,
comic_pages_attributes: [:comic_page_image],
comics_genres_attributes: [:genre_id, :comic_id])
end
In the console, I get records like this:
The issue is that genre_id is nil, but I am not sure how to get it to pass the right values.
Many thanks!