Rails 4, Strong Parameters, and Nested Forms
| Tagged:
I recently started building a Rails 4 app and ran into some hangups when using strong parameters with nested models and forms. It seems to be lacking documentation for this specific scenario, so I wanted to share my findings.
In a scenario where you have an album that has_many
songs, and you want to be able to edit both with the same form, you need to add every nested attribute that you plan to pass through to the params.permit()
. This is the setup that ended up working for me:
ruby
= form_for @album do |f|
= f.text_field :title
= f.fields_for :songs do |s|
= s.text_field :name
…
ruby
class Album < ActiveRecord::Base
:songs
has_many :songs, allow_destroy: true
accepts_nested_attributes_for
…end
ruby
class AlbumsController < ApplicationController
…private
def album_params
:album).permit(:title, songs_attributes: [:id, :_destroy, :name])
params.require(end
end
ruby
class Song < ActiveRecord::Base
:album
belongs_to
…end
Do you know of a better or easier way? Let me know!