psql - Rails - Simple Form with Nested Attributes -
i trying make app rails 4 , simple form.
i have 3 models, being: project, scope , background.
project has 1 scope. scope has 1 background. scope belongs project. background belongs scope. project accepts nested attributes scope. scope accepts nested attributes background.
project.rb: has_one :scope accepts_nested_attributes_for :scope
scope.rb:
belongs_to :project accepts_nested_attributes_for :background
background.rb
belongs_to :scope
the scope params permitted in project controller. also, background attributes permitted inside project controller (as scope attributes).
the background params permitted in background controller.
the permitted params in each controller include relevant _id.
so in the:
- background controller, permitted params include :scope_id (not :background_id)
- scope controller, permitted params include :project_id
this because these foreign keys models belong model.
project controller:
def project_params params[:project].permit( :title, :project_id, scope_attributes: [:background, :project_id, background_attributes: [scope_id, title]])
scope controller:
def scope_params params[:scope].permit(:background, project_id, background_attributes: [scope_id, :title])
background controller:
params[:background].permit(:scope_id, :title)
my trouble understanding why, when edit project in field attribute of background, db records show creation of new id , new scope_id?
for example, when create new project, background table has id 1 , scope_id 4. when edit project , go in background table of psql database, id has become 2 , scope_id has become 5.
i expecting these ids remain orignally because have updated records (not created new ones).
am wrong expect this? or, have put wrong id fields in permitted params in controllers? or, wrong white-label each nested param background in each of project, scope , background controller?
your project_params
should this
def project_params params.require(:project).permit(:id,:title, scope_attributes: [:id,:background, :project_id, background_attributes: [:id,scope_id, title]]) end
Comments
Post a Comment