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

4

1 回答 1

4
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。

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

于 2013-06-19T20:05:28.457 回答