Mam tabele books i authors w relacji wiele do wielu (tabela buforowa authorbooks). Wyświetla dane, jednak przy próbie wysłania formularza dane z list nie przechodzą walidacji.
formularz:
[code] <%= fields_for(:author_book) do |ab| %>
<%= collection_select(:authors, :id, Author.all, :id, :last_name, {}, {:multiple => true} ) %>
<%= fields_for(:category_book) do |cb| %>
<%= collection_select(:categories, :id, Category.all, :id, :name, {}, {:multiple => true} ) %>
respond_to do |format|
format.html # new.html.erb
format.json { render json: @book }
end
end
GET /books/1/edit
def edit
@book = Book.find(params[:id])
end
POST /books
POST /books.json
def create
@book = Book.new(params[:book])
params[:authors][:id].each do |author|
if !author.empty?
@book.authorbooks.build(:author_id => author)
end
end
params[:categories][:id].each do |category|
if !category.empty?
@book.categorybooks.build(:category_id => category)
end
end
respond_to do |format|
if @book.save
format.html { redirect_to @book, notice: 'Book was successfully created.' }
format.json { render json: @book, status: :created, location: @book }
else
format.html { render action: "new" }
format.json { render json: @book.errors, status: :unprocessable_entity }
end
end
end[/code]
log:
Processing by BooksController#create as HTML
Parameters: {"utf8"=>"âś“", "authenticity_token"=>"YySsW0VfFaqtawltpqbWA34MH5cN7XGOiJ//VDDZj5Q=", "book"=>{"title"=>"Zbrodnia i Kara", "description"=>"asd", "isbn"=>"hfgurenf", "year"=>"8"}, "authors"=>{"id"=>["", "2"]}, "categories"=>{"id"=>["", "1", "2"]}, "commit"=>"Create Book"}
pierwszy element tablicy jest zawsze pusty.
Dlaczego tak jest i co można z tym zrobić?