0
나는 요리법 앱 인 Ruby on Rails의 일반적인 학습 앱으로 보이는 것을하고 있습니다. 특히 has_many : through 관계로 조리법 및 재료 작업. 백만 가지의 예제와 질문을 통해 다 대다 관계 설정과 다중 모델 양식이 작동하지만 추가 필드를 추가하고 싶습니다. 작동시키지 못합니다. 이 물건들이 어떻게 작동하는지 이해하는 것 같아.has_many를 추가하는 방법 : 세부 정보를 양식에 첨부 RoR
모델 :
이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
컨트롤러 :
이def create
@recipe = Recipe.new(params[:recipe])
if @recipe.save
redirect_to :action => 'show', :id => @recipe
flash[:notice] = "Your record has been saved."
else
render :action => 'new'
end
end
def update
params[:recipe][:existing_recipe_ingredient_attributes] ||= {}
@recipe = Recipe.find(params[:id])
if @recipe.update_attributes(params[:recipe])
redirect_to :action => 'show', :id => @recipe
flash[:notice] = "Your changes have been saved."
else
render :action => 'edit'
end
end
보기 : 코드의 벽
죄송합니다, 희망은 의미가 여기에 빠른 세부 사항입니다. 이해할 수없는 것은 "금액"텍스트 필드가 작동하지 않는 이유입니다. 백만 가지 방법을 시도했지만 작동하지 않습니다. 이 경우 얻을 수있는 오류는 "정의되지 않은 메소드 'amount for #"입니다.
여기서 누락 된 주요 연결은 무엇입니까? 감사. 언뜻
감사합니다,하지만 그게 더 나쁜 것처럼 보인다. 이제 컴파일 오류가 발생했습니다. 컴파일 오류 /home/jchandler/rails-tests/recipes/app/views/recipes/_recipe_ingredient.html.erb:4 : 구문 오류, 예기치 않은 ')' ... cipe_ingredient do | ri_form |) .to_s); @ output_buffer.concat ... ^ /home/jchandler/rails-tests/recipes/app/views/recipes/_recipe_ingredient.html.erb:12 : 구문 오류, 예기치 않은 kens, 예상 ')' /home/jchandler /rails-tests/recipes/app/views/recipes/app/views/recipes/_recipe_ingredient.html.erb:14 : 구문 오류, 예상치 못한 kEND, 예상 ')' – SenorPuerco
'/ views/recipes/_recipe_ingredient.html.erb'의 4 행은 무엇입니까? . – apneadiving
죄송합니다. 변경 사항을 제안한 행입니다. <% = fields_for 접두어, recipe_ingredient do | ri_form | %> – SenorPuerco