2016-01-20 48 views
1

我正在嘗試構建一個基本的食譜應用程序,但遇到了麻煩,允許用戶爲一個配方輸入多個配料。成分的允許參數數組最終爲空。所以我想我的問題是 - 我該如何准許一系列配料?如何使用Rails爲同一模型獲取多個記錄form_for

我的控制器:

class RecipesController < ApplicationController 

def new 
    @recipe = Recipe.new 
    @ingredient = Ingredient.new 
end 

def create 
    safe_params = params.require(:recipe).permit(:title, :instruction, :category_id) 
    ingredient_params = params.require(:recipe).permit(:ingredient => []) 
    @recipe = Recipe.new(safe_params) 
    @recipe.save 
    ingredient_params.each do |i| 
     @recipe.ingredients << Ingredient.find_or_create_by(name: i[:ingredient][:name]) 
    end 
    render body: YAML::dump(ingredient_params) 
    #redirect_to index_path(id: @recipe.id) 
end 

end 

形式:

<%= form_for(@recipe, :url => create_path) do |f| %> 
<%= f.label :category %> 
<%= f.select :category_id, options_for_select(Category.all.map{|c|[c.title, c.id]}) %> 

<%= f.label :title %> 
<%= f.text_field :title%> 

<%= f.label :instruction %> 
<%= f.text_area(:instruction, size: "50x10") %> 

<%= f.fields_for "ingredients[]", @ingredient do |i| %> 
    <%= i.label :name %> 
    <%= i.text_field :name %> 
    <%= i.text_field :name %> 
    <%= i.text_field :name %> 
<% end %> 

<%= f.submit "Submit" %> 
<% end %> 

型號:

class Recipe < ActiveRecord::Base 
    has_and_belongs_to_many :ingredients 
    accepts_nested_attributes_for :ingredients 
    belongs_to :category 
end 

class Category < ActiveRecord::Base 
    has_many :recipes 
end 

class Ingredient < ActiveRecord::Base 
    has_and_belongs_to_many :recipes 
end 
+0

什麼允許錯誤你能看到日誌? – chaitanya

回答

0

有幾個問題在這裏,我就提供什麼,我會怎麼做:

#app/controllers/recipes_controller.rb 
class RecipesController < ApplicationController 

    def new 
     @recipe = Recipe.new 
     @recipe.ingredients.new 
    end 

    def create 
     @recipe = Recipe.new safe_params 
     @recipe.save 
    end 

    private 

    def safe_params 
     params.require(:recipe).permit(:title, :instruction, :category_id, ingredients_attributes: [:name]) 
    end 

end 

#app/views/recipes/new.html.erb 
<%= form_for @recipe do |f| %> 
    <%= f.label :category %> 
    <%= f.collection_select :category_id, Category.all, :id, :name %> 

    <%= f.label :title %> 
    <%= f.text_field :title%> 

    <%= f.label :instruction %> 
    <%= f.text_area(:instruction, size: "50x10") %> 

    <%= f.fields_for :ingredients do |i| %> 
     <%= i.label :name %> 
     <%= i.text_field :name %> 
    <% end %> 

    <%= f.submit "Submit" %> 
<% end %> 

如果你想要有多個ingredients字段,你必須要build mul在控制器中的tiple對象:

def new 
    @recipe = Recipe.new 
    3.times do 
     @recipe.ingedients.build 
    end 
end 

其他一切看起來都會很好。

-

作爲一個額外的,如果你想填充has_and_belongs_to_many關係,你就可以只通過[relationship]_ids參數:

<%= form_for @recipe do |f| %> 
    <%= f.collection_select :ingredient_ids, Ingredient.all, :id, :name %> 
    <%= f.submit %> 
<% end %> 

這將只對現有成分的工作。如果你想創造新的成分,上述將起作用。

+0

這非常有幫助。謝謝!我現在面臨的唯一挑戰是我需要表格來處理現有的和新的成分。如果你有任何我想聽到的想法。如果不是,我相信我可以最終弄清楚。再次感謝! – NoTroutAboutIt

+0

你最好查找['has_nested_attributes_for'](http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html)。如果你願意,我可以寫更新。它將脫離該問題的上下文,儘管 –

+0

無需編寫更新。我會盡力弄清楚自己。謝謝! – NoTroutAboutIt

0

需要做出一些改變你的代碼

class RecipesController < ApplicationController 

def new 
    @recipe = Recipe.new 
    # here you can decide how many ingredients do you want. (Not in form looping through text fields) 
    3.times do 
     ingredient = @recipe.ingredients.build 
    end  
end 

所以成分的領域被生成了三次。

<%= f.fields_for :ingredients do |i| %> 
    <%= i.label :name %> 
    <%= i.text_field :name %> 
<% end %> 

請通過下面的鏈接,它會清除有關嵌套形式

你的想法

http://railscasts.com/episodes/196-nested-model-form-part-1

相關問題