使用成员端点定义资源端点

时间:2013-06-19 20:01:12

标签: ruby-on-rails routes

我有一个模型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

1 个答案:

答案 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。

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