osadasami
osadasami

Reputation: 55

No route matches but created

I have spent a lot of time but I don't know how to solve this problem.

I need to send PUT request and I want to do it with ajax by click on checkbox

My 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'), |

    }

My routes

  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

My controller

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

  def set_published
    "test"
  end

end

When I type

rake routes 

I got

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

My checkbox is on the main admin page and when i want to enter I got

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

But i try to test controller from console

Admin::ProductsController.new.set_published

i got

=> "test"

I don't know what is wrong with the routes

enter image description here

Upvotes: 1

Views: 29

Answers (1)

spickermann
spickermann

Reputation: 107142

rake routes tells you that it expects a product_id:

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

But in url_for you provide a id:

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

Just change it to (and I recommend using the path method):

admin_product_set_published_path(product_id: product.id)

Upvotes: 1

Related Questions