Rails 3路由:非CRUD控制器

时间:2012-05-07 14:23:26

标签: ruby-on-rails ruby-on-rails-3

我需要一些帮助,将路由从Rails 2转换为Rails 3。

app/views/layouts/application.html.erb中,我有:

<%= link_to "Reports", reports_path %><br>

有一个ReportsController,在app/views/reports/index.html.erb中,我有这个:

<%= link_to "Clients With Animals", :action => "getAnimals", :controller => "clients" %>

然后,在config / routes.rb中,我有了这个(Rails 3)

match '/reports' => "reports#index"
match '/clients/getAnimals', to: "clients#getAnimals"

点击报告页面上的“getAnimals”链接时出现此错误:

ActiveRecord::RecordNotFound in ClientsController#show
Couldn't find Client with id=getAnimals

我不希望“getAnimals”成为ID - 我希望它成为动作,而不是。

我该怎么做?

1 个答案:

答案 0 :(得分:2)

假设您还有一个resources :clients条目,您希望确保match '/clients/getAnimals', to: "clients#getAnimals"高于它(Rails将匹配它首先匹配的任何内容)。

但是,更好的方法是将其放入资源中:

resources :clients do
  get 'getAnimals', :on => :collection
end