帮助渲染 fields_for 的部分内容

发布于 2024-11-27 15:19:17 字数 3278 浏览 0 评论 0原文

我正在使用nested_attributes,并尝试按照Ryan Bates有关嵌套模型的截屏视频(#196)通过ajax实现动态添加/删除字段

这样做,它不会工作,但是当删除“link_to_add_fields”行时,效果很好。

问题是我不知道我是否正确地完成了所有关联。

<%= form_for(@item) do |f| %>
<% if @item.errors.any? %>
<div id="error_explanation">
  <h2><%= pluralize(@item.errors.count, "error") %> prohibited this item from being saved:</h2>

  <ul>
  <% @item.errors.full_messages.each do |msg| %>
    <li><%= msg %></li>
  <% end %>
  </ul>
  </div>
  <% end %>

  <div class="field">
    <%= f.label :title %><br />
    <%= f.text_field :title %>
  </div>
  <div class="field">
    <%= f.label :item_type_id %><br />
    <%= f.select :item_type_id, @item_types.collect { |p| [ p.title, p.id ]} %>
    <br />
    <%= f.fields_for :item_parts do |parts_form| %>
      <%= render "item_part_fields", :p => parts_form %>
    <% end %>
  </div>
  <%= link_to_add_fields "Add Part", f, :part %>
  <div class="actions">
    <%= button_for "Save Item", :class => 'positive pill button', :button_type => 'submit' %>
  </div>
<% end %>

实际上我的模型是:

“ItemPart”模型:

class ItemPart < ActiveRecord::Base
  belongs_to :item
  belongs_to :part 
end

“Item”模型:

class Item < ActiveRecord::Base
  belongs_to :item_type
  has_many :item_parts
  has_many :parts, :through => :item_parts
  accepts_nested_attributes_for :item_parts, :allow_destroy => true
  after_save :save_id
  validates :title, :presence => true

  def save_id
    item_part_attributes = [ { :item_id => self.id } ]
  end

end

“Part”模型:

class Part < ActiveRecord::Base
  has_one :part_type
  has_many :item_parts
  has_many :items, :through => :item_parts

end

我这样做的错误:

undefined method `klass' for nil:NilClass
Extracted source (around line #26):



23:       <%= render "item_part_fields", :p => parts_form %>
24:     <% end %>
25:   </div>
26:   <%= link_to_add_fields "Add Part", f, :item %>
27:   <div class="actions">

应用程序跟踪

app/helpers/application_helper.rb:44:in `link_to_add_fields'
app/views/items/_form.html.erb:26:in `block in _app_views_items__form_html_erb__1418129287024756954_2169222480__3228169100620818569'
app/views/items/_form.html.erb:1:in `_app_views_items__form_html_erb__1418129287024756954_2169222480__3228169100620818569'
app/views/items/edit.html.erb:3:in `_app_views_items_edit_html_erb___1857878264245794505_2169270380_890290209246324491'

Application_helper.rb

def link_to_add_fields(name, f, association)
  new_object = f.object.class.reflect_on_association(association).klass.new # error line :44
  fields = f.fields_for(association, new_object, :child_index => "new_#{association}") do |builder|
    render(association.to_s.singularize + "_fields", :f => builder)
  end
  link_to_function(name, "add_fields(this, '#{association}', '#{escape_javascript(fields)}')" )
end

I'm using nested_attributes and trying to implement the add/remove fields on-the-fly throu ajax following Ryan Bates screencast about Nested Model (#196)

Doing this, it won't work, but when removing the "link_to_add_fields" line, it works fine.

The problem is that I'm don't know if I doing this all associations right.

<%= form_for(@item) do |f| %>
<% if @item.errors.any? %>
<div id="error_explanation">
  <h2><%= pluralize(@item.errors.count, "error") %> prohibited this item from being saved:</h2>

  <ul>
  <% @item.errors.full_messages.each do |msg| %>
    <li><%= msg %></li>
  <% end %>
  </ul>
  </div>
  <% end %>

  <div class="field">
    <%= f.label :title %><br />
    <%= f.text_field :title %>
  </div>
  <div class="field">
    <%= f.label :item_type_id %><br />
    <%= f.select :item_type_id, @item_types.collect { |p| [ p.title, p.id ]} %>
    <br />
    <%= f.fields_for :item_parts do |parts_form| %>
      <%= render "item_part_fields", :p => parts_form %>
    <% end %>
  </div>
  <%= link_to_add_fields "Add Part", f, :part %>
  <div class="actions">
    <%= button_for "Save Item", :class => 'positive pill button', :button_type => 'submit' %>
  </div>
<% end %>

Actually my models are:

"ItemPart" model:

class ItemPart < ActiveRecord::Base
  belongs_to :item
  belongs_to :part 
end

"Item" model:

class Item < ActiveRecord::Base
  belongs_to :item_type
  has_many :item_parts
  has_many :parts, :through => :item_parts
  accepts_nested_attributes_for :item_parts, :allow_destroy => true
  after_save :save_id
  validates :title, :presence => true

  def save_id
    item_part_attributes = [ { :item_id => self.id } ]
  end

end

"Part" model:

class Part < ActiveRecord::Base
  has_one :part_type
  has_many :item_parts
  has_many :items, :through => :item_parts

end

The error I'm getting doing this way:

undefined method `klass' for nil:NilClass
Extracted source (around line #26):



23:       <%= render "item_part_fields", :p => parts_form %>
24:     <% end %>
25:   </div>
26:   <%= link_to_add_fields "Add Part", f, :item %>
27:   <div class="actions">

Application trace

app/helpers/application_helper.rb:44:in `link_to_add_fields'
app/views/items/_form.html.erb:26:in `block in _app_views_items__form_html_erb__1418129287024756954_2169222480__3228169100620818569'
app/views/items/_form.html.erb:1:in `_app_views_items__form_html_erb__1418129287024756954_2169222480__3228169100620818569'
app/views/items/edit.html.erb:3:in `_app_views_items_edit_html_erb___1857878264245794505_2169270380_890290209246324491'

Application_helper.rb

def link_to_add_fields(name, f, association)
  new_object = f.object.class.reflect_on_association(association).klass.new # error line :44
  fields = f.fields_for(association, new_object, :child_index => "new_#{association}") do |builder|
    render(association.to_s.singularize + "_fields", :f => builder)
  end
  link_to_function(name, "add_fields(this, '#{association}', '#{escape_javascript(fields)}')" )
end

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

情绪失控 2024-12-04 15:19:17

经过几个小时的测试和谷歌搜索后,我终于解决了这个问题。

似乎我会使用相同的局部变量名称来呈现用于 form_for 的部分,然后再次将其用作所需的辅助方法参数。 (在本例中为“f”变量。)

我将清洁工作代码粘贴在下面,

<%= form_for(@item) do |f| %>

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

<p>
  <%= f.label :item_type_id %><br />
  <%= f.select :item_type_id, @item_types.collect { |p| [ p.title, p.id ]} %><br />
</p>

<p>
  <%= f.fields_for :item_parts do |parts_form| %>
    <%= render "item_part_fields", :f => parts_form %>
  <% end %>
</p>

<p><%= link_to_add_fields "Add Part", f, :item_parts %></p>
<p><%= button_for "Save Item", :class => 'positive pill button', :button_type => 'submit' %></p>

<% end %>

希望有一天这会对某人有所帮助:)

After hours and hours doing tests and googling a lot, I get it finally solved.

Seems that I'd to use the same local variable name to render the partial used for the form_for and then use it again as a helper method parameter needed. (In this case, "f" variable.)

I pasting the cleaning working code below

<%= form_for(@item) do |f| %>

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

<p>
  <%= f.label :item_type_id %><br />
  <%= f.select :item_type_id, @item_types.collect { |p| [ p.title, p.id ]} %><br />
</p>

<p>
  <%= f.fields_for :item_parts do |parts_form| %>
    <%= render "item_part_fields", :f => parts_form %>
  <% end %>
</p>

<p><%= link_to_add_fields "Add Part", f, :item_parts %></p>
<p><%= button_for "Save Item", :class => 'positive pill button', :button_type => 'submit' %></p>

<% end %>

Hope this going to help someone someday :)

空袭的梦i 2024-12-04 15:19:17

您的“添加更多”按钮有误。这应该是,

<p><%= link_to_add_fields "Add Part", f, :item_parts %></p>

关联名称应该是有很多关系的表名称。

There is a mistake in your add more button. This should be,

<p><%= link_to_add_fields "Add Part", f, :item_parts %></p>

The association name should be the table name on which have many relation is.

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