Search code examples
ruby-on-railsrubyruby-on-rails-4link-toactioncontroller

I'm getting No route matches [DELETE] with custom action and routes in ruby on rails


I have a show page which lists all objects in my DB. I have a delete link that sends the ID of that object to the destroy method in the associated controller and I can then easily delete that object and redirect back to the show page.

On the same show page I have a set of inline images beside each corresponding object. Each image is actually a link (except first as that shouldn't be deleted). When I click a specific image I send the ID of the object plus the column name of the object as a string to a new custom method called destroyImage.

I've created this method in my controller:

  def destroyImage
    garment = Parse::Query.new("Garments").eq("objectId", params[:id]).get.first
    garment[params[:imageToDelete]] = nil
    if garment.parse_delete
      flash[:success] = "Image successfully deleted!"
      redirect_to adminpanel_path
    else
      flash[:error] = "Image not deleted, try again!"
      render "show"
    end
  end

In my view:

   <td class= "images">
     <div id="deleteText"><%= "Click on image to delete." %></div>
     <%= image_tag garment["image"].url if garment["image"] %> 
     <%= link_to image_tag(garment["image2"].url), destroyImage_adminpanel_path(:id => garment["objectId"], :imageToDelete => "image2"), :method => 'delete' if garment["image2"] %>
     <%= link_to image_tag(garment["image3"].url), destroyImage_adminpanel_path(:id => garment["objectId"], :imageToDelete => "image3"), :method => 'delete' if garment["image3"] %>
     <%= link_to image_tag(garment["image4"].url), destroyImage_adminpanel_path(:id => garment["objectId"], :imageToDelete => "image4"), :method => 'delete' if garment["image4"] %>
     <%= link_to image_tag(garment["image5"].url), destroyImage_adminpanel_path(:id => garment["objectId"], :imageToDelete => "image5"), :method => 'delete' if garment["image5"] %>
     <%= link_to image_tag(garment["image6"].url), destroyImage_adminpanel_path(:id => garment["objectId"], :imageToDelete => "image6"), :method => 'delete' if garment["image6"] %>
     </td>
    </tr>
   </tbody>

In my routes:

Rails.application.routes.draw do
  resources :adminpanel do
    member do
      get 'destroyImage'
    end
  end

Rake routes:

destroyImage_adminpanel GET    /adminpanel/:id/destroyImage(.:format) adminpanel#destroyImage
       adminpanel_index GET    /adminpanel(.:format)                  adminpanel#index
                        POST   /adminpanel(.:format)                  adminpanel#create
         new_adminpanel GET    /adminpanel/new(.:format)              adminpanel#new
        edit_adminpanel GET    /adminpanel/:id/edit(.:format)         adminpanel#edit
             adminpanel GET    /adminpanel/:id(.:format)              adminpanel#show
                        PATCH  /adminpanel/:id(.:format)              adminpanel#update
                        PUT    /adminpanel/:id(.:format)              adminpanel#update
                        DELETE /adminpanel/:id(.:format)              adminpanel#destroy

I'm getting an error ActionController::RoutingError (No route matches [DELETE]. I've looked at my routing and there seems to be no path for destroyImage (DELETE) I can only see one for GET.

I'm sure this is small issue that can me fixed easily but after reading the guides I'm still slightly lost.

Appreciate your help.

Thanks.


Solution

  • You should create your route with delete instead of get:

    delete 'destroyImage'
    

    Also the convention in Ruby is that methods (thus, also Rails actions) are named with underscore instead of camel case, so your action should be named destroy_image.