0

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?

1 Answer 1

0

Whenever you are using nested forms than u need to declare form in this way for edit and new

<%= form_with(model: [recipe_ingredient.recipy, recipe_ingredient], local: true) do |form| %>

But partials and forms become tricky. Note the square brackets:

<%= form_for [@recipy, @recipe_ingredient] do |f| %>

Most important, if you want a URI, you may need something like this:

recipy_recipe_ingredient_path(@recipy, @recipe_ingredient)

Alternatively:

[@recipy, @recipe_ingredient]
Sign up to request clarification or add additional context in comments.

1 Comment

I actually did not change my form but changed the order so now I first display the recipy_ingredients with the delete option and below that the form to add new recipy_ingredients. it is working fine although I do not really understand what the issue was in the first place. I use AJAX to submit the form so it might be something with that? Thanks anyway

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.