Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Different Types of Renders in Rails

    • 0
    • 1
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 1.36k
    Comment on it

    render is used to provide response of a request in rails. There are multiple kinds of responses that you can send with request. Lets see one by one:


    1) actions:

    By default if nothing is specified as render, actions are rendered withing in the current layout if they are present. In that case the template of that action gets rendered. If we specify the action in the render, the template of that action will get rendered:

    # Renders the template for the action "index" 
      render :action => "index"
    
    # Renders the template for the action "index", but doesn't use the currently set layout
      render :action => "index", :layout => false
    
    # Renders the template for the action "index", and uses the specified layout
     render :action => "index", :layout => "some_layout"
    

     

    2) partials:

    Partials are used in multiple places, like in ajax calls when you want to update any particular portion of the page or you can also use them in views, to divide a large view into multiple pages.


      a) Rendering with a collection:

      In this case we can pass a collection to a partial and each will iterate over each element of the collection and will generate multiple appended views, i.e. in the example in each of the partial the user object will be available.

    render :partial => "shared/user", :collection => @users

     

    b) Rendering with object:

      In this case the object with the partial name will be available in the partial

    # @some_user will be available in user object
      render :partial => "user", :object => @some_user

     

    c) Using as:

      We can provide custom name for the object

    # @some_user will be available in person object in the partial
      render :partial => "user", :object => @some_user, :as => :person

     

    3) Templates:

    Rendering templates works similarly like the action, it takes the templates directly from the view according to its relative path. instance variables are always available in the partials or templates, you can also pass local variables using locals:

     render :template => "some_template"
    
     render :template => "some_template", :locals => {:some_local_var => "Test"}

     

    4) Files:

    We can also render files in rails, it also takes path relative to the view, in this case, layouts are not applied automatically.

    render :file => "/path/to/some/template.erb"

     

    5) Text, JSON or XML:

    We can render text, json, xml etc using render, depending upon our requirements:

     render :text => "Not Authorized"
    
     render :json => {success: true}
    
     render :xml => {success: true}.to_xml

     

    6) Inline Template:

    We can also render inline templates using render and even pass locals into them

     render :inline => "<%= 'Hi, ' * 3 + 'again' %>"
    
     # renders Hi, Hi, Hi, again
    
    
     render :inline => "<%= 'I am ' + name %>", :locals => { :name => "Sachin" }
    
     # renders I am Sachin

    Hope you enjoyed this 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: