Ruby on rails 如何使用Rails form_为同一型号获取多条记录
我正试图建立一个基本的食谱应用程序,但我有麻烦,让用户输入一个食谱多个成分。配料的允许参数数组最终为空。所以我想我的问题是-我如何允许一系列的成分 我的控制器:Ruby on rails 如何使用Rails form_为同一型号获取多条记录,ruby-on-rails,Ruby On Rails,我正试图建立一个基本的食谱应用程序,但我有麻烦,让用户输入一个食谱多个成分。配料的允许参数数组最终为空。所以我想我的问题是-我如何允许一系列的成分 我的控制器: class RecipesController < ApplicationController def new @recipe = Recipe.new @ingredient = Ingredient.new end def create safe_params = params.require(:r
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
def new
@recipe = Recipe.new
3.times do
@recipe.ingedients.build
end
end
class RecipesController[])
@配方=配方。新(安全参数)
@保存
成分参数各不相同|
@配方、配料
型号:
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
类配方
需要对代码进行一些更改
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
class RecipesController
因此,配料字段生成了三次
<%= f.fields_for :ingredients do |i| %>
<%= i.label :name %>
<%= i.text_field :name %>
<% end %>
请通过下面的链接,它将明确您对嵌套表单的看法
这里有几个问题,我只提供我要做的:
#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 %>
其他一切看起来都会很好
--
另外,如果您想填充has\u和\u属于\u多个
关系,您只需传递[relationship]\u id
参数即可:
<%= form_for @recipe do |f| %>
<%= f.collection_select :ingredient_ids, Ingredient.all, :id, :name %>
<%= f.submit %>
<% end %>
这只适用于目前存在的成分。如果您想创建新的配料,上述操作将起作用。您可以在日志中看到什么允许错误?这非常有用。谢谢我现在唯一的挑战是,我需要表格来处理现有和新的成分。如果你对此有什么想法,我很想听听。如果没有,我相信我最终会找到答案的。再次感谢!你最好抬头看看。我可以写一个更新,如果你愿意;虽然不需要编写更新,但它将脱离问题的上下文。我会自己想办法的。谢谢
<%= form_for @recipe do |f| %>
<%= f.collection_select :ingredient_ids, Ingredient.all, :id, :name %>
<%= f.submit %>
<% end %>