Rails嵌套表单与嵌套资源:创建新的has_many时更新belongs_to关联

时间:2010-06-18 16:42:55

标签: ruby-on-rails nested-forms nested-resources

嘿伙计们,我一直在用一个特殊的嵌套表格用例(我正在使用Rails 2.3.5)撞墙。

基本上我有项目和付款模式,看起来像这样

class Project < ActiveRecord::Base
  has_many :payments
end

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

我也在使用这两种资源的嵌套路由:

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

我正在尝试使用嵌套表单,以允许用户在创建新付款时修改项目的某些属性。因此,如果项目有标题,那么创建新付款的视图将如下所示:

<% 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 %>

Payments控制器非常标准:

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

我所发现的是,在新的付款表格中,我最终会得到这样的结果:

<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" />

(注意自动创建的#payment_project_attributes_id)

提交表单时,rails会这样接收(请记住项目#56已经存在):

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

这就是问题所在:当它通过控制器运行时,它不会将title属性应用于Payment的项目。

奇怪的是,如果我删除“id”=&gt;“56”,项目的标题会更新。以下是使用控制台的示例:

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>

(请注意,第二次payment.new,没有ID,会导致p.project.title更新)

这似乎与此票证直接相矛盾:https://rails.lighthouseapp.com/projects/8994/tickets/3687-nested-attributes-with-belongs_to-only-supports-one-use-case

有人有任何想法吗?

我应该注意到,我真正想做的是一层更复杂 - 我正在尝试更新Project的user_attributes(使用项目中的belongs_to:user / accepts_nested_attributes_for:user)但我排序希望只有工作才能解决这个问题。

1 个答案:

答案 0 :(得分:1)

似乎因为你基于@project.payments的关联调用构建方法,所以它已经知道项目id是什么。也许它不喜欢您尝试更新项目ID的事实?