Ruby on rails 如何将has_MUN:通过详细信息添加到表单RoR

Ruby on rails 如何将has_MUN:通过详细信息添加到表单RoR,ruby-on-rails,ruby,has-many,Ruby On Rails,Ruby,Has Many,我正在做的似乎是RubyonRails的一个常见学习应用程序,配方应用程序。具体来说,作为一个整体来研究食谱和配料有很多:通过关系。通过查看一百万个示例和问题,我的多对多关系设置和多模型表单正常工作,但我想添加一个附加字段,但无法正常工作。感觉我已经接近了解这些东西是如何工作的了。以下是快速详细信息: 型号: class Ingredient < ActiveRecord::Base has_many :recipe_ingredients has_many :recipes, :

我正在做的似乎是RubyonRails的一个常见学习应用程序,配方应用程序。具体来说,作为一个整体来研究食谱和配料有很多:通过关系。通过查看一百万个示例和问题,我的多对多关系设置和多模型表单正常工作,但我想添加一个附加字段,但无法正常工作。感觉我已经接近了解这些东西是如何工作的了。以下是快速详细信息:

型号:

class Ingredient < ActiveRecord::Base
  has_many :recipe_ingredients
  has_many :recipes, :through => :recipe_ingredients
end

class RecipeIngredient < ActiveRecord::Base
  belongs_to :recipe
  belongs_to :ingredient
end

class Recipe < ActiveRecord::Base
  has_many :recipe_ingredients
  has_many :ingredients, :through => :recipe_ingredients
  accepts_nested_attributes_for :ingredients, :recipe_ingredients

  def new_recipe_ingredient_attributes=(recipe_ingredient_attributes)
    recipe_ingredient_attributes.each do |attributes|
      recipe_ingredients.build(attributes)
    end
  end

  def existing_recipe_ingredient_attributes=(recipe_ingredient_attributes)
    recipe_ingredients.reject(&:new_record?).each do |recipe_ingredient|
      attributes = recipe_ingredient_attributes[recipe_ingredient.id.to_s]
      if attributes
        recipe_ingredient.attributes = attributes
      else
        recipe_ingredient.delete(recipe_ingredient)
      end
    end
  end

  def save_recipe_ingredients
    recipe_ingredients.each do |recipe_ingredient|
      recipe_ingredient.save(false)
    end
  end
end
视图:

对不起,代码墙,希望它有意义。我不明白的是为什么amount文本字段不起作用。我尝试了一百万种不同的方法,但都没能奏效。在本例中,我得到的错误是未定义的方法“amount”


我在这里错过了什么关键连接?谢谢。

乍一看,您只需更换:

      <% fields_for prefix, recipe_ingredient do |ri_form| %>
与:


谢谢你看,但这似乎让事情变得更糟了。我现在有一个编译错误。编译错误/home/jchandler/rails tests/recipes/app/views/recipes/_recipe_component.html.erb:4:语法错误,意外…cipe_component do|ri|u form|to|s@输出\u buffer.concat…^/home/jchandler/rails tests/recipes/app/views/recipes/_recipe_component.html.erb:12:语法错误,意外的kENSURE,预期的/home/jchandler/rails tests/recipes/app/views/recipes/_recipe_component.html.erb:14:语法错误,意外的kEND,预期的和/views/recipes/_recipe_component.html.erb的第4行是什么,这就是你建议改变的路线。奇怪的您是否尝试将前缀和配方_成分放在括号内?抱歉,已转移到一个项目中,尚未返回到该项目。因为这只是为了练习,我可能现在就让它去吧。无论如何,谢谢你的帮助。
<% form_for(@recipe) do |f| %>
    <%= f.label :name %><br />
    <%= f.text_field :name %>
etc.....
    Ingredients:
    <div id="recipe_ingredients">
      <div class="recipe_ingredient">
      <% new_or_existing = recipe_ingredient.new_record? ? 'new' : 'existing' %>
      <% prefix = "recipe[#{new_or_existing}_recipe_ingredient_attributes][]" %>
      <% fields_for prefix, recipe_ingredient do |ri_form| %>
        <p>
          <%= ri_form.collection_select(:id, Ingredient.find(:all), :id, :name, :include_blank => true) %>
          <%= ri_form.text_field :amount %>
        </p>
      <% end %>
      </div>
    </div>
  </p>
  <p>
    <%= f.submit 'Create' %>
  </p>
<% end %>
      <% fields_for prefix, recipe_ingredient do |ri_form| %>
      <%= fields_for prefix, recipe_ingredient do |ri_form| %>