0
Кто-нибудь знает, как обрабатывать эти атрибуты в новых контроллерах и создавать действия?Rails 4.2 Создать объект ActiveRecord с вложенными параметрами
(byebug) params
{"utf8"=>"V", "authenticity_token"=>"Wex9nnFigOviySzjfPN6zw==",
"recipe"=>{"name"=>"Cupcake"},
"name"=>{"ingredient_id"=>"2"},
"quantity"=>{"amount"=>"zwei"},
"commit"=>"Create", "controller"=>"recipes", "action"=>"create"}
Я не знаю, как создать запись величины со значением из ингредиента в рецепте # нового
("name"=>{"ingredient_id"=>"2")
Две записей должны быть созданы (Рецепт и количество).
Recipe
name
Quantity
ingredient_id
recipe_id
amount
Ingredient
name
class Recipe < ActiveRecord::Base
belongs_to :user
has_many :quantities
has_many :ingredients, through: :quantities
accepts_nested_attributes_for :quantities, :reject_if => :all_blank, :allow_destroy => true
accepts_nested_attributes_for :ingredients
end
class Ingredient < ActiveRecord::Base
has_many :quantities
has_many :recipes, :through => :quantities
end
class Quantity < ActiveRecord::Base
belongs_to :recipe
belongs_to :ingredient
accepts_nested_attributes_for :ingredient, :reject_if => :all_blank
end
<%= form_for(@recipe) do |f| %>
<%= f.label :name %>
<%= f.text_field :name %>
<%= form_for(@recipe.quantities.build) do |g| %>
<%= select("name", "ingredient_id", Ingredient.all.collect {|p| [ p.name, p.id ] }, {include_blank: 'Auswählen'}) %>
<%= g.label :amount %>
<%= g.text_field :amount %>
<%= g.submit "Create" %>
<% end %>
<% end %>