Question

Controller:

    def index
        @assets = current_customer.assets
    end

  def destroy
    @asset = current_customer.assets.find_by(id: params[:id])
    redirect_to root_url
  end

Index.html.erb:

<% for asset in @assets %>
<% assetcount += 1 %>
<a href="#" data-dropdown="hover<%= assetcount %>"><%= image_tag asset.file_name.url(:thumb).to_s %></a>
<ul id="hover<%= assetcount %>" class="assets-dropdown text-center" data-dropdown-content>
  <li><a href="<%= asset.file_name.to_s %>" class="text-center">Source Image</a></li>
  <li><%= link_to "Delete Image", asset, method: :delete %></li>

</ul>
<% end %>

Routes:

  resources :customers do
    resources :assets
  end

When I click on my Delete Image link_to I get a Routing Error: No route matches [DELETE] "/"

My routes:

home_index_path  GET     /home/index(.:format)   home#index
root_path    GET     /   home#index
customer_assets_path     GET     /customers/:customer_id/assets(.:format)    assets#index
POST     /customers/:customer_id/assets(.:format)    assets#create
new_customer_asset_path  GET     /customers/:customer_id/assets/new(.:format)    assets#new
edit_customer_asset_path     GET     /customers/:customer_id/assets/:id/edit(.:format)   assets#edit
customer_asset_path  GET     /customers/:customer_id/assets/:id(.:format)    assets#show
PATCH    /customers/:customer_id/assets/:id(.:format)    assets#update
PUT  /customers/:customer_id/assets/:id(.:format)    assets#update
DELETE   /customers/:customer_id/assets/:id(.:format)    assets#destroy
customers_path   GET     /customers(.:format)    customers#index
POST     /customers(.:format)    customers#create
new_customer_path    GET     /customers/new(.:format)    customers#new
edit_customer_path   GET     /customers/:id/edit(.:format)   customers#edit
customer_path    GET     /customers/:id(.:format)    customers#show
PATCH    /customers/:id(.:format)    customers#update
PUT  /customers/:id(.:format)    customers#update
DELETE   /customers/:id(.:format)    customers#destroy
sessions_path    POST    /sessions(.:format)     sessions#create
new_session_path     GET     /sessions/new(.:format)     sessions#new
session_path     DELETE  /sessions/:id(.:format)     sessions#destroy
register_path    GET     /register(.:format)     customers#new
signout_path     DELETE  /signout(.:format)  sessions#destroy
signin_path  GET     /signin(.:format)   sessions#new

It looks like the route is there.. DELETE /customers/:customer_id/assets/:id(.:format) assets#destroy

Anyone know why?

Was it helpful?

Solution

Your error is in this line <%= link_to "Delete Image", asset, method: :delete %>

You want to pass the customer as well

<%= link_to "Delete Image", [current_customer, asset], method: :delete %></li>

To be more clear you might want

<%= link_to "Delete Image", path_for(current_customer, asset), method: :delete %></li>

Also, you are not actually destroying the @asset in your destroy method :)

  def destroy
      if asset = current_customer.assets.find_by(id: params[:id])
          asset.destroy! #or destroy without ! but then it could rollback in not destroy
      end
      redirect_to root_url
  end

OTHER TIPS

<%= link_to "Delete Image", [current_customer, asset], method: :delete %>

If 'current_customer' is a helper and it is available in the view:

<%= link_to "Delete Image", [current_customer, asset], method: :delete %>

If not:

<%= link_to "Delete Image", [asset.customer, asset], method: :delete %>

You can also change your routes if you want assets not to be nested in customers when destroy:

   resources :customers do
     resources :assets, exept: :destroy
   end

   resources :assets, only: :destroy
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top