Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails String Interpolation in a string from a database

So here is my problem.

I want to retrieve a string stored in a model and at runtime change a part of it using a variable from the rails application. Here is an example:

I have a Message model, which I use to store several unique messages. So different users have the same message, but I want to be able to show their name in the middle of the message, e.g.,

"Hi #{user.name}, ...." 

I tried to store exactly that in the database but it gets escaped before showing in the view or gets interpolated when storing in the database, via the rails console.

Thanks in advance.

like image 366
Raphael Melo Avatar asked Jul 21 '11 15:07

Raphael Melo


2 Answers

I don't see a reason to define custom string helper functions. Ruby offers very nice formatting approaches, e.g.:

"Hello %s" % ['world'] 

or

"Hello %{subject}" % { subject: 'world' } 

Both examples return "Hello world".

like image 110
Sebastian vom Meer Avatar answered Sep 22 '22 17:09

Sebastian vom Meer


If you want

"Hi #{user.name}, ...." 

in your database, use single quotes or escape the # with a backslash to keep Ruby from interpolating the #{} stuff right away:

s = 'Hi #{user.name}, ....' s = "Hi \#{user.name}, ...." 

Then, later when you want to do the interpolation you could, if you were daring or trusted yourself, use eval:

s   = pull_the_string_from_the_database msg = eval '"' + s + '"' 

Note that you'll have to turn s into a double quoted string in order for the eval to work. This will work but it isn't the nicest approach and leaves you open to all sorts of strange and confusing errors; it should be okay as long as you (or other trusted people) are writing the strings.

I think you'd be better off with a simple micro-templating system, even something as simple as this:

def fill_in(template, data)   template.gsub(/\{\{(\w+)\}\}/) { data[$1.to_sym] } end #... fill_in('Hi {{user_name}}, ....', :user_name => 'Pancakes') 

You could use whatever delimiters you wanted of course, I went with {{...}} because I've been using Mustache.js and Handlebars.js lately. This naive implementation has issues (no in-template formatting options, no delimiter escaping, ...) but it might be enough. If your templates get more complicated then maybe String#% or ERB might work better.

like image 43
mu is too short Avatar answered Sep 19 '22 17:09

mu is too short