Adding Custom Route to Rails app

前端 未结 2 966
不知归路
不知归路 2021-02-02 00:55

I have read up on the Rails Guides.

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

GET profiles/charities

2条回答
  •  星月不相逢
    2021-02-02 01:46

    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. Resource Routing: the Rails Default - Controller Namespaces and Routing
    2. Non-Resourceful Routes - Naming Routes

提交回复
热议问题