添加自定义路由到Rails应用程序

Sri*_*esh 10 routing ruby-on-rails

我已经阅读了Rails指南.

我想要设置的是以下路由到路由到'profiles'控制器:

GET profiles/charities- 应显示所有慈善机构
GET profiles/charties/:id应显示特定慈善机构
GET profiles/donors- 应显示所有捐赠者
GET profiles/donors/:id- 应显示特定捐赠者

我创建了配置文件控制器和两种方法:慈善机构和捐赠者.

这就是我需要的吗?

kri*_*lim 19

以下将根据您的需要设置路线,但会将它们映射到:index:show:CharitiesControllerDonorsController:

namespace :profiles do
  # Actions: charities#index and charities#show
  resources :charities, :only => [:index, :show]

  # Actions: donors#index and donors#show
  resources :donors, :only => [:index, :show]
end
Run Code Online (Sandbox Code Playgroud)

当设置自定义路由更合适时,这样的事情会:

get 'profiles/charities', :to => 'profiles#charities_index'
get 'profiles/charities/:id', :to => 'profiles#charities_show'
get 'profiles/donors', :to => 'profiles#donor_index'
get 'profiles/donors/:id', :to => 'profiles#donor_show'
Run Code Online (Sandbox Code Playgroud)

以下是您所经历的指南中的相关部分:

  1. 资源路由:Rails默认 - 控制器命名空间和路由
  2. 非资源路由 - 命名路由