Rendering different template or view in Rails

It is possible in Rails framework to call on a different template when you want to call on a different view in Ruby on Rails app. This can be accomplished with the help of “render” being called from within an action which you want to use with a different template.

Here is an example of how this can be accomplished.

class MyCustomController < ApplicationController
 def index
  @title = “my other title”
  render action: ‘my_action’
 end
 def my_action
  @title = ‘my title’
 end
end

You can see that by calling on “render action: ‘my_action’” line of code, index action will direct Rails framework to utilize “my_action.html.erb” template for the view instead of “index.html.erb”. In addition, each view will display different @title variable when call from within templates. Moreover, render only fires after entire action method is finished processing. 

Featured pages

Ruby

Set of Ruby Object Oriented Programming Language tutorials that cover such topics as Ruby strings, …

Rails

Rails Framework tutorial teaches you how to utilize de facto framework of choice for Ruby developme…

Ruby Duck Typing

“If an object quacks like a duck just go ahead and treat it as a duck” – this fun…

Regular Expressions

Ruby uses the =~ operator to check any given string against regular expression. For example, a stri…

Credit Card Number

Every last digit of any credit card is a check sum digit that is determined by all digits in front …

Ruby Arrays

Ruby Programming Language has built in support for Arrays. Arrays help you define some of the compl…

Ruby Hashes

Hashes are very similar to arrays in Ruby and hashes interface is similar to Ruby array interface. …

Ruby Code Block

Ruby is very unique language when it comes to code blocks. You can simply pass a code block to a me…