Template Rendering And Redirecting In Ruby On Rails

Introduction

I assume you have already read my previous article on Generating Controller and Views In Ruby on Rails. Now, you will learn template rendering and redirecting in Ruby on Rails.

Requirements

  • Software - Ruby on Rails Installed In Your System.
  • Editor- Atom Text Editor Is The Best In Class.
  • Browser – Google Chrome Web Browser.

Rendering Template

The template file is located in following directory in your application file in app >> Views >> home.It will using for add dynamic content in output page.

 

Add the template in that folder file >> new file and save into folder sample>>app>>views>>home>>index2.html.erb.

Now, we can see two different templates one index.html.erb.

 

Another index2.html.erb

 

Now we can't access index2.html.erb file so add simple match route in route.rb file using following code.

Route code

  1. Get ‘home/index’  

 

After add that code in route.rb file.now check the browser.

Note

Before you opening application in browser you will start rails server first,

 

Now ,open the browser and search localhost:3000/home/index.

 

Now check second index file via browser localhost:3000/home/index2.

 

Now we open controller file in app directory>>controllers>>home_controller.rb.

 

After open that controller file we can see the action code is focused on index only but it doesn't specify what template should be we used.

  1. class HomeController < ApplicationController      
  2. layout false      
  3. def index;      
  4. end      
  5. end    
 
 
Now, following code define index2 template using in application.
  1. class HomeController < ApplicationController      
  2. layout false      
  3. def index;      
  4. end      
  5. def index2;      
  6. end      
  7. end    
 

The code is define action for each template to possible to make without returning an error.

Now we see the common way to rendering template action to close default rails rendering action using render method.

Render template syntax

  1. render(:template => ‘home/index2’)  

In this code render in to home directory, and template name is index2 this is longhand version.

Now we see shorthand version

  1. Render(‘home/index2’)  

This is same process function to longhand version.to specify the template only use this code.

  1. Render(‘index2’)  

Now we implement in application going to controller file and add method using this code.

  1. class HomeController < ApplicationController      
  2. layout false      
  3. def index;      
  4. render('index')      
  5. end      
  6. def index2;      
  7. render('index2')      
  8. end      
  9. end    

above that code we specified action in each template.

 

After the code is pasted in file, now check the browser localhost:3000/home/index.above the code we request “index” template but it renders action going to “index2” content in display because the code specified index template rendering into “index2” content in “index” template in browser via render method.

 

Now check the second template in browser localhost:3000/home/index2. above the code we request “index2” template but it renders action going to “index” content in browser because we specified “index2” template rendering into “index” content in index2 template in browser via render method.

 
 
qus: 

where we use this template rendering in application?

Ans:

The template rendering using in login page, if user enter correct password going into the home else it automatically return into login page.

Redirect actions from controller

Controller's is respond to user requests by rendering into the browser.

Now we add new action in controller in application using this code it will redirect into index.html.erb.

  1. class HomeController < ApplicationController  
  2. layout false  
  3. def index;  
  4. render('index2')  
  5. end  
  6. def index2;  
  7. render('index')  
  8. end  
  9. def goto_index;  
  10. end  
  11. end  
 
After we add action code in controller then adding routes in route.rb file.like this
  1. Rails.application.routes.draw do root 'home#index'  
  2. get 'home/index'  
  3. get 'home/index2'  
  4. get 'home/goto_index'  
  5. end   
 

Above that code is added into controller file now it will redirect into goto_index.

  1. class HomeController < ApplicationController  
  2. layout false  
  3. def index;  
  4. render('index2')  
  5. end  
  6. def index2;  
  7. render('index')  
  8. end  
  9. def goto_index;  
  10. redirect_to(: action => 'index')  
  11. end  
  12. end   

 

After the code is pasted in file now check the browser localhost:3000/home/goto_index it will automatically redirect into index.html page. because that is controller function.
 
 
Above the result page we search localhost:3000/home/goto_index page.but that address is redirect into root page locahost:3000 this is redirecting.
  

Conclusion

I hope you are well understood template rendering and redirecting in Ruby on Rails application in this article. In future articles you will learn Ruby on Rails in step by step. Keep in touch…


Similar Articles