Rails: has_many through association and form to create new instances

I'm still super new with Rails and just trying to get my first has_many through the established association.

Recipes contain many ingredients, and each ingredient has the amount needed for the recipe. The component_amount table has recipe_id, component._id and quantity.

When creating a new recipe, I want to be able to create recipes / ingredients associations in the same place. In the end, I'm going to create AJAX autorun for the ingredients. For now, as a childโ€™s step, I would just suggest that the ingredient exists, and take care of checking as soon as I have this part.

So how can I do new.html.erb for recipes? How can I extend the form for more than one ingredient?

As of now, after going through http://weblog.rubyonrails.org/2009/1/26/nested-model-forms I still canโ€™t get any fields for adding ingredients. The current code is below.

class Recipe < ActiveRecord::Base
    has_many :ingredient_amounts
    has_many :ingredients, :through => :ingredient_amounts
    accepts_nested_attributes_for :ingredient_amounts, :allow_destroy => true
end

class IngredientAmount < ActiveRecord::Base
  belongs_to :ingredient
  belongs_to :recipe
end

class Ingredient < ActiveRecord::Base
  has_many :ingredient_amounts
  has_many :recipes :through => :ingredient_amounts
end

Here is new.html.erb, as I currently have:

   <h1>New recipe</h1>

<% form_for @recipe do |f| %>
  <%= f.error_messages %>

  <p>
    <%= f.label :name %><br />
    <%= f.text_field :name %>
  </p>
  <p>
    <%= f.label :instructions %><br />
    <%= f.text_area :instructions %>
  </p>
  <p>
    <%= f.label :numberOfServings %><br />
    <%= f.text_field :numberOfServings %>
  </p>
  <p>
    <%= f.label :prepTime %><br />
    <%= f.text_field :prepTime %>
  </p>

  <p>
    <% f.fields_for :ingredient_amounts do |ingredient_form| %>
    <%= ingredient_form.label :ingredient_formedient_id, 'Ingredient' %>
      <%= ingredient_form.collection_select :ingredient_id, Ingredient.all, :id, :name, :prompt => "Select an Ingredient"%>
      <%= ingredient_form.text_field :amount %>
    <% unless ingredient_form.object.new_record? %>
        <%= ingredient_form.label :_delete, 'Remove:' %>
        <%= ingredient_form.check_box :_delete %>

    <% end %>
  </p>
  <% end %>
  <p>
    <%= f.submit 'Create' %>
  </p>
<% end %>

<%= link_to 'Back', recipes_path %>

Important recipe controller bits:

def new
    @recipe = Recipe.new

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @recipe }
    end
  end
  def create
    @recipe = Recipe.new(params[:recipe])

    respond_to do |format|
      if @recipe.save
        flash[:notice] = 'Recipe was successfully created.'
        format.html { redirect_to(@recipe) }
        format.xml  { render :xml => @recipe, :status => :created, :location => @recipe }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @recipe.errors, :status => :unprocessable_entity }
      end
    end
  end

And ... I do not know where to start in the component.samounts controller. This was my first blow, and I'm sure it is not so close :)

def new
    @recipe = Recipe.find(params[:recipe_id])
    @ingredient = Ingredient.find(params[:ingredient_id])
    @ingredient_amount = Recipe.ingredient_amounts.build
  end

Thanks for the help!

+4
source share
1 answer

I believe you are looking for "Nested Model Forms."

Try this link: http://weblog.rubyonrails.org/2009/1/26/nested-model-forms

Don't know what to look for if you really don't know the terminology to start with :)

+1
source

All Articles