我有一个模型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
?
答案 0 :(得分:3)
resources :foo do
get 'another_action', :on => :collection
end
将生成以下路线:/foo/another_action
,而:
resources :foo do
get 'another_action', :on => :member
end
将生成:/ foo /:id / another_action。