I have two tables, recipy and recipe_ingredients, recipe_ingredients is set up as a nested resource
resources :recipies do
resources :recipe_ingredients
end
Recipy_ingredients are shown on my recipy_edit page and I want to also delete them from there. My edit view:
<% @recipe_ingredients.each do |ing| %>
<p> <%= ing.amount %> <%= ing.unit %> <%= ing.ingredient.try(:ing_name) %> </p>
<p><%= link_to ('<i class="material-icons"> clear </i>').html_safe, recipy_recipe_ingredient_path(ing.recipy, ing),
method: :delete, data: {confirm: 'Are you sure?'} %>
</p>
<% end %>
</div>
My def edit from the recipies_controller:
def edit
@recipy = Recipy.find(params[:id])
@recipe_ingredients = @recipy.recipe_ingredients
end
def destroy from the recipe_ingredients controller:
def destroy
@recipe_ingredient = RecipeIngredient.find(params[:id])
recipy = @recipe_ingredient.recipy
@recipe_ingredient.destroy
redirect_to recipy
end
when trying to open the edit page I keep getting below error msg:
No route matches {:action=>"show", :controller=>"recipe_ingredients", :id=>nil, :recipy_id=>"160"}, missing required keys: [:id]
and this line is highlighted:
<p><%= link_to ('<i class="material-icons"> clear </i>').html_safe, recipy_recipe_ingredient_path(ing.recipy, ing),
Is the route messed up? I thought this would be the correct snytax for nested resources? Any help would be appreciated. Thanks. Let me know fi any additional info is needed!
EDIT
After some digging around I figured that if I remove a partial before the link_to in the edit view the code works fine. My partial before the code:
Add new Ingredient
<%= form_with model:[@recipy, @recipy.recipe_ingredients.build] do |form| %>
<div class="field">
<%= form.label :amount %>
<%= form.text_field :amount %>
</div>
<div class="field">
<%= form.label :unit %>
<%= form.text_field :unit %>
</div>
<div class="field">
<%= form.select(:ingredient_id, options_from_collection_for_select(Ingredient.all, :id, :ing_name))%>
</div>
<div class="actions">
<%= form.submit "add ingredient", remote: true %>
</div>
<% end %>
and the def create for the recipe_ingredients:
def create
@recipy = Recipy.find(params[:recipy_id])
@recipe_ingredient = @recipy.recipe_ingredients.new(recipe_ingredient_params)
respond_to do |f|
if @recipe_ingredient.save
@recipe_ingredients = @recipy.recipe_ingredients.last
# f.html
# f.json
f.js { render :template => "/recipies/updatingview.js.erb", layout: false}
end
end
end
Why would this mess up the delete function? If I remove this partial the delete actually works. And advice?