如何通过 fields_for 传递选项?

发布于 2024-09-28 23:03:56 字数 1587 浏览 3 评论 0原文

我正在尝试运行这个..

- 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

有木有妳兜一样 2024-10-05 23:03:56

这就是您想要的:

- f.fields_for :referrals, @organization.referrals.select{|ref|ref.new_record?} do |qf|
  = render :partial => 'referral_fields', :locals => {:qf => qf}

第一个参数是关联名称,rails 需要它才能知道如何构造参数。如果您的第一个参数是一个集合,rails 通常可以从该集合中推断出关联名称。

然而,您的集合已被过滤到常规数组中,无法轻松推断出其中的关联。因此,您将特定集合作为第二个参数传递。

祝你好运!

更新

我已经构建了一个小型 Rails 应用程序来分析问题,上面的解决方案对我来说效果很好 - 编辑表单不显示现有的推荐,只显示新的推荐。我将发布相关代码,以便我们可以了解您和我可能存在差异的地方。需要注意的是,这一切都在 erb 中,因为我很少使用 haml,并且不希望拼写错误搞乱解决方案:)

我的模型:

# app/models/organization.rb
class Organization < ActiveRecord::Base
  has_many :referrals
  accepts_nested_attributes_for :referrals
end

# app/models/referral.rb
class Referral < ActiveRecord::Base
  belongs_to :organization
end

我的控制器的编辑操作:

# app/controllers/organizations_controller.rb
class OrganizationsController < ApplicationController
  def edit
    @organization = Organization.find(params[:id])
    2.times { @organization.referrals.build }
  end
end

我的观点:

# app/views/organizations/edit.html.erb
<h1>Editing <%= @organization.name %></h1>

<% form_for(@organization) do |f| %>
  <%= f.error_messages %>

  <p>
    <%= f.label :name %><br />
    <%= f.text_field :name %>
  </p>

  <% f.fields_for :referrals, @organization.referrals.select{|ref| ref.new_record?} do |referral_fields| %>
    <%= render :partial => 'referral', :locals => {:f => referral_fields} %>
  <% end %>

  <p>
    <%= f.submit %>
  </p>
<% end %>

# app/views/organizations/_referral.html.erb
<p>
  <%= f.label :name, 'Referral Name' %><br />
  <%= f.text_field :name %>
</p>

当然,我刚刚阅读了您的新评论,并且也许你不再需要这个了。哦,好吧,为后代提供更多文档:)

Here's what you want:

- f.fields_for :referrals, @organization.referrals.select{|ref|ref.new_record?} do |qf|
  = render :partial => 'referral_fields', :locals => {:qf => qf}

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:

# app/models/organization.rb
class Organization < ActiveRecord::Base
  has_many :referrals
  accepts_nested_attributes_for :referrals
end

# app/models/referral.rb
class Referral < ActiveRecord::Base
  belongs_to :organization
end

My controller's edit action:

# app/controllers/organizations_controller.rb
class OrganizationsController < ApplicationController
  def edit
    @organization = Organization.find(params[:id])
    2.times { @organization.referrals.build }
  end
end

My views:

# app/views/organizations/edit.html.erb
<h1>Editing <%= @organization.name %></h1>

<% form_for(@organization) do |f| %>
  <%= f.error_messages %>

  <p>
    <%= f.label :name %><br />
    <%= f.text_field :name %>
  </p>

  <% f.fields_for :referrals, @organization.referrals.select{|ref| ref.new_record?} do |referral_fields| %>
    <%= render :partial => 'referral', :locals => {:f => referral_fields} %>
  <% end %>

  <p>
    <%= f.submit %>
  </p>
<% end %>

# app/views/organizations/_referral.html.erb
<p>
  <%= f.label :name, 'Referral Name' %><br />
  <%= f.text_field :name %>
</p>

Of course, I just read your new comments, and maybe you don't need this anymore. Oh well, more documentation for posterity :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文