0

I have below piece of code in results.js.erb of Rails application where I need to render the partial URL dynamically using the string interpolation concept. I was unable to interpolate selector value dynamically in the render. Any help in resolving this issue is much appreciated. Thanks in advance.

var selector =  = 'rc'  // possible values rc,mc.vc....
$("div.enroll-item div." + selector + "-item").empty().html('<%= j render "test/health_check/#{selector}_check", text_helper: text_helper  %>');
Rocky
  • 129
  • 2
  • 12
  • 1
    `var selector` defines javascript variable. the `selector` local variable you are referencing does not defined as per above snippet. Do you encounter any error? – Amit Patel Apr 03 '20 at 13:32
  • @AmitPatel I want to use the Javascript variable in the render instead of the ruby variable. I tried it as #{selector} ruby local variable but it is showing a local variable not defined. Is there an another way of adding javascript variable dynamically – Rocky Apr 04 '20 at 05:44

1 Answers1

0

You can instead define a ruby variable to be used while the results.js.erb file is being rendered.

<% ruby_selector = 'rc' %>
var selector =  '<%= ruby_selector %>'
$("div.enroll-item div." + selector + "-item").empty().html('<%= j render "test/health_check/#{ruby_selector}_check", text_helper: text_helper  %>');
Mahmoud Sayed
  • 668
  • 5
  • 18