Unpermitted parameters on with accepts_nested_attributes_for

nested-attributes, ruby, ruby-on-rails, ruby-on-rails-4

Solution

Ok, with some help from #RubyOnRails I have got it figured out.

I needed to build the artists in the new action of BandController

@band.artists.build

That then gets pulled through into the view via `form_for(@band)`, with `.fields_for :artist` changing to `.fields_for :artists`, and

  params.require(:band).permit(:name, :artist, artist_attributes: [ :band_id, :first_name ])

changing to

  params.require(:band).permit(:name, artists_attributes: [ :band_id, :first_name ])

Problem

So I'm fairly new to Rails so I'm probably missing something quite straight forward.. What I'm trying to do is create an `Artist` when creating a `Band`. Models band.rb ``` class Band < ActiveRecord::Base has_many :artists, dependent: :destroy accepts_nested_attributes_for :artists, allow_destroy: true ... end ``` artist.rb ``` class Artist < ActiveRecord::Base belongs_to :band ... end ``` Controller band_controller.rb ``` class BandController < ApplicationController def new @band = Band.new end def create @band = Band.new(band_params) if @band.save ... else ... end end private def band_params params.require(:band).permit(:name, :hometown, :email, :artist, artist_attributes: [ :band_id, :first_name, :last_name, :email, :password, :password_confirmation ]) end end ``` View new.html.erb ``` <%= form_for(@band, url: "/artist/signup") do |f| %> <%= render 'shared/error_messages', object: f.object %> <%= f.fields_for :artist do |artist| %> <%= artist.text_field :first_name, :placeholder => "First Name", :maxlength => 100 %> <%= artist.text_field :last_name, :placeholder => "Last Name", :maxlength => 100 %> <%= artist.email_field :email, :placeholder => "Email Address", :maxlength => 255 %> <%= artist.password_field :password, :placeholder => "Password", :maxlength => 255 %> <%= artist.password_field :password_confirmation, :placeholder => "Confirm Password", :maxlength => 255 %> <% end %> <%= f.text_field :name, :placeholder => "Band Name", :maxlength => 100 %> <%= f.text_field :hometown, :placeholder => "Hometown", :maxlength => 100 %> <%= f.text_field :email, :placeholder => "Band Email", :maxlength => 100 %> <%= f.submit "Apply", class: "button" %> <% end %> ``` My development log is writing out: ``` Parameters: {"utf8"=>"✓", "authenticity_token"=>"...", "band"=>{"artist"=>{"first_name"=>"", "last_name"=>"", "email"=>"", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "name"=>"", "hometown"=>"", "email"=>""}, "commit"=>"Apply"} ----- Unpermitted parameters: artist {"name"=>"", "hometown"=>"", "email"=>""} ----- ``` Now, I thought that I wouldn't need to permit artist due to it being an hash.. Even if I do permit it, it doesn't change.. Any ideas?

Original source