0

I am trying to create a delete button on my show page that shows the data of the selected recipe. My problem is every time I try to make a delete button i keep either getting path errors like "cant find the path recipe_path" or an error that says

/home/robert/recipe/app/views/recipes/show.html.erb:41: syntax error, unexpected tIDENTIFIER, expecting ')' ...pend=( link_to 'Delete' recipes, method: :delete );@output_b... ... ^~~~~~~" 

if you need any more code or information please ask.

I have tried changing my path looking at others that had delete issues like this and watching several videos on deleting in ruby on rails but none of it has worked.

<button><%= link_to 'Delete' recipes, method: :delete   
 %></button>


def destroy
   @recipes = Recipe.find(params[:id])
   @recipes.destroy
 end

for some permutations of the delete buttons I have made I have gotten either the error messages stated above or a button shows up that does nothing.

Rockwell Rice
  • 3,376
  • 5
  • 33
  • 61

1 Answers1

1

You are missing a comma between 'Delete' recipes in the arguments list:

<%= link_to 'Delete', recipes, method: :delete %>

Which solves the syntax error. However placing an <a> inside a <button> tag is invalid HTML.

Permitted content
Phrasing content but there must be no Interactive content
- MDN Web Docs: The Button element

Either just style a regular <a> with CSS to look like a button or create a discrete form with button_to:

<%= button_to 'Delete', recipes, method: :delete %>

Since you seem to be just flailing around you can get an example of the how to actually code this by running the scaffold generator.

$ rails g scaffold recipe title:string

Which generates:

# config/routes.rb
Rails.application.routes.draw do
  resources :recipes
  # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
end

# app/controllers/recipes_controller.rb
class RecipesController < ApplicationController
  before_action :set_recipe, only: [:show, :edit, :update, :destroy]

  # GET /recipes
  # GET /recipes.json
  def index
    @recipes = Recipe.all
  end

  # ...

  # DELETE /recipes/1
  # DELETE /recipes/1.json
  def destroy
    @recipe.destroy
    respond_to do |format|
      format.html { redirect_to recipes_url, notice: 'Recipe was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_recipe
      @recipe = Recipe.find(params[:id])
    end

    # ...
end

# app/views/recipes/index.html.erb
<table>
  <thead>
    <tr>
      <th colspan="3"></th>
    </tr>
  </thead>

  <tbody>
    <% @recipes.each do |recipe| %>
      <tr>
        <td><%= link_to 'Show', recipe %></td>
        <td><%= link_to 'Edit', edit_recipe_path(recipe) %></td>
        <td><%= link_to 'Destroy', recipe, method: :delete, data: { confirm: 'Are you sure?' } %></td>
      </tr>
    <% end %>
  </tbody>
</table>

Note that in Rails proper pluralization is very important as it ties in with the whole convention over configuration approach.

max
  • 96,212
  • 14
  • 104
  • 165
  • Note that `link_to` uses javascript to fake a form submission so that the request is sent as `DELETE` instead of as a `GET` request. If the link is not working properly check the browser console for errors. You can also just change it for `button_to` which works without JS. – max Apr 14 '19 at 09:47