控制器的行动路线

时间:2013-07-30 13:54:29

标签: ruby-on-rails ruby-on-rails-3

我上次使用:

data: {autocomplete_source: categories_path} %>

指向类别控制器中的动作索引。一切正常!

现在我在类别控制器中创建了一个新动作

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

3 个答案:

答案 0 :(得分:3)

检查rake routes该路由是否确实存在。 有关详细信息,请参阅http://guides.rubyonrails.org/routing.html#path-and-url-helpers

答案 1 :(得分:2)

您的routes.rb

应该有类似的内容
resources :categories do
  collection do
    get :search
  end
end

答案 2 :(得分:0)

你应该做这样的事情

resources :categories do
  collection do
    get :search
  end
end