具有嵌套资源的 Rails 嵌套表单:创建新的 has_many 时更新 own_to 关联
我一直在为嵌套表单的特定用例而苦恼(我正在使用 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 %>
付款控制器几乎是标准的:
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"}
这就是问题:当它通过控制器运行时,它确实如此'T 将标题属性应用于付款项目。
奇怪的是,如果我删除 "id"=>"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 被更新)
有人有什么想法吗?
我应该注意到,我真正想做的是更复杂的一层 - 我正在尝试更新项目的 user_attributes (在项目上使用 own_to :user/accepts_nested_attributes_for :user ),但我有点希望如果我能解决这个问题,就会起作用。
I have 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看来,由于您是根据 @project. payments 的关联调用构建方法,所以它已经知道项目 ID 是什么。也许它不喜欢您尝试更新项目 ID 的事实?
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?