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


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


But does the inclusion ID

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

, causing an invalid connection.)


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



All Articles