我花了很多时间,但我不知道如何解决这个问题。
我需要发送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
当我输入
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"
我不知道路线有什么问题
答案 0 :(得分: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)