将自定义路线添加到Rails应用 [英] Adding Custom Route to Rails app

查看:98
本文介绍了将自定义路线添加到Rails应用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已阅读 Rails指南

我要设置的是路由到配置文件控制器的以下路由:

What I want to set up are the following routes that are routed to the 'profiles' controller:

获取配置文件/慈善机构-应该显示所有慈善机构

获取个人资料/ charties /:id 应该显示特定的慈善机构

获取个人资料/捐赠者-应该显示所有捐赠者

获取个人资料/捐赠者/:id -应该显示特定的捐赠者

GET profiles/charities - Should display all the charities
GET profiles/charties/:id should display a specfic charity
GET profiles/donors - Should display all the donors
GET profiles/donors/:id - Should display a specfic donor

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

I have created the profile controller and two methods: charities and donors.

这就是我所需要的吗?

推荐答案

以下内容将为您想要的设置路线,但会将它们映射到:index :show CharitiesController DonorsController

The following will set up routes for what you want, but will map them to :index and :show of CharitiesController and DonorsController:

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

何时更适合设置自定义路线,则可以这样进行:

When it's more appropriate to set up custom routes, something like this would do:

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'

此处是您正在阅读的指南中的相关部分:

Here are relevant sections in the guide that you were going through:


  1. 资源路由:Rails的默认设置-控制器的命名空间和路由

  2. 非资源路由-命名路由

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

这篇关于将自定义路线添加到Rails应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆