Rails: accepts_nested_attributes_for with "strong parameters" requiring identifiers in parameters

Suppose the model Book

, with:

class Book < ActiveRecord::Base
  has_many: pages
  accepts_nested_attributes_for :pages

      

To UPDATE a record Page

instead of CREATING a new one, Rails requires the nested model ID (here:) Page

to resolve in "strong parameters" (instance Book

):

def update
  @book.update(book_params)
end

private

  def book_params()
    params.require(:book).permit(:title, :author, :published,
      pages_attributes: [:id, :word_count, :line_count]) #  <= this ID in here
  end

      

But does the inclusion ID

of "strong parameters" mean their goal? (Meaning: The user may submit false ID

, causing an invalid connection.)

+3


source to share


1 answer


Rails makes sure that the page you are trying to update belongs to the book. If you tried to change the page id on the form, you will get an error:



Could not find page with ID = 3 for book with ID = 1

+3


source







All Articles