config/routes.rb is the gateway to your ruby on rails application. All request send by your users are directed to the appropriate code by the routes. Example:
match 'profiles', to: 'users#index', via: 'get'When someone visits your-website.com/profiles then the request is taken to the Index action of the UsersController. Under that action you will get the index.html.erb. So using routes we have configured the UsersController to respond to the users request it is its responsibility to do it now. We can declare routes in various ways:
match 'profiles', to: 'users#index', via: 'get' get 'profiles', to: 'users#index', via: 'get' resources :users resource :users, path: 'profiles'Since there are multiple ways to declare routes (as all forms are right), its best to stick with a single method for the code to be more readable. routes.rb is going to be one of the most heavily edited file in your project as when ever you add a new page or create a new form, you need to add a route to access the page or an end-point to accept the request. So it is most likely that your routes.rb file will start to grow ugly:
resources :saved_filters
match "/404" => "errors#error404", via: [ :get, :post, :patch, :delete ], as: 'error404'
devise_for :saas_admins
mount RailsAdmin::Engine => '/saas_admin', :as => 'rails_admin'
%w(about privacy benefits compliance onboarding payroll time_off terms contact aca-compliance).each do |page|
get page => "content##{page.underscore}"
end
get "/not_authorized", to: 'errors#not_authorized'
post '/broker_sign_in_path', to: 'application#broker_sign_in_domain', as: 'broker_sign_in_domain'
post '/employer_sign_in_path', to: 'application#employer_sign_in_domain', as: 'employer_sign_in_domain'
post '/messages/reply_from_external_user',to: 'messages#reply_from_external_user',as: '/messages/reply_from_external_user'
# Routes for the public site
So here we will share some tips to write proper, maintainable routes:
First important point to note is that, its best to write routes as resources
eg:
resources :usersdeclaring resources will create the following 7 routes.
if you don’t want some of these routes, then pass that as options while declaring
resource :users, only: [:create, :update, :show, :destroy]Use nested routes, if you find yourself referring to a child model from parent.
resources :posts do resources :comments endIf you want to declare custom routes, within the resources, the they should only be declared within the resource block. Note: Difference between Member and Collection Member: is used when the routes is meant to work on a single object (eg: DeActivate single user) Collection: is used when the route is meant as an action over a collection of object (eg: search for a user)
resources :users do
member do
match 'de_activate', to: 'user#de_activate', via: 'get'
end
collection do
match 'search', to: 'user#search', via: 'post'
end
end
Always write routes through match rather than defining as post/get, because in future you might have to add multiple HTTP verb for the same route and using match would eleminate multiple lines of the same route.]]>
