我有一個滑軌模型,看起來是這樣的:如何使用此動態生成的字段保存模型?
class Recipe < ActiveRecord::Base
has_many :ingredients
attr_accessor :ingredients_string
attr_accessible :title, :directions, :ingredients, :ingredients_string
before_save :set_ingredients
def ingredients_string
ingredients.join("\n")
end
private
def set_ingredients
self.ingredients.each { |x| x.destroy }
self.ingredients_string ||= false
if self.ingredients_string
self.ingredients_string.split("\n").each do |x|
ingredient = Ingredient.create(:ingredient_string => x)
self.ingredients << ingredient
end
end
end
end
的想法是,當我創建從網頁中的成分,我通過在ingredients_string
,讓模型排序這一切了。當然,如果我正在編輯一個成分,我需要重新創建該字符串。這個錯誤基本上是這樣的:如何通知成分_字符串的視圖(優雅地)並且仍然檢查在set_ingredients
方法中是否定義了ingredient_string
?
對不起,我真的不明白你在問什麼。你想解決什麼問題?你能舉一個例子說明它是如何使用以及它在哪裏崩潰的? – bhollis 2008-09-21 04:53:08