views:

520

answers:

1

Hey guys, I've been beating my head against a wall with a particular use case for nested forms (I'm using Rails 2.3.5).

Essentially I have Project and Payment models that looks like this

class Project < ActiveRecord::Base
  has_many :payments
end

class Payment < ActiveRecord::Base
  belongs_to :project
  accepts_nested_attributes_for :project
end

I'm also using nested routing for these two resources:

map.resources :projects do |project|
  project.resources :payments
end

I'm trying to use a nested form to allow the user to modify certain attributes of the Project when creating a new Payment. So if the Project has a title, for example, the view for creating a new Payment would look like this:

<% form_for([@project, @payment]) do |f| %>
  <% f.fields_for :project do |project_form| %>
     <%= project_form.label :title %>
     <%= project_form.text_field :title %>
  <% end %>

  <%= f.text_field :credit_card_number %>

  ...
<% end %>

The Payments controller is pretty much standard:

class PaymentsController < ApplicationController
  before_filter :load_project

  # GET /payments/new
  # GET /payments/new.xml
  def new
    @payment = @project.payments.build

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @payment }
    end
  end

  # POST /payments
  # POST /payments.xml
  def create
    @payment = @project.payments.build(params[:payment])

    respond_to do |format|
      if @payment.save
        flash[:notice] = 'Payment was successfully created.'
        format.html { redirect_to([@project, @payment]) }
        format.xml  { render :xml => @payment, :status => :created, :location => @payment }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @payment.errors, :status => :unprocessable_entity }
      end
    end
  end

  private
    def load_project
      @project = Project.find(params[:project_id])
    end
end

What I'm finding is that on the new Payment form, I'm ending up with something like this:

<input id="payment_project_attributes_title" name="payment[project_attributes][title]" size="30" type="text" />
<input id="payment_project_attributes_id" name="payment[project_attributes][id]" type="hidden" value="56" />

(Note the automatically created #payment_project_attributes_id)

When the form is submitted, rails receives this like so (bear in mind that Project #56 already exists):

"payment"=>{"project_attributes"=>{"title"=>"test title", "id"=>"56"}, "credit_card_number"=>"41111111111111111"}

And here's the problem: when this is run through the controller, it DOESN'T apply the title attribute to the Payment's Project.

What's strange is that if I remove that "id"=>"56", the Project's title IS updated. Here's an example using the console:

ruby-1.8.7-p249 >   Project.find(56)
 => #<Project id: 56, title: nil, created_at: "2010-06-18 15:58:25", updated_at: "2010-06-18 16:01:37"> 
ruby-1.8.7-p249 > p=Project.find(56).payments.new({"project_attributes"=>{"title"=>"my new title", "id"=>"56"}})
 => #<Payment id: nil, project_id: 56, created_at: nil, updated_at: nil> 
ruby-1.8.7-p249 > p.project
 => #<Project id: 56, title: nil, created_at: "2010-06-18 15:58:25", updated_at: "2010-06-18 16:01:37"> 
ruby-1.8.7-p249 > p=Project.find(56).payments.new({"project_attributes"=>{"title"=>"test title"}})
 => #<Payment id: nil, project_id: 56, created_at: nil, updated_at: nil> 
ruby-1.8.7-p249 > p.project
 => #<Project id: nil, user_id: nil, title: "test title", created_at: nil, updated_at: nil>

(Note that the second payments.new, without the ID, causes p.project.title to be updated)

This seems directly contradictory to this ticket: https://rails.lighthouseapp.com/projects/8994/tickets/3687-nested-attributes-with-belongs_to-only-supports-one-use-case

Does anybody have any thoughts?

I should note that what I'm really trying to do is one layer more complex--I'm trying to update user_attributes for Project (using a belongs_to :user/accepts_nested_attributes_for :user on the Project) but I'm sort of hoping that will just work if I can figure this out.

+1  A: 

It seems that since you're calling the build method based on the association of @project.payments it already knows what the project id is. Maybe it doesnt like the fact that youre trying to update the project id?

Mike
Also, i think your form for should just be for payment <% form_for @payment do |f| %>and then your fields for should be fine.
Mike