I'm working on an app resembles a library booking app. I'm stuck at building a nested form for adding a new book. I got the form working for one-to-many relationships (images for a book etc) but I also want to CHOOSE and tie an existing author to that book (A book has many Authors and an Author has many Books through the contributions table) with a collection_select.
When submitting the form, it doesn't make the entry in the join table with the book_id and author_id. (I managed to get it working with simple text_entry fields when creating a new author, but the collection_select just doesn't work. Before adding reject_if to the accepts_nested_attributes, it kept creating a new author with blank first_name and last_name and making an entry in the join table with this blank new author)
Here's my form stripped down to the important parts
<%= form_for @book do |f| %>
<div class="panel panel-default">
<div class="panel-body">
<%= f.label :name, "Book title" %>
<%= f.text_field :name %>
<%= f.label :description %>
<%= f.text_area :description, rows: 5 %>
<%= f.label :year_of_publication %>
<%= f.text_field :year_of_publication %>
</div>
</div>
<div class="panel panel-default">
<div class="panel-body">
<h3>Add authors</h3>
<h4>Choose from existing authors</h4>
<%= f.fields_for :authors do |builder| %>
<%= builder.collection_select( :id, Author.all, :id, :full_name, prompt: "Select from existing authors") %>
<% end %>
</div>
</div>
<%= f.submit "Submit", class: "btn btn-info" %>
<% end %>
And this is the HTML currently rendered for the collection_select
<select id="book_authors_attributes_0_id" name="book[authors_attributes][0][id]"><option value="">Select from existing authors</option>
<option value="1">Berman, Jules J.</option>
<option value="2">Writerton, Andy R.</option>
<option value="3">Goldner, Merle</option>
<option value="4">Auer, Cordell</option>
<option value="5">Metz, Dewitt</option>
<option value="6">Leffler, Briana</option>
<option value="7">Trantow, Audra</option>
<option value="8">Murazik, Ebony</option>
<option value="9">Bahringer, Cale</option>
<option value="10">Schmitt, Wiley</option>
<option value="11">Casper, Zoe</option>
Here's my Book model.
class Book < ActiveRecord::Base
# default_scope -> { order('name ASC') }
validates :year_of_publication, presence: true
validates :description, presence: true
validates :name, presence: true
has_many :stock_items, dependent: :destroy
has_many :libraries, through: :stock_items
has_many :contributions, dependent: :destroy
has_many :authors, through: :contributions
has_many :bookings, through: :stock_items
has_many :book_images, dependent: :destroy
accepts_nested_attributes_for :book_images
accepts_nested_attributes_for :authors, :allow_destroy => true, :reject_if => proc {|attributes| attributes['last_name'].blank? }
accepts_nested_attributes_for :libraries
accepts_nested_attributes_for :stock_items
accepts_nested_attributes_for :contributions
validates :name, presence: true
# validate year of pub length to 4
end
And my Author model.
class Author < ActiveRecord::Base
validates :first_name, presence: true
validates :last_name, presence: true
has_many :contributions, dependent: :destroy
has_many :books, through: :contributions
def full_name
"#{last_name}, #{first_name}"
end
end
The Contribution model (join table)
class Contribution < ActiveRecord::Base
belongs_to :author
belongs_to :book
validates :author_id, presence: true
validates :book_id, presence: true
end
Relevant parts of the schema
create_table "authors", force: true do |t|
t.string "last_name"
t.string "first_name"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "contributions", force: true do |t|
t.integer "book_id"
t.integer "author_id"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "books", force: true do |t|
t.string "name"
t.datetime "created_at"
t.datetime "updated_at"
t.string "year_of_publication"
t.string "description"
end
My new and create actions in the BooksController
def new
@book = Book.new
# Displays empty fields in the new book action view (n.times)
1.times { @book.authors.build }
1.times { @book.book_images.build }
@book.stock_items.build
end
def create
@book = Book.new(book_params)
if @book.save
redirect_to @book
flash[:success] = "Book added"
else
render 'new'
flash.now[:danger] = "Book NOT added"
end
end
My book_params
private
def book_params
# Include the nested parameters in the strong parameters as model_name_attributes !!!!!!!!!!
params.require(:book).permit( :name,
:description,
:year_of_publication,
authors_attributes: [ :first_name, :last_name ],
book_images_attributes: [ :image_url, :book_id ],
libraries_attributes: [ :name ],
stock_item_attributes: [ :book_id, :library_id ],
contribution_attributes: [ :book_id, :author_id ])
end
If i understood correctly,You should make use of your join_table contributions
.Changing your fields_for
part to this will make it work
<div class="panel panel-default">
<div class="panel-body">
<h3>Add authors</h3>
<h4>Choose from existing authors</h4>
<%= f.fields_for :contributions do |builder| %>
<%= builder.collection_select(:author_id, Author.all, :id, :full_name, prompt: "Select from existing authors") %>
<% end %>
</div>
</div>