Reputation: 873
I'm trying to find and replace some html code using an AJAX call.
The following code in my .js.erb file successfully executes and displays the partial view called 'edit unit' with the resource '@units[0]'.
$("#edit_unit").replaceWith( "<%= escape_javascript(sync partial: 'edit_unit', resource: @units[0]) %>" );
However, when I try to accomplish the same thing, but define a string in the rails controller and pass it through to my .js.erb file, it doesn't execute correctly, i.e.
In my rails controller:
@string = "sync partial: 'edit_unit', resource: @units[0]"
In my .js.erb file:
$("#edit_unit").replaceWith( "<%= escape_javascript(@string) %>" );
When I do it this way the string is displayed as text on the page instead of executing and displaying the partial view.
Why is this happening?
Update:
Eval seems to execute the string successfully. However, it doesn't seem to work in the case when I loop through multiple strings.
$("#edit_delivery_unit_div").replaceWith(
<% @array.each do |string| %>
'<%= escape_javascript(eval(string)) %>'
<% end %>
)
This doesn't return any errors, but also doesn't display the partial views. Any ideas?
Upvotes: 0
Views: 307
Reputation: 17834
String can't be executed, you can use eval
to achieve this, try this out
$("#edit_unit").replaceWith( "<%= escape_javascript(eval(@string)) %>" );
But use this with precaution as you are giving full access to the system
Upvotes: 1