2

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?

2 Answers 2

7

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 ])
Sign up to request clarification or add additional context in comments.

1 Comment

It would be nice if you could edit out some fields to make the question and answer easier to read. For example, you don't need to show all attributes of artists.
0

Make you sure that create a build for your band model into new action, following this:

class BandController < ApplicationController
  def new
    @band = Band.new
    @band.artists.build
  end
end

You might need more nested artists attributes for one band, than you can:

class BandController < ApplicationController
  def new
    @band = Band.new
    3.times{@band.artists.build}
  end
end

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.