2010-01-11 28 views
4

我仍然超級新的Rails,並試圖讓我的第一個has_many通過關聯設置。Rails:has_many通過關聯,並創建新的實例的形式

食譜有很多成分,每種成分都有配方所需的量。 ingredient_amount表具有recipe_id,ingredient_id和金額。

創建新配方時,我希望能夠在同一個地方創建這些配方/配料關聯。最後,我要爲這些原料構建一個AJAX autocompleter。現在,作爲一個嬰兒的一步,我想只是假設這種成分存在,並且一旦我把這個部分弄下來就照顧檢查。

那麼,我怎樣才能讓食譜的new.html.erb做到這一點?我如何將表單擴展爲多種成分?

現在看來,經過http://weblog.rubyonrails.org/2009/1/26/nested-model-forms 我仍然無法獲得任何領域添加配料。當前的代碼如下。

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 

這裏的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 %> 

配方控制器的重要位:

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 

而且......我不知道在哪裏從成分控制器開始。 這是我的第一個嘗試,我敢肯定它不是如此接近:)

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

感謝您的幫助!

+0

開始你有沒有算出來的術語呢?我正在嘗試做幾乎完全相同的事情。 – Eric 2012-11-06 17:36:20

回答

1

我相信你正在尋找的是'嵌套式模型'。

嘗試此鏈接:http://weblog.rubyonrails.org/2009/1/26/nested-model-forms

很難知道搜索什麼,當你真的不知道用:)

+0

鏈接幫助!但我仍然無法實現它的工作。我正在編輯上述文章,以獲得新的new.html.erb和recipe模型。 – Joan 2010-01-12 00:21:06