I've been following the rails edge guide in building a simple recipe app, and I'm now trying to branch out and do a few more interesting things to try and learn more about rails.
Rather than only being able to edit an ingredient within an embedded form, I thought it might be neat to go to the recipe/:recipe_id/ingredient/:ingredient_id/edit page and update the ingredient there.
However, the form partial I use for editing the ingredient is nested in the recipe form. so it starts with
<%= form_for([@recipe, @recipe.ingredients.build]) do |f| % >
the edit page knows what recipe is but what it really wants is ingredient. However if I change the @recipe to @ingredient, then this same form does not work in the recipe controller.
I'm quite sure I'm not supposed to have to make two seperate forms with the same fields to do this.
-------- more data about what the form partial and routing looks like -------- The complete embedded form for recipe & ingredients is
< form_for([@recipe, @recipe.ingredients.build]) do |f| %> <%= f.label :ingredient %> <%= f.text_field :ingredient %> <%=f.label :amount %> <%=f.text_field :amount %> <% end %>
This is called from the recipes/show.html.erb
<%= render @recipe.ingredients %>
What I am now trying to do is be able to call the same form from ingredients/edit.html.erb
<%= render @ingredients %>
as the ingredients does not have the context of a recipe from within the ingredients controller.
Is there a better way to connect ingredients to recipes? I'm just now realizing that this isn't really a nested form with the exception that it is called from within the recipe page.