0

初学者问题在这里。我在显示的表中添加了另一列以获取资源索引,但我不知道如何设置路由,以便在单击特定资源对象的链接时转到页面:resource/run/id_number (其中 run 是我添加的链接的名称)。我已经有一个运行的视图文件和控制器方法,并且该部分有效,所以我必须命名路由以使其以这种方式工作。

我一直在尝试以下形式的东西: match 'route' to:resource_controller#run

但没有运气。谢谢。

4

1 回答 1

0

Since you've already created a Rails resource, you'll probably want to follow RESTful conventions as closely as possible for any additional actions you want to route to your controller.

The best way to do is to create a member block and add run as a resource:

resources :resource_name do
    member do
        get 'run'
    end
end

This creates a route like this (from rake routes):

run_resource_name GET    /resource_names/:id/run(.:format)                   resource_names#run

Overall, this is considered preferable to a routing pattern like resource/run/:id_number since it follows the conventions of your CRUD routes.

于 2013-06-10T22:50:28.550 回答