我想我可能在这里遗漏了一些东西,或者我对 Rails 模型关联的理解还不够(仍在学习中)。我有两个模型,一个配方和一个成分。食谱有很多成分,当我填写表格时,我想将食谱和成分参数发布到各自的数据库中
配方模型
class Recipe < ActiveRecord::Base
attr_accessible :dish_name, :country_of_origin, :difficulty, :preperation_time
has_many :ingredients
end
配料模型
class Ingredient < ActiveRecord::Base
belongs_to :recipe
attr_accessible :ingredients
end
配方控制器
def new
@recipe = Recipe.new
end
def create
@recipe = Recipe.new(params[:recipe])
if @recipe.valid?
@recipe.save
redirect_to recipes_path, :notice => "Recipe sucessfully created."
else
flash.now.alert = "Please ensure all fields are filled in."
render :new
end
end
配料控制器
def new
@ingredient = recipes.find(params[:recipes_id].ingredients.new
@recipe_id = params[:recipe_id]
end
形式
<%= form_for @recipe do |f| %>
<%= f.label :dish_name, "Dish Name" %>
<%= f.text_field :dish_name, :placeholder => "Enter Dish Name" %>
more fields here(wanted to keep as short as poss)
<%= f.label :ingredients, "Ingredients" %>
<%= f.text_field :ingredients , :class => :ingred, :placeholder => "Enter Ingredient Here" %><br>
<% end %>
为代码的大小道歉,还有什么需要看看为什么我不能创建一个食谱吗?