使用nested_form宝石在Rails的,不确定的方法“link_to_remove /加”

问题描述:

我试图使用nested_form宝石,我遇到了一些问题:使用nested_form宝石在Rails的,不确定的方法“link_to_remove /加”

确切的错误: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>&nbsp;</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 

有人可以给我一些指点?谢谢!

我有同样的问题。我通过将调用link_to_remove放回到new.html.erb文件中来修复它。看起来它不起作用,如果电话是在部分。不知道如何解决这个问题。

更新:我想我找到了解决方法。您可以像这样渲染部分:

<%= f.fields_for :tasks %> 

以这种方式做它似乎有所作为。我在github上的nested_form的页面上得到了这个(关于Partials的部分)

+0

我在视图中调用方法,仍然得到no方法错误 – sixty4bit 2014-09-09 02:45:34