加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – 添加自定义路由到Rails应用程序

发布时间:2020-12-16 21:15:11 所属栏目:百科 来源:网络整理
导读:我已经读过 the Rails Guides了. 我想要设置的是以下路由到路由到’profiles’控制器: GET个人资料/慈善机构 – 应显示所有慈善机构 GET个人资料/图表/:id应该显示一个特定的慈善机构 GET个人资料/捐赠者 – 应显示所有捐赠者 GET个人资料/捐赠者/:id –
我已经读过 the Rails Guides了.

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

GET个人资料/慈善机构 – 应显示所有慈善机构
GET个人资料/图表/:id应该显示一个特定的慈善机构
GET个人资料/捐赠者 – 应显示所有捐赠者
GET个人资料/捐赠者/:id – 应该显示一个特定的捐赠者

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

这就是我需要的吗?

解决方法

以下将根据您的需要设置路由,但会将它们映射到:index和:show of CharitiesController和DonorsController:
namespace :profiles do
  # Actions: charities#index and charities#show
  resources :charities,:only => [:index,:show]

  # Actions: donors#index and donors#show
  resources :donors,:show]
end

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

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'

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

> Resource Routing: the Rails Default – Controller Namespaces and Routing
> Non-Resourceful Routes – Naming Routes

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读