多対多の関連付けに関連する値を追加するための追加リンクを作成しようとしている Railscast をフォローしていました。モデルを使用して多対多を実装し、実装していないという唯一の違い
has_and_belongs_to_many.
そして、それはうまく機能しません。
(事前設定は問題ありませんが、追加ボタンは失敗します。)
ここに私のコードがあります
ファイル:ingredient_recipe.rb
class IngredientRecipe < ActiveRecord::Base
attr_accessible :created_at, :ingredient_id, :order, :recipe_id
belongs_to :recipe
belongs_to :ingredient
end
ファイル:ingredient.rb
class Ingredient < ActiveRecord::Base
has_many :ingredient_recipes
has_many :recipes, :through => :ingredient_recipes
...
ファイル:recipes.rb
class Recipe < ActiveRecord::Base
has_many :ingredient_recipes
has_many :ingredients, :through => :ingredient_recipes
...
UIで
<%= link_to_add_fields "Add", f, :ingredient_recipes %>
メソッドが定義されている
def link_to_add_fields(name, f, association)
new_object = f.object.class.reflect_on_association(association).klass.new
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
問題
render(association.to_s.singularize + "_fields", :f => builder) は
<
%= f.text_field :name%> <%= f.hidden_field :_destroy%> <%= link_to "x","" を表示します、クラス: "remove_fields"%> エラーを出力します
ActionView::Template::Error (undefined method `name' for #<IngredientRecipe:0x6393ee8>):
何か案は?