Jump to content

The ultimate community for Ruby on Rails developers.


Photo

Nested form not saving user_id despite proper setup


  • Please log in to reply
2 replies to this topic

#1 klyrish

klyrish

    Signalman

  • Members
  • 10 posts
  • LocationDenver, CO

Posted 17 September 2013 - 10:04 PM

I'm building a site that has Payouts. Payouts have only one attribute: year.

 

However, there are many awards per Payout. Awards belong to a Payout and a User. Users and Payouts both have many awards.

 

I'm not having any issues getting the awards associated with the Payout they're added to, however, I cannot for the life of me get the user_id set for an award when using the nested form for Awards on the Payout form. I used Railscast #196 revised as my starting point and my site is running Rails 3.2. I'm at a complete loss of what to do because as far as I can tell, I'm doing everything right.

 

I'm guessing it's something stupid/little I'm overlooking so any help would be appreciated. 

class Award < ActiveRecord::Base
  attr_accessible :amount, :name, :user_id, :awards_attributes, :users_attributes
  
  belongs_to :user
  belongs_to :payout
  
end

class Payout < ActiveRecord::Base
  attr_accessible :year, :awards_attributes, :users_attributes
  
  has_many :awards
  
  accepts_nested_attributes_for :awards,
                                :reject_if => :all_blank,
                                :allow_destroy => true
end

class User < ActiveRecord::Base
  authenticates_with_sorcery!

  attr_accessible :email, :password, :password_confirmation, :is_admin, :display_name, :awards_attributes

  validates_confirmation_of :password
  validates_presence_of :password, :on => :create
  validates_presence_of :email
  validates_presence_of :display_name
  validates_uniqueness_of :email
  
  has_many :awards

end

Payouts form:

<div class="control-group">
		<%= f.label :year %>
		<div class="controls">
			<%= f.number_field :year %>
		</div>
	  </div>
	
	 <div class="control-group">
		<% f.fields_for :awards do |builder| %>
	  		<%= render 'award_fields', f: builder %>
	 	<%end %>	
	 </div>
		
	<div class="control-group">
		<div class="controls">
			<%= link_to_add_fields "Add award", f, :awards %>
		</div>
	</div>
	  <div class="actions">
	    <%= f.submit %>
	  </div>
	<% end %>
	</div>

And finally, the _awards_fields partial

<fieldset>
	<div class="control-group">
		<%= f.label :name, "Award" %>
		<div class="controls">
			<%= f.text_field :name %>
		</div>
	</div>

	<div class="control-group">
		<%= f.label :amount %>
		<div class="controls">
			<%= f.number_field :amount %>
		</div>
	</div>

	<div class="control-group">
		<%= f.label :user_id, "Winner" %>
		<div class="controls">
			<%= collection_select(:award, :user_id, User.all, :id, :display_name) %>
		</div>
	</div>
	
	<div class="control-group">
		<div class="controls">
			<label class="checkbox">
				<%= f.check_box :_destroy %> Remove payout
			</label>
		</div>
	</div>
</fieldset>


Adding an award from the /awards/new action works fine and associates the chosen User record no problem. However, when submitting from the Payouts new/edit form, the user_id is not inserted/updated ever and results in errors when I attempt to call the award.user.display_name field. I have no idea what I'm doing wrong because as far as I can tell, I've set this up correctly. Awards are being associated with payouts so that's fine...I just can't figure out how to get the user_id field on the award set when creating an award through the Payout's nested Award form.

 

Any help/insight would be appreciated.



#2 klyrish

klyrish

    Signalman

  • Members
  • 10 posts
  • LocationDenver, CO

Posted 17 September 2013 - 11:13 PM

So I looked closer at the API and updated the collection_select to:

<%= f.collection_select(:user_id, User.all, :id, :display_name) %>

And it seems to be working just fine now.

 

However, when I go to edit a Payout, none of the associated Awards are displayed on the form. Again...I'm not sure what I'm doing wrong as I followed the Railscast and even went back to a 2.3 app I built years ago to see if I forgot something and, as far as I can tell, have not. :-/



#3 klyrish

klyrish

    Signalman

  • Members
  • 10 posts
  • LocationDenver, CO

Posted 17 September 2013 - 11:42 PM   Best Answer

Aaaaaaaaaand I'm an idiot. I didn't have the = for the form fields. Added that and we're set.

 

Thanks for letting me type all this out so I could figure it out on my own haha  :P


  • Jamie likes this




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users