2013-07-30 210 views
0

的行動上,我用:路由到控制器

指在類別中的動作指數controller.All工作得很好!

現在,我創建了類控制器的新動作

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

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

並試圖指向行動:

data: {autocomplete_source: search_categories_path} %> 

,但我得到的錯誤:

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

什麼了我錯了?謝謝!

我的路線:

 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 

路線:

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


resources :categories do 
collection do 
    :search 
end 
end 

回答

2

你應該有這樣的事情在你的routes.rb

resources :categories do 
    collection do 
    get :search 
    end 
end 
+0

仍然沒有工作!我添加了我的路線問題!謝謝 –

+1

@EmSta也添加你的'routes.rb'的內容。 –

+0

@EmSta是否在編輯'routes.rb'後重啓了應用服務器? –

0

你應該做這樣的事情

resources :categories do 
    collection do 
    get :search 
    end 
end