2016-02-13 84 views
1

我花了很多時間,但我不知道如何解決這個問題。沒有路線匹配,但創建了

我需要發送PUT請求,我想用AJAX通過點擊複選框做

我HAML

- subcate.products.each do |product| 
    = check_box_tag 'published', product.id, product.published, id: '',        | 
    data: {                      | 
     remote: true,                    | 
     url: url_for(controller 'products', action: :set_published, id: product.id, method: 'PUT'), | 

    } 

我的路線

namespace :admin do 
    resources :categories 
    resources :subcategories 
    resources :posts 

    resources :products do 
     resources :images, only: [:create, :destroy] 
     put 'set_published', to: 'products#set_published' 
    end 
    end 

我控制器

class Admin::ProductsController < AdminController 
    before_action :set_product, only: [:show, :edit, :update, :destroy] 

    def set_published 
    "test" 
    end 

end 

Whe n個I型

rake routes 

admin_product_set_published  PUT  /admin/products/:product_id/set_published(.:format)  admin/products#set_published 

我複選框是主要的管理頁面上,當我想進入我

No route matches {:action=>"set_published", :controller=>"admin/products", :id=>49, :method=>"PUT"} 

但我嘗試測試從控制檯控制器

Admin::ProductsController.new.set_published 

我得到了

=> "test" 

我不知道什麼是錯的路線

enter image description here

回答

1

rake routes告訴你,它需要一個product_id

/admin/products/:product_id/set_published(.:format) 

url_for您提供id

url_for(controller 'products', action: :set_published, id: product.id, method: 'PUT') 

只是將其更改爲(我建議使用路徑法):

admin_product_set_published_path(product_id: product.id) 
+1

太謝謝你了。它的工作非常棒 –