Em Sta
Em Sta

Reputation: 1696

Route to action of controller

Previous i used:

data: {autocomplete_source: categories_path} %>

To point to the action index in categories controller.All worked fine!

Now i created an new action in categories controller

def search
@categories = Category.order(:name).where("name like ?", "%#{params[:term]}%")

render json: @categories.map(&:name)
end

And tried to point to that action:

data: {autocomplete_source: search_categories_path} %>

But i get the error:

undefined local variable or method `search_categories_path' for #<#<Class:0x51844c8>:0x5375820>

What did i wrong? Thanks!

My routes:

     products GET    /products(.:format)            products#index
          POST   /products(.:format)            products#create
 new_product GET    /products/new(.:format)        products#new
edit_product GET    /products/:id/edit(.:format)   products#edit
  product GET    /products/:id(.:format)        products#show
          PUT    /products/:id(.:format)        products#update
          DELETE /products/:id(.:format)        products#destroy
 categories GET    /categories(.:format)          categories#index
          POST   /categories(.:format)          categories#create
new_category GET    /categories/new(.:format)      categories#new
edit_category GET    /categories/:id/edit(.:format) categories#edit
 category GET    /categories/:id(.:format)      categories#show
          PUT    /categories/:id(.:format)      categories#update
          DELETE /categories/:id(.:format)      categories#destroy

Routes:

Autorails::Application.routes.draw do
resources :products


resources :categories do
 collection do
    :search
 end
end

Upvotes: 0

Views: 61

Answers (3)

Luiz E.
Luiz E.

Reputation: 7229

You should do something like this

resources :categories do
  collection do
    get :search
  end
end

Upvotes: 0

Marek Lipka
Marek Lipka

Reputation: 51151

You should have something like this in your routes.rb:

resources :categories do
  collection do
    get :search
  end
end

Upvotes: 2

Ayonix
Ayonix

Reputation: 2002

Check rake routes if that route really exists under that name. For more information see http://guides.rubyonrails.org/routing.html#path-and-url-helpers

Upvotes: 3

Related Questions