Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8228693
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T16:32:46+00:00 2026-06-07T16:32:46+00:00

I’ve currently got three models: Songs have many Setlists and vice versa, through the

  • 0

I’ve currently got three models: Songs have many Setlists and vice versa, through the Allocations model.

I’m trying to use a nested form to add existing songs to a setlist.

My current view for the nested form:

<div>
  <%=form_for @allocation do|builder|%>
    <%=builder.label :song_id, "Pick a song" %>

     <%= builder.hidden_field :setlist_id, value: @setlist.id %>

     <%= builder.select(:song_id, options_for_select(@selections), {}, {multiple: true, size: 7}) %>

    <%=builder.submit "Add Song", class: "btn btn-large btn-primary" %>
  <% end %>
</div>

and my controller for editing setlists:

  def edit
    @songs = Song.all(order: 'title')
    @setlist = Setlist.find(params[:id])
    @allocations = @setlist.allocations
    @allocation = Allocation.new
    @selections = Song.all.collect {|s| [ [s.title, s.artist].join(" by "), s.id ]   }
  end

  def update

    @setlist = Setlist.find(params[:id])
    @selections = Song.all.collect {|s| [ [s.title, s.artist].join(" by "), s.id] }
    @allocations = @setlist.allocations
    @allocation = Allocation.new

    params[:allocation][:song_id].reject! { |c| c.empty? }

    if @setlist.update_attributes(params[:setlist])
      if @allocation.save
        flash[:success] = "SETLIST SAVED!"
        redirect_to setlist_path(@setlist)
      else
        flash[:fail] = "Setlist not saved"
        render 'edit'
      end
    else
      flash[:fail] = "FAIL!"
      render 'edit'
    end
  end

Whenever I submit the form to add a song to the setlist I get an error back saying:

Validation failed: Setlist can't be blank, Song can't be blank

All the parameters appear to being passed correctly so I’m stumped. Here’s the parameters returned:

   {"utf8"=>"✓",
 "_method"=>"put",
 "authenticity_token"=>"ThIXkLeizRYtZW77ifHgmQ8+UmsGnDhdZ93RMIpppNg=",
 "setlist"=>{"date(1i)"=>"2012",
 "date(2i)"=>"7",
 "date(3i)"=>"11",
 "morning"=>"false"},
 "allocation"=>{"setlist_id"=>"1",
 "song_id"=>["5"]},
 "commit"=>"Add Song",
 "id"=>"1"}

Thank you for any help in advance

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T16:32:48+00:00Added an answer on June 7, 2026 at 4:32 pm

    You’re allowing multiple selections in the :song_id field, one option of which I imagine has a blank value. That option and one other must be getting selected, causing the ["", 13] response.

    params[:allocation][:song_id].reject! { |c| c.empty? }
    

    This will clean blank entries from that param. This should be placed in the update method anywhere before this line

    if @setlist.update_attributes(params[:setlist])
    

    As for the validation error, I assume it’s coming from the Allocation since that’s what the form is for.

    @allocation = Allocation.new
    
    if @allocation.save!
    

    Not knowing all of the attributes have attributes here that require values, like :set_list_id and :song_id. You’re trying to persist an Allocation to the database without first setting any of it’s attributes. This is the likely source of the validation issues you’re having.

    Edit:

    A nested form in rails is a set of form fields for an object associated with the object for the parent form. Notice how this form has fields_for called on the person_form object. This will result in nested parameters like param[:person][:children][:name].

    <% form_for @person do |person_form| %>
    
      <%= person_form.label :name %>
      <%= person_form.text_field :name %>
    
      <% person_form.fields_for :children do |child_form| %>
        <%= child_form.label :name %>
        <%= child_form.text_field :name %>
      <% end %>
    
      <%= submit_tag %>
    

    In the update method for this you could have something as simple as

    person = Person.find(params[:id]).update_attributes(params[:person])
    

    The update_attributes on Person can manage the creation, updating, and saving of it’s children association for you with accepts_nested_attributes_for

    I think this is what you’re after, and as such you may need to rethink your view and the update method accordingly. This is relatively tricky stuff for an absolute beginner to rails; just keep going back to the documentation (it’s very well written) and asking for help here.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to loop through a bunch of documents I have to put
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I am trying to understand how to use SyndicationItem to display feed which is
I have a jquery bug and I've been looking for hours now, I can't
Basically, what I'm trying to create is a page of div tags, each has
this is what i have right now Drawing an RSS feed into the php,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.