Answer the question
In order to leave comments, you need to log in
How to correctly style the Create method and save objects in the database?
There is a book, in each book there are pages, there are illustrations in the page. The connection is appropriate.
When creating new illustrations on a page, only the page id is stored in the database.
How can I save also the id of the book?
def create
@book = Book.friendly.find(params[:book_id])
@page = @book.pages.friendly.find(params[:page_id])
@picture = @page.images.new(image_params)
if @picture.save
...
end
end
private
def image_params
params.require(:picture).permit(:book_id, :picture)
end
Answer the question
In order to leave comments, you need to log in
Does picture have a book_id field?
@picture.book = Does @book fire?
Update: Ah, well, it looks like the problem is that you have a string :book_id, and you need to write an integer in the field. Alternatively, you can find a book in the image_params method and add it to the parameters. I don't know if this is a good practice.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question