我正在尝试使用nested_form gem,我遇到了一些问题:
确切错误: undefined method link_to_remove for #<ActionView::Helpers::FormBuilder:0xb57e288>
这是我的 new.html.erb:
<%= javascript_include_tag :defaults, 'nested_form' %>
<div class="recipe new">
<h2>Add a new recipe</h2>
<%= nested_form_for(:recipe, :url => {:action => 'create'}) do |f| %>
<%= render(:partial => "form", :locals => {:f => f}) %>
<br />
<br />
<div class="form-buttons">
<%= submit_tag("Create Recipe") %>
</div>
<% end %>
</div>
以下是部分表单 _form.html.erb :
<%= javascript_include_tag :defaults, 'nested_form' %>
<h3>Recipe Name</h3>
<%= f.text_field(:recipe_name) %>
<%= fields_for :ingredient do |i| %>
<table summary="Ingredient form fields">
<tr>
<td> </td>
<td>Ingredient</th>
<td>Quantity</td>
<td>Measurement Unit</td>
<td>Total Calories</td>
<td>Total Grams</td>
</tr>
<tr>
<%= i.link_to_remove "Remove this ingredient" %>
<td><%= fields_for :food do |r| %> <%= r.text_field(:long_desc) %><% end %></td>
<td><%= i.text_field(:quantity, :size => '6', :maxlength => '6') %></td>
<td><%= i.text_field(:units, :size => '20', :maxlength => '20') %></td>
</tr>
<% end %>
<%= i.link_to_add "Add an ingredient" %>
<tr>
<td><%= f.text_field(:total_grams, :size => '4', :maxlength => '4') %></td>
<td><%= f.text_field(:carb_calories, :size => '4', :maxlength => '4') %></td>
<td><%= f.text_field(:fat_calories, :size => '4', :maxlength => '4') %></td>
<td><%= f.text_field(:protein_calories, :size => '4', :maxlength => '4') %></td>
</tr>
</table>
以下是 recipe.rb模型:
class Recipe < ActiveRecord::Base
belongs_to :user
has_many :foods, :through => :recipe_ingredient
has_many :recipe_ingredient
accepts_nested_attributes_for :recipe_ingredient
attr_accessible :recipe_name, :prep_time_mins, :total_grams, :carb_calories, :fat_calories, :protein_calories
attr_accessible :recipe_ingredient_attributes
end
这是 recipe_ingredients.rb模型:
# join table between each individual food item and their individual nutritional data
class RecipeIngredient < ActiveRecord::Base
belongs_to :recipe
belongs_to :food, :foreign_key => 'ndb_no'
accepts_nested_attributes_for :food
attr_accessible :food_attributes, :quantity, :units
end
有人可以给我一些指示吗?谢谢!
答案 0 :(得分:2)
我遇到了同样的问题。我通过将对link_to_remove的调用带回new.html.erb文件来修复它。如果呼叫是部分的,它似乎不起作用。不知道怎么解决这个问题。
更新:我想我找到了解决问题的方法。您可以像这样呈现部分:
<%= f.fields_for :tasks %>
这样做似乎有所作为。我从github页面上的README中获取了nested_form(Partials部分)