如何在Rails 4.2

时间:2015-06-17 14:59:23

标签: ruby-on-rails ruby associations

我正在尝试制作食谱应用。在我的应用程序中我有表成分(名称),表食谱(type_of,代码,描述)连接表数量(recipe_id,ingredient_id,数量)

我希望能够在创建时将配料添加到我的食谱中。食谱可以有很多成分。

我的模特:

class Ingredient < ActiveRecord::Base
   has_many :quantities
   has_many :recipes, through: :quantities
   has_many :stock_of_ingredients
end

class Quantity < ActiveRecord::Base
  belongs_to :recipe
  belongs_to :ingredient

  accepts_nested_attributes_for :ingredient,
                                :reject_if => :all_blank
end

class Recipe < ActiveRecord::Base
   has_many :quantities
   has_many :ingredients, :through => :quantities
    accepts_nested_attributes_for :quantities,
           :reject_if => :all_blank,
           :allow_destroy => true
  accepts_nested_attributes_for :ingredients

end   

My Recipe控制器是从脚手架创建的,所以

def create
    @recipe = Recipe.new(recipe_params)

    respond_to do |format|
      if @recipe.save
        format.html { redirect_to @recipe, notice: 'Recipe was successfully created.' }
        format.json { render :show, status: :created, location: @recipe }
      else
        format.html { render :new }
        format.json { render json: @recipe.errors, status: :unprocessable_entity }
      end
    end   end

我还添加了

  def recipe_params
      params.require(:recipe).permit(:type_of_base, :code, :description, :quantities_attributes =>[:quantity, :recipe_id, :ingredient_id, :ingredient_attributes])
    end

我不知道我需要从基础控制器创建数量控制器吗?

现在我的表单看起来像这样

<%= form_for(@recipe, html: { class: "form-horizontal", role: "form" }) do |f| %>
 ...

  <div class="form-group">
   ...
  </div>
<div class="ingderdient-wraper">
  <%= render "quantites/quantity_fields" %>
</div>


  <div class="form-group">
    <div class="col-sm-offset-2 col-sm-10">
      <%= f.submit class: "btn btn-primary" %>
    </div>
  </div>
<% end %>

我的渲染文件

 <%= text_field :quantity %>
  <%= select :ingredient_ids, Ingredient.all.collect {|x| [x.name, x.id]}, {}, :multiple => true %>

但是,如果不能正常工作,我希望有可能添加更多的成分。需要一个按钮添加更多。

如果有人能向我解释如何达到目的。问候

2 个答案:

答案 0 :(得分:0)

根据我的理解,你只需要制作一个javascript方法来动态生成字段,你可以使用这个rails cast http://railscasts.com/episodes/197-nested-model-form-part-2。这正是您所需要的。

答案 1 :(得分:0)

在我看来,你有模特的问题。在此任务中,您应该使用has_and_belongs_to_many(habtm)关联。你可以看到Active Record Associations

你可以制作模型:

class Ingredient < ActiveRecord::Base
   has_and_belongs_to_many :recipes
...
end

class Recipe < ActiveRecord::Base
   has_and_belongs_to_many :ingredients
...
end 

在迁移中:

def self.up
    create_table :ingradients, force: true do |t|
      t.string :name    
      t.references :recipe, index: true
      t.timestamps null: false    
    end
    create_table :recipes, force: true do |t|
      t.string :name    
      t.references :ingradient, index: true
      t.timestamps null: false    
    end
    create_table :ingradients_recipes, force: true, id: false do |t|
      t.belongs_to :ingradient, index: true
      t.belongs_to :recipe, index: true
      t.integer    :quantity
    end
  end

我认为接下来的步骤很简单......