Blog Archives

Routes in Rails ( Part- 2)

Hello guys, I am back with my new blog regarding routes in rails. I guess you guys have read my first blog on routes. Here I am going to discuss regarding the advance routes in ROR using some examples

Example1: I have a CategoriesController and ProductsController. In my models following associations are there.

Category model
has_many :products

Product model
belongs_to :category

So here, I want to do all the operations in the products but for that I need the category_id for this. For this I need to have nested resources concept in my routes.

resources :categories do
resources :products
end

Upon doing CONTROLLER=’products’ rake routes it gives the following outputs

category_products GET /categories/:category_id/products(.:format) products#index

POST /categories/:category_id/products(.:format) products#create

new_category_product GET /categories/:category_id/products/new(.:format) products#new

edit_category_product GET /categories/:category_id/products/:id/edit(.:format) products#edit

category_product GET /categories/:category_id/products/:id(.:format) products#show

PUT /categories/:category_id/products/:id(.:format) products#update

DELETE /categories/:category_id/products/:id(.:format) products#destroy

If you mark all these routes, you can find that every route contaings the :category_id through which we can deal with the products of a particular category.

Example2: In my application, I have admin user who can have separate roles and I want to separate all the controllers those will be used for the admin scope. So here namespace concept is being introduced. In this example, I am adding UsersController in the admin namespace.

namespace :admin do

resources :users

end

Upon doing the CONTROLLER=’admin/users’ rake routes, it gives the following routes

admin_users GET /admin/users(.:format) admin/users#index

POST /admin/users(.:format) admin/users#create

new_admin_user GET /admin/users/new(.:format) admin/users#new

edit_admin_user GET /admin/users/:id/edit(.:format) admin/users#edit

admin_user GET /admin/users/:id(.:format) admin/users#show

PUT /admin/users/:id(.:format) admin/users#update

DELETE /admin/users/:id(.:format) admin/users#destroy

Example 3: In this example, we have a situation like the ProductsController will be in the admin folder (namespace) but we don’t want the admin prefix in the routes for it. So here is the following code.

scope module: "admin" do

resources :products

end

Upon doing the CONTROLLER=’admin/products’ rake routes, it gives the following routes

products GET /products(.:format) admin/products#index

POST /products(.:format) admin/products#create

new_product GET /products/new(.:format) admin/products#new

edit_product GET /products/:id/edit(.:format) admin/products#edit

product GET /products/:id(.:format) admin/products#show

PUT /products/:id(.:format) admin/products#update

DELETE /products/:id(.:format) admin/products#destroy

 

Example 4: Here we have a situation. Our categories controller is under development mode and we want that only the users whose having ip like 192.168.10.* only access it. So for that we will add a constraints to it.

constraints(ip: /192\.168\.10\.\d+/) do
resources :categories
end

Example 5: In this example, we have a sort action in the products controller and it takes the parameter sort_by. But the sort_by should take the values ‘day’, ‘month’, ‘year’. So we need to add constraints in the routes for it.

match 'products/:sort_by/sort', to: 'products#sort', constraints: {sort_by: /day|month|year/}

By this, we can restrict our routes to take the parameter ‘sort_by’ as ‘day’ or ‘month’ or ‘year’.

That’s all for the routes. Thanks for reading this blog 🙂

Routes in Rails

In this blog, I am just going to discuss what is routes and how can we modify it as per our requirements in the ROR application. In Rails, routes generates the URL and map those to specific controller actions. In the following there are some examples of routes.

Example 1:

I have an users_controller and I need to map all the default actions for it. So here is the syntax for the routes

resources :users

Upon doing rake routes in console it will show all the routes for the application. We can also list the routes for specific controller by using the command CONTROLLER=your-controller-name rake routes in the console.

CONTROLLER=users rake routes

Here is the output in the console. It will list all the routes-helper, its method, url and the controller#action.

users GET /users(.:format) users#index
POST /users(.:format) users#create
new_user GET /users/new(.:format) users#new
edit_user GET /users/:id/edit(.:format) users#edit
user GET /users/:id(.:format) users#show
PUT /users/:id(.:format) users#update
DELETE /users/:id(.:format) users#destroy

Example 2:

If I need only index, new, create actions in my UsersController, then I will add the following code to the routes.

resources :users, : only => [:index, :new, :create]

Upon doing rake routes, I got routes for my 3 actions.

users GET /users(.:format) users#index
POST /users(.:format) users#create
new_user GET /users/new(.:format) users#new

Example 3:

Now I need to exclude the destroy actions in my controller. For that, I can use only as per the example 2 but for that I need to write rest 6 actions. So there is except keyword for this type of scenario.

resources :users, :except => [:destroy]

Here is the output upon doing rake routes.

users GET /users(.:format) users#index
POST /users(.:format) users#create
new_user GET /users/new(.:format) users#new
edit_user GET /users/:id/edit(.:format) users#edit
user GET /users/:id(.:format) users#show
PUT /users/:id(.:format) users#update

Example 4:

In this example, I need to add a custom action count_users in the UsersController. So here is my routes for that

resources :users do
get :count_users, : on => :collection
end

Here is the output upon doing rake routes.

count_users_users GET /users/count_users(.:format) users#count_users
users GET /users(.:format) users#index
POST /users(.:format) users#create
new_user GET /users/new(.:format) users#new
edit_user GET /users/:id/edit(.:format) users#edit
user GET /users/:id(.:format) users#show
PUT /users/:id(.:format) users#update
DELETE /users/:id(.:format) users#destroy

Normally, we use collection on those routes, which do not require any parameter with in the URL.

Example 5:

In this example, my application requires an action, which will show a perticular user name. For that, I have added get_name action. Here I need to pass the user’s id in the URL. So collection won’t help on this. For that I need to add member in routes.

resources :users do
get :get_name, : on => :member
end

Here is the output upon doing rake routes.

get_name_user GET /users/:id/get_name(.:format) users#get_name
users GET /users(.:format) users#index
POST /users(.:format) users#create
new_user GET /users/new(.:format) users#new
edit_user GET /users/:id/edit(.:format) users#edit
user GET /users/:id(.:format) users#show
PUT /users/:id(.:format) users#update
DELETE /users/:id(.:format) users#destroy

So when we require any parameter to pass in the URL, we are using member. Because using this action, we will get the id of the user and do the operation on that particular user.

That’s all for today. I will come back with the nested routes and namespace in my next blog.

Thank you for reading this blog 🙂 .