【问题标题】:Using nested_form gem for in Rails, undefined method "link_to_remove/add"在 Rails 中使用nested_form gem,未定义方法“link_to_remove/add”
【发布时间】:2012-02-26 22:02:14
【问题描述】:

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

谁能给我一些建议?谢谢!

【问题讨论】:

    标签: jquery ruby-on-rails forms rubygems gem


    【解决方案1】:

    我遇到了同样的问题。我通过将对 link_to_remove 的调用带回 new.html.erb 文件来修复它。如果调用是部分的,它似乎不起作用。不知道如何解决这个问题。

    更新:我想我找到了绕过它的方法。您可以像这样渲染部分:

    <%= f.fields_for :tasks %>
    

    这样做似乎有所作为。我从 github 页面上的 nested_form 的 README 中得到了这个(Partials 部分)

    【讨论】:

    • 我正在视图中调用方法,但仍然收到无方法错误
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多