Urgghhh - Newbie help needed to stop me giving up on Rails.
I have 3 nested models. Recipe->RecipeParts->RecipeIngredients (to the ingredients index so maybe 4 levels)
class Recipe < ActiveRecord::Base has_many :recipe_parts accepts_nested_attributes_for :recipe_parts
class RecipePart < ActiveRecord::Base belongs_to :recipe has_many :recipe_ingredients has_many :ingredients, :through => :recipe_ingredients accepts_nested_attributes_for :recipe
class RecipeIngredient < ActiveRecord::Base belongs_to :recipe_part belongs_to :ingredient
I'm trying to write an edit page for Recipe that includes a list of RecipeParts - and within that a list of RecipeIngredients (each as a select list) <h1>Editing recipe</h1>
<% form_for(@recipe) do |recipe_form| %>
<% recipe_form.fields_for :recipe_parts do |recipe_part_form| %> <% recipe_part_form.fields_for :recipe_ingredients do |rcp_ing| %> <p><%= rcp_ing.collection_select( :name, @ing_list_for_select,:id, :name) %></p>
<% end %> <p><%= recipe_part_form.text_field :title %></p> <p><%= recipe_part_form.text_field :description %></p> <% end %> <%= link_to "Add Part", {:controller => "recipes",:action => "add_recipe_part", :id => @recipe.id} %> <%= recipe_form.submit "Update" %> <% end %> <%= link_to 'Show', @recipe %> | <%= link_to 'Back', recipes_path %>
The multiple RecipeParts list correctly, but within each part I get 1 selection for the ingredients only, despite the data having a mixture of 0-3 ingredients for the various parts.
So - my recipe_part_form.fields_for line doesn't seem to be iterating over the recipe_ingredients properly , and I can't work out why - I think I'm using the identical structure that I did for the RecipeParts - which works fine. Secondly - the collection_select isn't giving me a correct Selected item - but this could be due to the first problem perhaps.
Sorry if the post is too long/stupid/wrong but Rails/Ruby is proving to be (BY FAR) the hardest and most obtuse environment I've tried learning in almost 30 years of programming. After almost 3 months of online tutorials,books, reading forums etc and experimentation I can't understand just how little I can get working in Rails.
Ian