Introduction

I assume you have already read my previous article on Template Rendering and Redirecting In Ruby on Rails. Now, you will learn about the View templates, naming of template files, ERB tags, and how to use ERB tags in HTML templates. We will learn all this via two examples in this article.

Requirements

View templates

Template File Naming

File name - Home.html.erb

Explanation

Template name - Home

Process with - ERB

Output format - HTML

What is ERB tag?

Sample expression tags

<% code %> - non output printing tag.
<%= code %> - output printing tag.

Comment tag

<%# comments %> - it’s used to documentation work only.

Now we can see Two ERB tag examples one by one, I cannot explain ERB tags fully because this ERB tag is using an event, functioning only within Ruby on Rails. We are not using events and functions in this application, so I can’t explain fully in this article but I shal explain futher in an upcoming article.

Step 1

  • After put above the code in Rails command prompt some process going in there, we skip that process, if you have any doubt in application creation refer my previous article Creating application in ruby on rails.

Step 2

Step 3

After completing the file creation now we have to configure the files in our home controller, The 'home_controller.rb' file is stored in our app>>controller folder and now we add rendering codes like in the following picture. This shows code for each page for rendering when the page requests to render it.

Home_controller.rb code
  1. class HomeController < ApplicationController
  2. layout false
  3. def index
  4. render('index')
  5. end
  6. def main
  7. render('main')
  8. end
  9. end

Step 4

After configuring the HTML pages in the home_controller file the rendering process is complete, now we can add the route code in the route.rb file.
  1. Rails.application.routes.draw do
  2. get 'home/index'
  3. get 'home/main'
  4. end



The routing codes are added in route.rb file.

Step 5

To view the newly created HTML template files in our browser go to localhost:3000/home/main for the main page, and for the index page visit localhost:3000/home/index in yoru search bar. Before we open the template please confirm the rails server is ON, if not you can start it using this code in your Rails command prompt.

Rails s

Example 1

  • Now check in the browser by visiting localhost:3000/home/main.

  • Check localhost:3000/home/main in browser.

  • The output printing ERB tag will show output in our HTML template because we used the output printing ERB tag. It will give output in HTML templates using an expression ERB tag.

Example 2

In this example we can use comment tag. This tag will not give output in the browser page. Use the code given below.
  1. <%# target='How are you' %>

  • Now we check in our browser localhost:3000/home/main.


Conclusion

I hope you understood the topics covered, such as viewing templates, the template file naming system, what ERB tags are and how to use ERB tags in the two examples utilizing Ruby on Rails. In future articles you will learn more Ruby on Rails in a step by step easy to learn way. Keep in touch…