如何通过 fields_for 传递选项?
我正在尝试运行这个..
- f.fields_for :referrals do |qf|
但我也想传递这个 @organization.referrals.select{|ref|ref.new_record?}
。这样传递的形式都是新对象,而不是旧对象。
我尝试这样做......
- f.fields_for :referrals do |qf|
- if qf.object.new_record?
= render :partial => 'referral_fields', :locals => {:qf => qf}
这使得它在视图中正确显示,但参数仍然填充有每个先前创建的嵌套对象。
这让我相信我需要在 fields_for 语句本身中传递此选项。
我也尝试过这个:
- f.fields_for @organization.referrals.select{|ref|ref.new_record?} do |qf|
= render :partial => 'referral_fields', :locals => {:qf => qf}
以及这个:
- f.fields_for :referrals, @organization.referrals.select{|ref|ref.new_record?} do |qf|
= render :partial => 'referral_fields', :locals => {:qf => qf}
第一个示例将显示并且只允许传递一个对象。我的表单允许动态数量的重复嵌套表单。
第二个将显示并传递所有嵌套对象
应用程序信息
#organization.rb
has_many :referrals
accepts_nested_attributes_for :referrals, :reject_if => proc { |attributes| attributes.all? { |k, v| v.blank? } }, :allow_destroy => true
#referral.rb
belongs_to :organization
#referrals_controller.rb
def new
2.times { @organization.referrals.build }
....
def create
@referral = Referral.new(params[:referral])
if @referral.valid? && @organization.referrals << @referral
flash[:notice] = "Referrals saved."
redirect_to new_organization_referrals_path(@organization)
else
render :action => :new, :layout => 'manage'
end
end
I am trying to run this..
- f.fields_for :referrals do |qf|
But I would like to pass this, @organization.referrals.select{|ref|ref.new_record?}
as well. This is so that the forms passed are exclusively new objects, and not older ones.
I've tried to do this..
- f.fields_for :referrals do |qf|
- if qf.object.new_record?
= render :partial => 'referral_fields', :locals => {:qf => qf}
Which makes it display correctly in the view, but the params are still populated with every single previously created nested object.
Which leads me to believe that I need to pass this option within the fields_for statement itself.
I have also tried this :
- f.fields_for @organization.referrals.select{|ref|ref.new_record?} do |qf|
= render :partial => 'referral_fields', :locals => {:qf => qf}
As well as this :
- f.fields_for :referrals, @organization.referrals.select{|ref|ref.new_record?} do |qf|
= render :partial => 'referral_fields', :locals => {:qf => qf}
The first example will show and only allows to pass one object. Where as my form is to allow a dynamic number of duplicate nested forms.
The second will display and pass all nested objects
App Info
#organization.rb
has_many :referrals
accepts_nested_attributes_for :referrals, :reject_if => proc { |attributes| attributes.all? { |k, v| v.blank? } }, :allow_destroy => true
#referral.rb
belongs_to :organization
#referrals_controller.rb
def new
2.times { @organization.referrals.build }
....
def create
@referral = Referral.new(params[:referral])
if @referral.valid? && @organization.referrals << @referral
flash[:notice] = "Referrals saved."
redirect_to new_organization_referrals_path(@organization)
else
render :action => :new, :layout => 'manage'
end
end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这就是您想要的:
第一个参数是关联名称,rails 需要它才能知道如何构造参数。如果您的第一个参数是一个集合,rails 通常可以从该集合中推断出关联名称。
然而,您的集合已被过滤到常规数组中,无法轻松推断出其中的关联。因此,您将特定集合作为第二个参数传递。
祝你好运!
更新
我已经构建了一个小型 Rails 应用程序来分析问题,上面的解决方案对我来说效果很好 - 编辑表单不显示现有的推荐,只显示新的推荐。我将发布相关代码,以便我们可以了解您和我可能存在差异的地方。需要注意的是,这一切都在 erb 中,因为我很少使用 haml,并且不希望拼写错误搞乱解决方案:)
我的模型:
我的控制器的编辑操作:
我的观点:
当然,我刚刚阅读了您的新评论,并且也许你不再需要这个了。哦,好吧,为后代提供更多文档:)
Here's what you want:
The first parameter is the association name, which rails needs in order to know how to structure the params. If your first parameter is a collection, rails can usually infer the association name from that collection.
Your collection however, has been filtered into a regular array, where the association can't be as easily inferred. So you pass the specific collection as the second parameter.
Good luck!
UPDATE
I've built out a small rails app to analyze the problem, and the solution above is working just fine for me - the edit form doesn't display existing referrals, only new ones. I'll post the relevant code, so we can see where you and I might differ. One caveat, this is all in erb since I rarely work with haml and wouldn't want a typo to mess up the solution :)
My models:
My controller's edit action:
My views:
Of course, I just read your new comments, and maybe you don't need this anymore. Oh well, more documentation for posterity :)