无法在Rails中删除客户的资产

时间:2014-02-13 23:08:14

标签: ruby-on-rails ruby routes link-to

控制器:

    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 %>

路线:

  resources :customers do
    resources :assets
  end

当我点击我的删除图片link_to时,我收到路由错误:没有路由匹配[删除]“/”

我的路线:

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

看起来路线就在那里.. DELETE /customers/:customer_id/assets/:id(.:format)assets#destroy

任何人都知道为什么?

3 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:1)

您的错误就在这一行     &lt;%= link_to“删除图片”,资产,方法:: delete%&gt;

您也希望传递客户

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

更清楚你可能想要

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

另外,你实际上并没有破坏destroy方法中的@asset:)

  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

答案 2 :(得分:1)

如果'current_customer'是帮助器,并且在视图中可用:

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

如果不是:

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

如果您希望在销毁时不要将资产嵌套在客户中,您也可以更改路线:

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

   resources :assets, only: :destroy