Rails表单标签不会自定义删除路由,而是转到标准删除路由

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Rails表单标签不会自定义删除路由,而是转到标准删除路由相关的知识,希望对你有一定的参考价值。

我试图删除基于复选框的项目集合。我创建了一个自定义路线。 destroy_multiple路由。

  Rails.application.routes.draw do
  resources :projects do
    resources :lists, shallow: true do
      member do
        post :sort
      end
      resources :items, shallow: true
    end
  end

  resources :items do
    collection do
     delete :destroy_multiple, :as => :destroy_multiple
    end
  end
  resource :react
end

当我运行rake路线时,我得到了这个结果

Prefix Verb   URI Pattern                               Controller#Action
             sort_list POST   /lists/:id/sort(.:format)                 lists#sort
            list_items GET    /lists/:list_id/items(.:format)           items#index
                       POST   /lists/:list_id/items(.:format)           items#create
         new_list_item GET    /lists/:list_id/items/new(.:format)       items#new
             edit_item GET    /items/:id/edit(.:format)                 items#edit
                  item GET    /items/:id(.:format)                      items#show
                       PATCH  /items/:id(.:format)                      items#update
                       PUT    /items/:id(.:format)                      items#update
                       DELETE /items/:id(.:format)                      items#destroy
         project_lists GET    /projects/:project_id/lists(.:format)     lists#index
                       POST   /projects/:project_id/lists(.:format)     lists#create
      new_project_list GET    /projects/:project_id/lists/new(.:format) lists#new
             edit_list GET    /lists/:id/edit(.:format)                 lists#edit
                  list GET    /lists/:id(.:format)                      lists#show
                       PATCH  /lists/:id(.:format)                      lists#update
                       PUT    /lists/:id(.:format)                      lists#update
                       DELETE /lists/:id(.:format)                      lists#destroy
              projects GET    /projects(.:format)                       projects#index
                       POST   /projects(.:format)                       projects#create
           new_project GET    /projects/new(.:format)                   projects#new
          edit_project GET    /projects/:id/edit(.:format)              projects#edit
               project GET    /projects/:id(.:format)                   projects#show
                       PATCH  /projects/:id(.:format)                   projects#update
                       PUT    /projects/:id(.:format)                   projects#update
                       DELETE /projects/:id(.:format)                   projects#destroy
destroy_multiple_items DELETE /items/destroy_multiple(.:format)         items#destroy_multiple
                 items GET    /items(.:format)                          items#index
                       POST   /items(.:format)                          items#create
              new_item GET    /items/new(.:format)                      items#new
                       GET    /items/:id/edit(.:format)                 items#edit
                       GET    /items/:id(.:format)                      items#show
                       PATCH  /items/:id(.:format)                      items#update
                       PUT    /items/:id(.:format)                      items#update
                       DELETE /items/:id(.:format)                      items#destroy
                 react POST   /react(.:format)                          reacts#create
             new_react GET    /react/new(.:format)                      reacts#new
            edit_react GET    /react/edit(.:format)                     reacts#edit
                       GET    /react(.:format)                          reacts#show
                       PATCH  /react(.:format)                          reacts#update
                       PUT    /react(.:format)                          reacts#update
                       DELETE /react(.:format)                          reacts#destroy

我的表单标签看起来像这样..

<%= form_tag url_for(:controller => 'items', :action => 'destroy_multiple'), :method => 'delete' do %>

  <ul class="list-group sortable" data-update-url="<%= sort_list_path(list) %>">
    <%= render list.items %>
  </ul>
  <%= submit_tag("Delete Items") %>
<% end %>

我没有得到任何错误,但它没有进行正确的控制器操作,它最终只是针对单个项目的销毁操作。这是我的控制器。 class ItemsController <ApplicationController

  before_action :set_item, only: [:edit, :update]

  def create
    @list = List.find(params[:list_id])
    @item = @list.items.create params[:item].permit(:content)
  end

  def edit
  end

  def update
    @item.update_attributes(params[:item].permit(:content))
  end

  def destroy
    @item.destroy
  end

  def destroy_multiple

    debugger
  end

  private

  def set_item
    @item = Item.where(id: params[:id]).first!
  end

end

我只想让表单提交转到自定义destroy_multiple操作。我感谢任何帮助。谢谢。

答案

根据@JoshHumphrey,我的错误是有多条路线。我更新的路线文件是

Rails.application.routes.draw do
  resources :projects do
    resources :lists, shallow: true do
      member do
        post :sort
      end
      resources :items, shallow: true do 
        collection do
           delete :destroy_multiple, :as => :destroy_multiple
        end 
      end
    end
  end


  resource :react
end

我稍微更新了表单以传递列表ID,所以这里是表单

<%= form_tag(destroy_multiple_list_items_path(list_id: list.id),method: :delete,remote: true) do %>
  <ul class="list-group sortable" data-update-url="<%= sort_list_path(list) %>">
    <%= render list.items %>
  </ul>
  <%= submit_tag("Delete Selected",  :class => "btn btn-warning") %>
<% end %>

其他一切都保持不变!谢谢Josh的帮助。

另一答案

您可以尝试遵循,首先您创建像原始destroy行动的路线

resources :items do
 collection do
   #delete :destroy_multiple, :as => :destroy_multiple
   delete '/destroy_multiple/:id(.:format)' => 'items#destroy_multiple', constraints: { id: /.*/ }, as: :destroy_multiple
  end
end

现在运行rake routes你得到这样的东西(没有经过测试的路线,但现在的代码)

destroy_multiple_items DELETE /items/destroy_multiple/:id(.:format)         items#destroy_multiple

现在您可以尝试使用复选框删除

def destroy_multiple
   Item.where(id: params[:id]).destroy_all
   # redirect
end

希望能有所帮助

以上是关于Rails表单标签不会自定义删除路由,而是转到标准删除路由的主要内容,如果未能解决你的问题,请参考以下文章

Rails 路由:将自定义路由添加到标准操作列表

Rails自定义路由,如何删除?id =

Angular的内置模块

laravel 无法删除记录

H5C3--语义标签以及语义标签IE8兼容,表单元素新属性,度量器,自定义属性,dataList,网络监听,文件读取

通过自定义前端表单设置 Woocommerce 产品标签和类别