Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Responses In Rails: Part 1

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 461
    Comment on it

    Hi Friends,

    As we all know rails works on MVC Architecture, where the controllers acts as the receiver of the requests and sends the response back to the requester. There are actually 3 ways a controller can send a response :

    a) render: It sends a full response to the requester
    b) redirect_to: It sends and HTTP redirect status code to requester
    c) head: It just replies back with head status code with no response

    Now lets look at each one of them in detail. First lets talk about:

    a) Using Render:

    Render in rails gets called in two ways. One is by default which is without specifying it in controller and the other is manually specifying it. In case of default, whenever their is an action in a controller and there is nothing like render or redirect_to is specified in the action the view of the same actions name gets rendered.

    For example If we have a controller like this:

    # app/controllers/products_controller.rb
    
    class ProductsController < ApplicationController
      def new
      end
    end
    

    Now if new action gets called automatically a view app/views/products/new.html.erb will be rendered.


    The second way of using render is by explicitly specifying render. So suppose you want to render some data or some view which is not named as actions name, then you can manually specify that in the action like this:

    # app/controllers/products_controller.rb
    
    class ProductsController < ApplicationController
      def new
        render 'new_page'
      end
    end

    In this case app/views/products/new_page.html.erb will get rendered.

     

    b) Using redirect_to:

    It is another way that handles the HTTP requests. As its names suggests, it tells the browser to hit a new request to a different URL. Suppose a non logged in user want to hit a protected action which should be accessible only to a logged in user, then the action redirects the browser to the login url like this:

    redirect_to login_url

     

    c) Using head:

    Head method sends only headers in the response to the browser, it accepts either a number or a symbol of status. The options argument is interpreted as a hash of header names and values. For example suppose an api for signout return only status code, then we can use head in action for sending response:

    head 204

    This would produce the following header: 

    HTTP/1.1 400 No Content
    Connection: close
    Date: Mon, 01 Aug 2016 10:10:00 GMT
    Transfer-Encoding: chunked
    Content-Type: text/html; charset=utf-8
    X-Runtime: 0.065451
    Set-Cookie: _blog_session=...snip...; path=/; HttpOnly
    Cache-Control: no-cache

     

    For sending response with head :

    head 204, location: users_path(@user)

    It will produce:

    HTTP/1.1 400 No Content
    Connection: close
    Date: Mon, 01 Aug 2016 10:10:00 GMT
    Transfer-Encoding: chunked
    Location: /users/1
    Content-Type: text/html; charset=utf-8
    X-Runtime: 0.065451
    Set-Cookie: _blog_session=...snip...; path=/; HttpOnly
    Cache-Control: no-cache
    

    Hope you liked reading this blog. We will talk more about rendering in my next blog.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
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: