Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to use partials in Rails Applications

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 665
    Comment on it

    As we know that in almost all web applications there is a set of code which has to be executed often in many places.

    Now suppose we have a header and footer which has to be used  all over the project.

    Now if we would write this code of header and footer in all the pages, it would be wastage of memory and nothing else.

    This will also affect the time and space complexity of the project. 

    Now to avoid this kind of situation what we can do is write the code of header and footer in separate files and then include these files in the application.html.erb file so that the code would be reflected in all the pages, as the code written in application.html.erb file is executed first before any other view file.

    These partials are kept in layouts folder of the project and then they can be included by their name in the application.html.erb file.

    Below is a demonstration on how to use partials in your rails application.

    Firstly we need to create a file in views/layouts/_header.html.erb like this

    <nav class="navbar navbar-inverse" style="margin-top:-50px;">
      <div class="container">
        <div class="navbar-header">
          <h1>This is the header!</h1>
        </div>
      </div>
    </nav>

     

    now same way we need to create a file in views/layouts/_footer.html.erb

    <footer class="panel-footer" style="background: #8F9284;">
      <h4 style="text-align: center;color: white;">
        copyright @ Chayan Sharma. All rights reserved.
      </h4>
    </footer>

     

    Once done with this now all we are left with is including these files in the views/layouts/application.html.erb file like this

    <!DOCTYPE html>
    <html>
      <head>
        <title>Shopoholic</title>
        <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
        <%= stylesheet_link_tag    'application', media: 'all', 'data-turbolinks-track' => true %>
        <%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
        <%= csrf_meta_tags %>
        
        <%= render 'layouts/header'%>
      
      </head>
      
      <body>
        <%= yield %>
      
        <%= render 'layouts/footer'%>
      
      </body>
    </html>
    

    Now, when you will run your project in the browser you will be able to see the header and footer throughout your project in all the pages.

    Hope this blog was helpful to understand that what partials actually are and how to use them.

 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: