Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails routes: Nested, Member, Collection, namespace, scope and customizable

I am trying to understand more about Rails routes.

Member and Collection

  # Example resource route with options:
     resources :products do
       member do
         get 'short'
         post 'toggle'
       end

       collection do
         get 'sold'
       end
     end

Namespace and Scope

  # Example resource route within a namespace:
     namespace :admin do
       resources :products
     end

     scope :admin do
       resources :products
     end

Constraints, Redirect_to

# Example resource route with options:
 get "/questions", to: redirect {|params, req| 
     begin
       id = req.params[:category_id]
       cat = Category.find(id)
       "/abc/#{cat.slug}"
     rescue
       "/questions"
     end
 }

Customization:

resources :profiles

original url from resource profiles for edit.

http://localhost:3000/profiles/1/edit

I want to make it for users available only through click edit profile and see url like in below.

http://localhost:3000/profile/edit

Also, is there advanced routing, How most big companies design their routes in rails ? I would be really glad to see new kind of routes if there exist.

Thank You !

like image 320
7urkm3n Avatar asked Apr 03 '16 10:04

7urkm3n


People also ask

How many types of routes are there in Rails?

Rails RESTful Design which creates seven routes all mapping to the user controller. Rails also allows you to define multiple resources in one line.

What is namespace in Rails routes?

This is the simple option. When you use namespace , it will prefix the URL path for the specified resources, and try to locate the controller under a module named in the same manner as the namespace.

What are Rails routes?

In Rails, a resourceful route provides a mapping between HTTP verbs and URLs to controller actions. By convention, each action also maps to a specific CRUD operation in a database. A single entry in the routing file, such as: resources :photos Copy.


2 Answers

**Collection & Member routes**
  • A member route requires an ID, because it acts on a member.

  • A collection route doesn't require an ID because it acts on a collection of objects

:member creates path with pattern /:controller/:id/:your_method

:collection creates path with the pattern /:controller/:your_method

For example :

map.resources :users, :collection => { :abc => :get } => /users/abc
map.resources :users, :member => { :abc => :get } => /users/1/abc

**Scopes & Namespaces routes**

namespace and scope in the Rails routes affect the controller names, URIs, and named routes.

The scope method gives you fine-grained control:

scope 'url_path_prefix', module: 'module_prefix', as: 'named_route_prefix' do
  resources :model_name
end

For Example :

scope 'foo', module: 'bar', as: 'baz' do
  resources :posts
end

produces routes as :

  Prefix Verb         URI Pattern                  Controller#Action
    baz_posts GET    /foo/posts(.:format)          bar/posts#index
              POST   /foo/posts(.:format)          bar/posts#create
 new_baz_post GET    /foo/posts/new(.:format)      bar/posts#new
edit_baz_post GET    /foo/posts/:id/edit(.:format) bar/posts#edit
     baz_post GET    /foo/posts/:id(.:format)      bar/posts#show
              PATCH  /foo/posts/:id(.:format)      bar/posts#update
              PUT    /foo/posts/:id(.:format)      bar/posts#update
              DELETE /foo/posts/:id(.:format)      bar/posts#destroy

The namespace method is the simple case — it prefixes everything.

namespace :foo do
  resources :posts
end

produces routes as :

   Prefix Verb        URI Pattern                  Controller#Action
    foo_posts GET    /foo/posts(.:format)          foo/posts#index
              POST   /foo/posts(.:format)          foo/posts#create
 new_foo_post GET    /foo/posts/new(.:format)      foo/posts#new
edit_foo_post GET    /foo/posts/:id/edit(.:format) foo/posts#edit
     foo_post GET    /foo/posts/:id(.:format)      foo/posts#show
              PATCH  /foo/posts/:id(.:format)      foo/posts#update
              PUT    /foo/posts/:id(.:format)      foo/posts#update
              DELETE /foo/posts/:id(.:format)      foo/posts#destroy

**Constraints & Redirect**

Rails routes are executed sequentially, you can mimic conditional login in the following manner:

match '/route' => 'controller#action', :constraints => Model.new
match '/route' => 'user#action'

The first line checks whether the conditions of the constraint are met (i.e., if the request is emanating from a Model domain). If the constraint is satisfied, the request is routed to controller#action.

We can add constraints to routes for multiple uses like for ip-matching, params matching, restrict format parameter, request-based restrictions etc as :

- ip-matching
   => resources :model, constraints: { ip: /172\.124\.\d+\.\d+/ }
- filtering id params
   => match 'model/:id', to: 'model#show' ,constraints: { id: /\d+/}, via: :get
- restrict format params
   => match 'model/:id', to: 'model#show' ,constraints: { format: 'json' }, via: :get
- request-based constraints
   => get 'admin/', to: 'admin#show', constraints: { subdomain: 'admin' }
like image 154
Muhammad Yawar Ali Avatar answered Sep 28 '22 04:09

Muhammad Yawar Ali


Use a singular resource for it:

resource :profile

and in controller manipulate the profile of current user.

As for complex routes - usually namespaces, nested resources with shallow routes and custom actions are all that is needed.

like image 40
Vasfed Avatar answered Sep 28 '22 05:09

Vasfed