Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
  • Routes in Rails using Objects

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 305
    Comment on it

    Hi friends,

    Whenever a request comes to a server, the first thing comes into picture is routes, that tells the request about which controller's action will respond to that request. When I started creating my first application one thing always confuses me that whenever I write

    redirect to @objectName

    It redirects to the show action of the corresponding object's controller. Then I realized that rails allows you create routes with the help of the objects. For example suppose we have a route like this in our routes.rb

    resources :categories do
      resources :blogs

    If you run rake routes for the following, you will get something like:

    category_blogs GET    /categories/:category_id/blogs(.:format)                    blogs#index
                             POST   /categories/:category_id/blogs(.:format)          blogs#create
           new_category_blog GET    /categories/:category_id/blogs/new(.:format)      blogs#new
          edit_category_blog GET    /categories/:category_id/blogs/:id/edit(.:format) blogs#edit
               category_blog GET    /categories/:category_id/blogs/:id(.:format)      blogs#show
                             PATCH  /categories/:category_id/blogs/:id(.:format)      blogs#update
                             PUT    /categories/:category_id/blogs/:id(.:format)      blogs#update
                             DELETE /categories/:category_id/blogs/:id(.:format)      blogs#destroy
                  categories GET    /categories(.:format)                             categories#index
                             POST   /categories(.:format)                             categories#create
                new_category GET    /categories/new(.:format)                         categories#new
               edit_category GET    /categories/:id/edit(.:format)                    categories#edit
                    category GET    /categories/:id(.:format)                         categories#show
                             PATCH  /categories/:id(.:format)                         categories#update
                             PUT    /categories/:id(.:format)                         categories#update
                             DELETE /categories/:id(.:format)                         categories#destroy

    From here the magic of rails comes, that allows you to pass the object in place of its numeric id to create the route. So suppose you want to open the show of a particular blog, you can write like this:

    <%= link_to 'Blog Detail', category_blog_path(@category, @blog) %>
    ## It will simulate to


    There are so many other ways of specifying the routes like:

    a) Using just the object in link:

    You can just use the single object and it will understand its path like this:

    <%= link_to 'Category Detail', @category %>

    Using the category object rails will fetch the category_path and will pass the id of the current object to simulate:


    b) Using url_for:

    url_for helper can also be used with a set of objects in the hierarchy, to determine the route like this:

    <%= link_to 'Blog Detail', url_for([@category, @blog]) %>
    ## It will fetch category_blog_path from the above url_for helper and will simulate the url like

    c) without using url_for:

    Rails is so smart that even if you don't use url_for and pass the object in array it will fetch the same url:

    <%= link_to 'Blog Detail', [@category, @blog] %>
    ## It will fetch category_blog_path from the above url_for helper and will simulate the url like

    d) Using methods:

    You can also pass methods of edit or update to specify the routes associated for that purposes like this:

    <%= link_to 'Edit Blog', [:edit, @article, @blog] %>

    Hope you like this blog. For more information related to routes in rails. You can visit.

 0 Comment(s)

Sign In

Sign up using

Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: