使用Rails API的自定义路由

时间:2013-05-25 03:54:48

标签: ruby-on-rails rails-routing

我正在使用Rocket_Pants gem为我现有的Rails应用程序提供API访问。我的路线中有以下内容:

  # HTTP routing 
  resources :posts do
    collection do
      get 'search'
    end
  end

  # API routing
  api :version => 1 do
    resources :posts, :only => [:index, :show]
    resources :posts do
      collection do
        get 'search'
      end
    end
  end

我对posts/post/:id works的json API请求。但是,localhost:3000/1/posts/search?query=my_query的json API查询没有。

(注意:对localhost:3000/posts/search?query=my_query的http请求有效)。

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

我认为您不应多次重新定义:posts块中的api资源。

api :version => 1 do
  resources :posts, only: [:index, :show] do
    collection do
      get 'search'
    end
  end
end
相关问题