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

NoTroutAboutIt

我正在尝试构建一个基本的食谱应用程序,但在让用户为一个食谱输入多种成分方面遇到麻烦。成分的允许参数数组最终为空。所以我想我的问题是-如何允许各种配料?

我的控制器:

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
理查德·派克

这里有几个问题,我只提供我要做的事情:

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

这仅适用于当前存在的成分。如果您想创建新的成分,以上方法将起作用。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Rails form_for使用Nil填充模型

来自分类Dev

Rails-同一表单的不同form_for语句

来自分类Dev

Rails form_for关系模型

来自分类Dev

如何在同一Rails form_for中更新两个User对象?

来自分类Dev

Rails 4:如何使用form_for和partials

来自分类Dev

如何获得引导通知以使用rails form_for Submit?

来自分类Dev

rails form_for和相关模型

来自分类Dev

rails form_for和相关模型

来自分类Dev

在 rails 中的同一模型上使用不同的 SQL 填充活动记录集合

来自分类Dev

Rails form_for多个不同的类对象?

来自分类Dev

Rails - 对同一记录的多个属性进行操作

来自分类Dev

CakePHP使用记录ID更新同一模型的多行

来自分类Dev

Rails中同一模型上的多个多态关联

来自分类Dev

将Select 2与form_for rails一起使用

来自分类Dev

Rails 从不同的模型一次创建多个记录

来自分类Dev

当有多个引用同一模型时,在迁移级别上使用Rails 4.2外键定义

来自分类Dev

在Rails中,如何对活动记录“ where”使用模型的方法?

来自分类Dev

如何强制多个视图显示同一模型行

来自分类Dev

如何同步共享同一模型的多个视图

来自分类Dev

如何绑定权重以训练同一模型的多个副本

来自分类Dev

使用同一模型可以有多个hasMany关系吗?

来自分类Dev

Rails:如何仅使用.collection获取保存的模型?

来自分类Dev

如何使用别名加入Rails活动记录

来自分类Dev

如何在Rails模型中使用order by

来自分类Dev

Ruby on Rails-如何使用嵌套模型

来自分类Dev

如何在Rails模型中使用order by

来自分类Dev

如何使用Rails 4设置嵌套模型

来自分类Dev

如何使用Rails实施委托授权模型?

来自分类Dev

如何在rails上通过form_for传递与模型无关的参数?

Related 相关文章

热门标签

归档