Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple string concatenation in rails in view page

I am new to Rails. Can someone please explain to me the concept of string concatenation using variables in view page and the controller?

like image 309
sudhakar Avatar asked Mar 24 '23 17:03

sudhakar


1 Answers

For example :

In controller Code :

def show
   @firstname = 'Test'
   @lastname = 'User'
end

In view page :

Full Name : <%= "#{@firstname}  #{lastname}" %>

For further details Click Here

Scenarios:- If you want to keep two variables on View page and add concatenation for those then use of space is necessary.

View page:

<%
  var string_1 = "With"
  var string_2 = "Rails"

  var addition_1 = string_1 + string_2;
  var addition_2 = string_1 + " " + string_2

%>
<h1> First Addition  -> #{addition_1} </h1>
<h1> Second Addition  -> #{addition_2} </h1>

Output :

First Addition  -> WithRails
Second Addition  -> With Rails 
like image 178
Rubyist Avatar answered Mar 26 '23 06:03

Rubyist