gpt4 book ai didi

ruby-on-rails-3 - 帮助从 fields_for 渲染部分内容

转载 作者:行者123 更新时间:2023-12-04 07:16:06 29 4
gpt4 key购买 nike

我正在使用 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

“元素”型号:

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

“零件”模型:

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

最佳答案

经过数小时的测试和谷歌搜索,我终于解决了。

似乎我会使用相同的局部变量名称来呈现用于 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 %>

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

关于ruby-on-rails-3 - 帮助从 fields_for 渲染部分内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6939634/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com