首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >向Rails应用程序添加自定义路由

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

Stack Overflow用户
提问于 2013-10-16 10:16:03
回答 2查看 38.3K关注 0票数 12

我读过关于the Rails Guides的文章。

GET profiles/charities -应该显示所有的慈善机构

GET profiles/charties/:id应该展示一个特定的慈善机构

GET profiles/donors -应该显示所有的捐赠者

GET profiles/donors/:id -应显示特定的捐赠者

我已经创建了配置文件控制器和两个方法:慈善机构和捐赠者。

这就是我需要的全部吗?

EN

回答 2

Stack Overflow用户

发布于 2013-10-16 10:39:57

下面将为您想要的内容设置路由,但会将它们映射到CharitiesControllerDonorsController:index:show

代码语言:javascript
复制
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

当设置自定义路由更合适时,可以这样做:

代码语言:javascript
复制
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'

以下是您正在阅读的指南中的相关部分:

  1. Resource Routing: the Rails Default - Controller Namespaces and Routing
  2. Non-Resourceful Routes - Naming Routes
票数 22
EN

Stack Overflow用户

发布于 2013-10-16 11:05:31

慈善机构和捐赠者似乎是嵌套的资源。如果是这样,那么在config/routes.rb文件中应该有如下内容,

代码语言:javascript
复制
resources :profiles do
  resources :charities
  resources :donors
end

因为这些是嵌套的资源,所以在配置文件控制器中不需要名为慈善和捐赠者的两个方法。事实上,根据你的应用程序,你可能需要为你的慈善机构和捐赠者单独的控制器和/或模型。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19394192

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档