Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails options_for_select in js.erb not being rendered

I have a js.erb with this basic content:

$("#currencies").html("<%= options_for_select([['Dollar', '$']])%>");

And the html is not being added to the element #currencies.

However if I do it like this:

$("#currencies").html('<option value="$">Dollar</option>');

The html is being updated.

What can be happening?

For debugging purposes I have tried this:

alert("<%= options_for_select([['Dollar', '$']]) %>");

And it doesn't show anything either.

like image 794
Hommer Smith Avatar asked Nov 28 '25 08:11

Hommer Smith


1 Answers

You need to escape javascript with <%=j .. %>

<%=j options_for_select .. %>

or

<%= escape_javascript(..) %>

or

$('some_element').replaceWith('<%=j render 'some/element_template' %>');

from api documentation

like image 115
Alper Karapınar Avatar answered Nov 30 '25 21:11

Alper Karapınar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!