开发者

reuse embedded form within its own controller

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.


You have

/recipe/:recipe_id/ingredient/:ingredient_id/edit

which is a route to edit an existing ingredient. But your form

<%= form_for([@recipe, @recipe.ingredients.build]) do |f| % >

is expecting you to build a new ingredient which belongs to @recipe.

If you want to edit an existing ingredient then you need to get it using your params.

@recipe = Recipe.find params[:recipe_id]
@ingredient = @recipe.ingredients.find params[:ingredient_id]
<%= form_for([@recipe, @ingredient]) do |f| % >    

I don't really know what example you're following but I guess an ingredient is a many to many join between recipe and (say) food? If that is the case, and ingredient belongs to a recipe rather than many recipes then you can do this:

/ingredient/:ingredient_id/edit
@ingredient = Ingredient.find params[:ingredient_id]
@recipe = @ingredient.recipe

and thus:

<%= form_for(@ingredient) do |f| % >    

further suggestion:

I can't say for sure without seeing all your template but are you doing something like:

<%- form_for @item do |f| -%>
  <%= f.fields_for :field -%>

 <%- form_for @different_item do |ff| -%>
   <%= ff.fields_for :another_field  -%>
 <%- end -%>

<%- end -%>

Actually, can you post the gist of your form as above.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜