Adding a Custom Route to Rails Application

I read the Rails Guides .

What I want to configure are the following routes that are redirected to the profile controller:

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

I created a profile controller and two methods: charities and donors.

It's all I need?

+6
source share
2 answers

The following are the routes for what you want, but map them to :index and :show from 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 is more appropriate to set up custom routes, something like this:

 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' 

Below are the relevant sections of the guide in which you went:

+12
source

Charities and donors seem to be invested resources. If so, in your config / routes.rb file you should have something like

 resources :profiles do resources :charities resources :donors end 

Since these are invested resources, you do not need two methods called charities and donors in your profile controller. In fact, depending on your application, you may need separate controllers and / or models for your charitable organizations and donors.

+2
source

Source: https://habr.com/ru/post/956036/


All Articles