简体   繁体   English

Rails:has_many通过关联,以及用于创建新实例的表单

[英]Rails: has_many through association, and the form for creating new instances

I'm still super new with Rails, and just trying to get my first has_many through association set up. 我仍然是Rails的新手,只是试图通过关联设置获得我的第一个has_many。

Recipes have many ingredients, and each ingredient has an amount needed for the recipe. 食谱有许多成分,每种成分都有配方所需的量。 The ingredient_amount table has a recipe_id, an ingredient_id, and an amount. ingredient_amount表有recipe_id,ingredient_id和amount。

When creating a new recipe, I want to be able to create these recipe/ingredient associations in the same place. 在创建新配方时,我希望能够在同一个地方创建这些配方/配料关联。 In the end, I'm going to build an AJAX autocompleter for the ingredients. 最后,我将为这些成分构建一个AJAX自动完成器。 For now, as a baby step, I'd like to just assume the ingredient exists, and take care of checking once I've got this part down. 现在,作为一个婴儿步骤,我想假设成分存在,并在我将这部分放下后进行检查。

So, how can I make new.html.erb for recipes do this? 那么,我如何为配方制作new.html.erb呢? How can I extend the form for more than one ingredient? 如何扩展多种成分的表格?

As it stands now, after going through http://weblog.rubyonrails.org/2009/1/26/nested-model-forms I still can't get any fields to add ingredients. 按照现在的情况,经过http://weblog.rubyonrails.org/2009/1/26/nested-model-forms后,我仍然无法获得添加成分的任何字段。 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's new.html.erb as I have it currently: 这是我当前拥有的new.html.erb:

   <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 %>

The important bits of the recipe controller: 配方控制器的重要部分:

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 have no idea where to start in the ingredient_amounts controller. 而且......我不知道从哪里开始在ingredient_amounts控制器中。 This was my very first stab, and I'm pretty sure it's 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! 谢谢您的帮助!

I believe what you are looking for is 'Nested Model Forms'. 我相信你要找的是'嵌套模型表'。

Try this link: http://weblog.rubyonrails.org/2009/1/26/nested-model-forms 试试这个链接: http//weblog.rubyonrails.org/2009/1/26/nested-model-forms

It's hard to know what to search for when you dont really know the terminology to begin with :) 当你真的不知道开头的术语时,很难知道要搜索什么:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM