2013-06-19 10 views
1

我有一個模型foo定義具有成員端點的資源端點

在我config/routes.rb我有:

resources :foo do 
    member do 
    post 'approve' 
    post 'delete' 
    end 

    get 'another_action' 
end 

如果我做一個jQuery $.get("/foo/another_action")電話,我得到下面的輸出:

ActiveRecord::RecordNotFound (Couldn't find Foo with id=another_action): 
    app/controllers/foo_controller.rb:20:in `show' 

我也想這樣做:

resources :foo do 
    member do 
    post 'approve' 
    post 'delete' 
    end 
end 

resource :foo do 
    get 'another_action' 
end 

如何指定/foo/another_action的路線,以便控制呃將其視爲資源級端點而不是id

回答

3
resources :foo do 
    get 'another_action', :on => :collection 
end 

將生成以下路線:/foo/another_action,而:

resources :foo do 
    get 'another_action', :on => :member 
end 

將生成:/富/:ID/another_action。

看看指南:http://guides.rubyonrails.org/routing.html

+0

謝謝!我已經瀏覽了路由指南,但是我設法錯過了'collection',並且在我所有的Google搜索中,我都沒有想到要使用該關鍵字。一旦SO會讓我接受這個答案。 – asfallows