将自定义路线添加到Rails应用 [英] Adding Custom Route to Rails app
问题描述
我已阅读 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:
- Resource Routing: the Rails Default - Controller Namespaces and Routing
- Non-Resourceful Routes - Naming Routes
这篇关于将自定义路线添加到Rails应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!