nested_form 的问题,模型关联的深度有几个层次
我在使用 nested_form
和 simple_form
插件创建可以与以下域模型正常工作的嵌套表单时遇到问题。我认为我错误地构建了表单或 attr_accessible 和accepts_nested_attributes_for 调用。
我有一个具有以下关联的域模型:
company.rb
class Company < ActiveRecord::Base
has_one :subscription
attr_accessible :name, :subscription_attributes, :cycles_attributes, :payments_attributes
accepts_nested_attributes_for :subscription
end
subscription.rb
class Subscription < ActiveRecord::Base
belongs_to: company
has_many :cycles
attr_accessible :company_id, :cycles_attributes, :payments_attributes
accepts_nested_attributes_for :cycles
end
Cycle.rb
class Cycle < ActiveRecord::Base
belongs_to :subscription
has_many :payments
attr_accessible :payments_attributes
accepts_nested_attributes_for :payments
end
payment.rb
class Payment < ActiveRecord::Base
belongs_to :cycle
end
我的视图如下:
<%= simple_nested_form_for company do |f| -%>
<%= f.input :name %>
<%= f.fields_for :subscription do |s| %>
#assorted fields for subscription
<%= s.fields_for :cycles do |c| %>
#assorted fields for cycle
<%= c.link_to_remove "[ - ] Erase Cycle" %>
<%= c.fields_for :payments do |p| %>
#assorted fields for payments
<%= p.link_to_remove "[ - ] Erase Payment" %>
<% end %>
<%= c.link_to_add "[ + ] New Payment", :payments %>
<% end %>
<%= s.link_to_add "[ + ] New Cycle", :cycles %>
<% end %>
公司控制器:
def new
@company = Company.new
@company.build_subscription
@categories = Category.find(:all)
respond_with @company
end
def create
@company = Company.new(params[:company])
if @company.save
flash[:notice] = "Success"
else
flash[:error] = "Error #{@company.errors.full_messages}"
end
respond_with @company
end
当然,我正在简化视图和内容,有一些验证和字段我没有表现出来,但这些都不是问题。 当我提交表单时,我收到一些奇怪的错误,基本上所有信息都在参数中传递,但它没有正确嵌套:/
Parameters: {"utf8"=>"✓", "authenticity_token"=>"57wgXJinL6kql0F9CxShKpf11RhdMfqXnb6y8K/pDg0=",
"company"=>{"name"=>"asdf12",
"subscription_attributes"=>{
"cycles_attributes"=>{
"0"=>{"plan_id"=>"1", "amount"=>"123", "months"=>"12", "_destroy"=>"false"}
},
"0"=>{
"0"=>{
"payments_attributes"=>{
"new_1329843584974"=>{"payment_type"=>"Efectivo", "amount"=>"123", "receipt_number"=>"1231", "note"=>"asdf asdf", "_destroy"=>"false"
}
}
}
}
}
}, "commit"=>"Save"}
WARNING: Can't mass-assign protected attributes: 0
错误是:
@messages={:"subscription.cycles.subscription"=>["can't be blank"],
:"subscription.cycles.payments"=>["can't be blank"]}
我的猜测是,属性传递给应用程序的方式出现了问题是当我点击新的付款按钮时,它是在表单“外部”创建的......有人遇到过类似的情况吗?
如果您需要更多信息,请告诉我。
I'm having trouble creating a nested form that will work correctly with the following domain model using the nested_form
and simple_form
plugins. I think I'm constructing the forms incorrectly or the attr_accessible and the accepts_nested_attributes_for calls.
I have this domain model with the following associations:
company.rb
class Company < ActiveRecord::Base
has_one :subscription
attr_accessible :name, :subscription_attributes, :cycles_attributes, :payments_attributes
accepts_nested_attributes_for :subscription
end
subscription.rb
class Subscription < ActiveRecord::Base
belongs_to: company
has_many :cycles
attr_accessible :company_id, :cycles_attributes, :payments_attributes
accepts_nested_attributes_for :cycles
end
cycle.rb
class Cycle < ActiveRecord::Base
belongs_to :subscription
has_many :payments
attr_accessible :payments_attributes
accepts_nested_attributes_for :payments
end
payment.rb
class Payment < ActiveRecord::Base
belongs_to :cycle
end
my view is the following:
<%= simple_nested_form_for company do |f| -%>
<%= f.input :name %>
<%= f.fields_for :subscription do |s| %>
#assorted fields for subscription
<%= s.fields_for :cycles do |c| %>
#assorted fields for cycle
<%= c.link_to_remove "[ - ] Erase Cycle" %>
<%= c.fields_for :payments do |p| %>
#assorted fields for payments
<%= p.link_to_remove "[ - ] Erase Payment" %>
<% end %>
<%= c.link_to_add "[ + ] New Payment", :payments %>
<% end %>
<%= s.link_to_add "[ + ] New Cycle", :cycles %>
<% end %>
the company controller:
def new
@company = Company.new
@company.build_subscription
@categories = Category.find(:all)
respond_with @company
end
def create
@company = Company.new(params[:company])
if @company.save
flash[:notice] = "Success"
else
flash[:error] = "Error #{@company.errors.full_messages}"
end
respond_with @company
end
Of course, I'm simplifying the views and stuff, there are some validations and fields I'm not showing, but those aren't the problem.
When I submit the form I'm getting some weird errors, basically all info is passed in the params but it's not correctly nested :/
Parameters: {"utf8"=>"✓", "authenticity_token"=>"57wgXJinL6kql0F9CxShKpf11RhdMfqXnb6y8K/pDg0=",
"company"=>{"name"=>"asdf12",
"subscription_attributes"=>{
"cycles_attributes"=>{
"0"=>{"plan_id"=>"1", "amount"=>"123", "months"=>"12", "_destroy"=>"false"}
},
"0"=>{
"0"=>{
"payments_attributes"=>{
"new_1329843584974"=>{"payment_type"=>"Efectivo", "amount"=>"123", "receipt_number"=>"1231", "note"=>"asdf asdf", "_destroy"=>"false"
}
}
}
}
}
}, "commit"=>"Save"}
WARNING: Can't mass-assign protected attributes: 0
The error is:
@messages={:"subscription.cycles.subscription"=>["can't be blank"],
:"subscription.cycles.payments"=>["can't be blank"]}
There is something going awry with how the attributes are being passed to the app, my guess is that when I click on the new payment button, it gets created "outside" the form... has anybody encountered something similar?
If you need any more info, let me know.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题出在nested_form插件本身。
当具有 has_one 关系并将嵌套模型作为其子项时,nested_form 在生成表单时遇到问题。
我检查了这个拉取请求,并使用更改
nested_form 存储库中的问题 #124
<一个href="https://github.com/kevinrood/nested_form/commit/7fda6884cef3c64b17a5d0114e40f2518ce9eed9" rel="nofollow">修复问题的补丁
此代码最终应合并到主存储库中。
The problem was with the nested_form plugin itself.
When having a has_one relationship and nested models as children of that, nested_form was having problems generating the forms.
I checked out this pull request and patched the corresponding file with the change
Issue #124 in the nested_form repo
the patch that fixes the issue
This code should be merged into the main repo eventually.