Mongoid kullanarak içine gömülü Malzemeler içeren bir Reçete modelim var.Rails 3'te Mongoid gömülü kaynakları kullanarak nasıl iç içe geçmiş bir form oluşturabilirim?
class Recipe
include Mongoid::Document
include Mongoid::Timestamps
field :title, :type => String
embeds_many :ingredients
accepts_nested_attributes_for :ingredients, :reject_if => lambda { |a| a[:title].blank? }, :allow_destroy => true
validates :title, :presence => true
end
class Ingredient
include Mongoid::Document
field :name, :type => String
field :quantity, :type => String
embedded_in :recipe, :inverse_of => :ingredients
end
Aynı anda, yeni bir tarif ve bu tarifi için ilişkili maddeler oluşturmak isterler, ama ben bunu hakkında gitmek istiyorum anlamak mücadele ediyorum. Bu benim şimdiye kadar ne var:
_form.html.erb - Tarif Kullanılan görmektedir
<%= form_for @recipe do |f| %>
...
<li>Title: <%= f.text_field :title %></li>
<% f.fields_for :ingredients do |builder| %>
<%= render "ingredient_fields", :f => builder %>
<% end %>
...
<%= f.submit %>
_ingredient_fields.html.erb
<%= f.text_field :name %>
Reçete Kontrol
def new
@recipe = Recipe.new
@ingredient = @recipe.ingredients.build
end
def create
@recipe = Recipe.new(params[:recipe])
if @recipe.save
redirect_to @recipe, notice: 'Recipe was successfully created.'
else
render action: "new"
end
end
İçindekiler Denetleyici
def new
@recipe = Recipe.find(params[:recipe_id])
@ingredient = @recipe.ingredients.build
end
def create
@recipe = Recipe.find(params[:recipe_id])
@ingredient = @recipe.ingredients.build(params[:ingredient])
# if @recipe.save
end
Bu, yeni içerikler formunu oluşturur, ancak bileşenler için alan yoktur. Herkes yanlış yaptığım şey hakkında herhangi bir işaretçi verebilir mi? Eğer iç içe formu gösterdiğinizde
Eğer bu sorunu çözmek için gereken herhangi bir bilgiyi kaçırıyorsam, lütfen bana bildirin, çünkü hala bu konuda güvendeyim ... – purpletonic