Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails best way to concatenate a string with a variable in a view

Essentially I'm trying to perform this comparison

request.fullpath == "/contacts/"+current_user.id 

What is the best way to concatenate a string with a variable to perform a comparison like this?

like image 509
Catfish Avatar asked Nov 26 '12 03:11

Catfish


People also ask

How do you concatenate strings and variables?

In JavaScript, we can assign strings to a variable and use concatenation to combine the variable to another string. To concatenate a string, you add a plus sign+ between the strings or string variables you want to connect. let myPet = 'seahorse'; console.

What is the most efficient way to concatenate many strings together?

The most efficient is to use StringBuilder, like so: StringBuilder sb = new StringBuilder(); sb. Append("string1"); sb.

How do you add a variable to a string in Ruby?

Instead of terminating the string and using the + operator, you enclose the variable with the #{} syntax. This syntax tells Ruby to evaluate the expression and inject it into the string.


2 Answers

You can interpolate it as request.fullpath == "/contacts/#{current_user.id}"

like image 180
joofsh Avatar answered Sep 17 '22 13:09

joofsh


"/contacts/#{current_user.id}" 

Always use String interpolation, it's faster than << and +

like image 22
doesterr Avatar answered Sep 17 '22 13:09

doesterr