Search code examples
ruby-on-railsformsbuttonsubmitnested-resources

Summit button not working in rails form_with edit action


I have created a form to edit a nested resource within a rails application. The form renders properly and the submit button works with the form for the create action but causes noting to happen in when applied to the edit action. No error is triggered and the page doesn't change so it is hard to figure out where the problem exists. Similar problems seam to be the result of a syntax error but I cant seem to find one in this case however, I may be missing something. Below is the form in question.

<h1>Edit an Ingredient<h1>

    <%= form_with model: [ @recipe, @ingredient ], local: true do |form| %>

      <p>
        <%= form.label :quantity %><br>
        <%= form.text_field :quantity %>
      </p>

      <p>
        <%= form.label :measurement %><br>
        <%= form.text_area :measurement %>
      </p>

      <p>
        <%= form.label :ingredientname %><br>
        <%= form.text_area :ingredientname %>
      </p>

      <p>
        <%= form.label :label %><br>
        <%= form.text_area :label %>
      </p>

      <p>
        <%= form.submit %>
      </p>

      <% end %>

    <%= link_to 'Back', recipes_path %>

And the functioning "New" form...

<h1>Add an Ingredient<h1>

<%= form_with model: [ @recipe, @recipe.ingredients.build ], local: true do |form| %>

  <p>
    <%= form.label :quantity %><br>
    <%= form.text_field :quantity %>
  </p>

  <p>
    <%= form.label :measurement %><br>
    <%= form.text_area :measurement %>
  </p>

  <p>
    <%= form.label :ingredientname %><br>
    <%= form.text_area :ingredientname %>
  </p>

  <p>
    <%= form.label :label %><br>
    <%= form.text_area :label %>
  </p>

  <p>
    <%= form.submit %>
  </p>

  <% end %>

<%= link_to 'Back', recipes_path %>

And finally the relevant controller...

class IngredientsController < ApplicationController

def new
    @recipe = Recipe.find(params[:recipe_id])
end

def edit
    @recipe = Recipe.find(params[:recipe_id])
    @ingredient = @recipe.ingredients.find(params[:id])
end

def create
    @recipe = Recipe.find(params[:recipe_id])
    @ingredient = @recipe.ingredients.create(ingredient_params)
    redirect_to recipe_path(@recipe)
end

def update
    @recipe = Recipe.find(params[:recipe_id])
    @ingredient = @recipe.ingredients.find(params[:id])
end

def destroy
    @recipe = Recipe.find(params[:recipe_id])
    @ingredient = @recipe.ingredients.find(params[:id])
    @ingredient.destroy
    redirect_to recipe_path(@recipe)
end


private
    def ingredient_params
        params.require(:ingredient).permit(:quantity, :measurement, :ingredientname, :label)
    end
end

Additionally the form is populated correctly when it is rendered leading to me believe it is not a problem with the form_with statement. Any help is appreciated!


Solution

  • I was able to work out the solution. The submit button was not working due to an incomplete definition of the update action in the controller. Instead of ...

    def update
      @recipe = Recipe.find(params[:recipe_id])
      @ingredient = @recipe.ingredients.find(params[:id])
    end
    

    the update action should be defined as...

    def update
        @recipe = Recipe.find(params[:recipe_id])
        @ingredient = @recipe.ingredients.find(params[:id])
    
        if @ingredient.update(ingredient_params)
            redirect_to @recipe
        else
            render 'edit'
        end
    end