Rails 3 using fields_for in view doesn't work -



Rails 3 using fields_for in view doesn't work -

i'm trying implement master detail in rails form fields_for.

i have 1 model called recipe:

class recipe < activerecord::base validates :name, :presence => true validates :directions, :presence => true has_many :recipe_ingredients end

and 1 model called recipeingredient:

class recipeingredient < activerecord::base belongs_to :recipe #belongs_to :ingredient end

in new controller populate ingredients 3 empty records this:

def new @recipe = recipe.new 3.times {@recipe.recipe_ingredients.build} # @ingredients = recipeingredient.new respond_to |format| format.html # new.html.erb format.xml { render :xml => @recipe } end end

what in view output recipes fields (which works ok) , 3 fields recipe ingredients. in top part of view have this:

<%= form_for :rec |f| %>

i list recipe fields displayed correctly, like:

<div class="field"> <%= f.label :name %><br /> <%= f.text_field :name %> </div>

i seek display ingredient lines code never seems go fields_for section:

first try:

<% ingredient in @recipe.recipe_ingredients %> prints out 3 times <% fields_for "...", ingredient |ingredient_form| %> never prints out <p> ingredient: <%= ingredient_form.text_field :description %> </p> <% end %> <% end%>

i have 3 empty lines in recipe_ingredients , loop seems iterate 3 times code within fields_for never triggers.

second try:

<% recipe_ingredient in @recipe.recipe_ingredients %> b <% fields_for "...", recipe_ingredient |recipe_ingredient| %> rec: <%= recipe_ingredient.text_field :description %> <% end %> <% end %>

third seek (based on reply here):

<% form_for :recipe |f| %> <% f.fields_for ingredients |ingredient_form| %> <p> ingredient: <%= ingredient_form.text_field :description %> </p> <% end %> <% end %>

is there obvious i'm doing wrong?

switch to

<% form_for :recipe |f| %> <% f.fields_for :ingredients |ingredient_form| %> <p> ingredient: <%= ingredient_form.text_field :description %> </p> <% end %> <% end %>

ruby-on-rails

Comments

Popular posts from this blog

java - How to set log4j.defaultInitOverride property to false in jboss server 6 -

c - GStreamer 1.0 1.4.5 RTSP Example Server sends 503 Service unavailable -

Using ajax with sonata admin list view pagination -