如何使用Rails form_for获取相同模型的多个记录

时间:2016-01-20 04:35:47

标签: ruby-on-rails

我正在尝试构建一个基本的食谱应用程序,但我很难让用户为一个食谱输入多种成分。成分的允许参数数组最终为空。所以我想我的问题是 - 我如何允许一系列成分?

我的控制器:

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

2 个答案:

答案 0 :(得分: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

答案 1 :(得分: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字段,则必须在控制器中构建多个对象:

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

这仅适用于现有成分。如果你想创造新的成分,上面的方法就可以了。