Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Use of Render option in Ruby on Rails

    • 0
    • 1
    • 1
    • 1
    • 0
    • 0
    • 0
    • 0
    • 523
    Comment on it

    render() method in rails is used for rendering a content to the view . Its uses are:

    In Controller for rendering an action Example:

    render :action => "show"
    

    This is the most common use of render in controller . The template for show will be rendered without any redirect to the action show within the same controller.

    Sometimes we need to render to the action within same controller with a custom layout. Then we do it in this way:

        render :action => "show" , layout: admin
    

    We can do the same thing as :

        render :template => "user/show" , layout: admin
    

    This is known as template rendering. One thing we need to care here is the path of the template should be relative to its own root directory.

    Rendering partial in a view page Example:

        <%= render :partial => social_share, :locals => {blog: @blog} %>
    

    For details on render partial go to this link .

    Rendering file:

    File rendering works similarly to action rendering. Here the file path is file system path and if layout is not mentioned then file is rendered without any layout.

    Example:

    render :file => "/home/ubuntu/Railsapp/app/views/profiles/template.erb", :layout => true, :status => 404
    
    

    In the above example status 404 is sent while rendering the file with the current layout.

    Render xml and json data

        render :xml => {:pin => "248001"}
    
    
    If we want to pass the same as json data
    
        render :json => {:pin => 248001}
    
    

    Rendering text If we want render text from controller then we can do it like this :

        render :text => "Hi how r u!", :layout => true
    
    In rails 4.1 and above render :text => is deprecated . This is written as:
    
        render :plain => "Hi how r u!", :layout => true #content type text/plain
        render :html =>  "Hi how r u!", :layout => true #content type html/plain
    

    Render inline

    Using render inline we can render any text to a template without the physical existence of the template in the application. For example:

        def welcome
          render :inline => "Hi how r u <%= name %>!", :locals => { :name => "Jonathan" }
        end
    

    This action has no template, but if we send a request to this action from browser then "Hi how r u Jonathan !" will be displayed in the browser. Default format of template is erb. If we want in xml format then :

        render :inline => "xml.p { 'Good to see you back!' }", :type => :builder
    

 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: