views:

22

answers:

1

At the moment, I have this run of code:

<%=h current_user.notes.collect { |t| t.name }.join(', ') %>

which outputs this:

note 1, note 2, note 3

How can I change it so that the output looks like this?

"note 1", "note 2", "note 3"

Thanks for reading.

Edit: Here is the full code of KandadaBoggu's suggestion below

$(window).ready(function() {
    $("input#note_name").autocomplete({
        source: [<%=h escape_javascript(current_user.notes.collect { |t| '"%s"' % t.name }.join(', ')) %>]
    });
});

And here is the output HTML:

$(window).ready(function() {
    $("input#note_name").autocomplete({
        source: [\&quot;note 1\&quot;, \&quot;note 2\&quot;]
    });
});
+1  A: 

Try this:

current_user.notes.collect { |t| '"%s"' % t.name }.join(', ')

You can escape the string for javascript:

escape_javascript(current_user.notes.collect { |t| '"%s"' % t.name }.join(', '))

Note If you test the code in a irb console it will print the following output:

"\"note 1\", \"note 2\", \"note 3\""

This is the correct result as the irb console escapes the quotes. The string will be displayed without the \ in a view

KandadaBoggu
Hmm, strange that it works in the console but not in my view. This is my output: "note 1", "note 2"
ben
Sorry, I forgot to mention that I'm running this code in javascript, not sure if that makes a difference?
ben
It should not. Use `escape_javascript` and see if it makes any difference. Refer to my updated answer.
KandadaBoggu
Didn't work again. I've posted the full code and output HTML above. Thanks for your help.
ben
Go it working, just had to remove the h from <%=hThanks again.
ben