【问题标题】:Can't delete asset for customer in Rails无法在 Rails 中删除客户的资产
【发布时间】:2014-03-13 02:47:42
【问题描述】:

控制器:

    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 时,我收到一个路由错误:没有路由匹配 [DELETE] "/"

我的路线:

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

有人知道为什么吗?

【问题讨论】:

  • 您需要将customerasset 一起传递给link_to,以便助手生成正确的链接

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


【解决方案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

【讨论】:

    【解决方案2】:

    你的错误在这一行

    你也想超越客户

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

    【讨论】:

    • 不错!谢谢 :) 也感谢所有说同样话的人
    【解决方案3】:
    <%= link_to "Delete Image", [current_customer, asset], method: :delete %>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-04-15
      • 1970-01-01
      • 2014-11-02
      • 1970-01-01
      • 2023-03-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多