2017-12-28 33 views
0

체크 박스를 기반으로 항목 모음을 삭제하려고합니다. 맞춤 경로를 만들었습니다. 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 

나는 결과

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

그러나 그것은이다 올바른 컨트롤러 액션으로 가지 않는 것은 단 하나의 아이템에 대한 파괴 액션에서 끝납니다. 여기 내 컨트롤러가있다. 클래스 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 작업에 가고 싶다. 어떤 도움을 주셔서 감사합니다. 고맙습니다.

+1

당신은 아이템 자원을 2 번 선언합니다. 처음에는 목록 안에 있고 두 번째는 자체 목록에 있습니다. 중첩 된 항목 리소스 내에서 사용자 정의 삭제 경로를 이동해보십시오. –

+0

굉장! 그게 효과가 있었어. 도움에 감사드립니다. – Spikerr

+0

위대한, 다행스럽게 도울 수있어. 답변을 표시하려면 게시물을 수정해야합니다. –

답변

1

@ 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 

을 경로를 생성에

0

당신은

destroy_multiple_items DELETE /items/destroy_multiple/:id(.:format)   items#destroy_multiple 
이제 당신은 이런 식으로 뭔가를 얻을 수 (테스트하지 경로를하지만 코드가 바로 지금이다) rake routes을 실행 따라 시도 할 수 있습니다 이제

당신은 체크 박스

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

H를 사용하여 삭제를 시도 할 수 있습니다 ope to help