I have three models – a donar, a recipient and a gift,
The donor model – has_one :recipient, belongs_to :gift, accepts_nested_attributes_for, :recipient, accepts_nested_attributes_for :gift
The recipient model – belongs_to :donor
And the gift model – has_many :donors
When a user is on the show page – /donors/1 – I’d like to be able to edit the recipient and add a gift. They are editing all three models from one form.
My show action in the controller is providing a form to add a reciient to the donar on that form through @recipient = @donor.build_recipient – I set up the form in the view. This works!
however I’m trying to allow the user to attach a preexisting gift to a user through a select box using –
<%= select(:gift, :gift_id, Gift.all.collect {|p| [ p.name, p.id ] }, {:prompt => 'Select gift'}) %>
This is displaying a select box with all of the gifts listed from the table.
When I submit the form I can edit the donor’s details, add a recipient, but the gift model (or the donor’s relationship with a gift) is not updating at all. What am I doing wrong?
I believe you need something like that:
In that case there’s no need for
accepts_nested_attributes_for :giftas you are selecting gift from a list of available ones instead of creating it in the donor form.