【问题标题】:associated models in rails 3rails 3中的相关模型
【发布时间】:2012-11-02 22:22:54
【问题描述】:

我想我可能在这里遗漏了一些东西,或者我对 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 %>

为代码的大小道歉,还需要什么来了解为什么我无法创建配方吗?

【问题讨论】:

  • 为什么您“无法创建食谱”?你有错误吗?

标签: ruby-on-rails-3 model


【解决方案1】:

首先,您的成分模型是否包含像“recipe_id”这样的外键列?

另外,如果给定食谱的成分只是存储在单个文本字符串中,为什么不在食谱模型中将其作为一列而不是创建第二个模型?或者,如果您要将多种成分绑定到一个配方,您应该查看具有嵌套模型属性的表单。上面有一个很好的导轨here

希望这会有所帮助。

【讨论】:

    【解决方案2】:

    你的代码应该是链接这个...

    ---型号---

    class Recipe < ActiveRecord::Base
     attr_accessible :dish_name, :country_of_origin, :difficulty, :preperation_time, :ingredients_collection
     has_many :ingredients
     def ingredients_collection
       self.ingredients.collect(&:name).join(" ,")  
     end
     def ingredients_collection(ingredients)
       ingredients.each do |ingredient|
       i = Ingredient.create(:name => ingredient)    
       self.ingredients << i
     end
     end
     end
    
    class Ingredient < ActiveRecord::Base
      belongs_to :recipe
      attr_accessible :name # you should have some valid attribute name as it would conflict with the ingredients which is a method defined by the association (:has_many) in the instance of the Recipe...
      end
    

    ----控制器----

    def new
     @recipe = Recipe.new
    end
    def create
      @recipe = Recipe.new(params[:recipe])
      if @recipe.save # no need to call the valid the validations would take care of it by themselves
        redirect_to recipes_path, :notice => "Recipe sucessfully created."
      else
        flash.now.alert = "Please ensure all fields are filled in."
        render :new
      end
    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_collection , :class => :ingred, :placeholder => "Enter Ingredient Here" %><br>
      <% end %>
    

    我仍然建议您研究虚拟属性,我想它会非常适合您的情况和需求...

    http://railscasts.com/episodes/16-virtual-attributes了解虚拟属性

    【讨论】:

      猜你喜欢
      • 2011-07-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多