2

This passes the string productID instead of the variable which == 5. How can I pass the variable, not the string?

<script>
$(document).ready(function(){
    $(".test").click(function(){
      var productID = 5;
      $("#productModal").html("<%= escape_javascript(render 'layouts/modal', :product_id => "productID").html_safe %>"); 
      $("#modal").modal();
    });
});
</script>

also tried:

$("#productModal").html("<%= escape_javascript(render 'layouts/modal', :product_id => " + productID + ").html_safe %>");

This also just outputs + productID + instead of 5

3
  • As i understand you assign productID on frontend, but you try to render erb on backend with variable you assigned on frontend. Server render it first, and it does not execute any html/javascript, so it's not aware of your variable. It's unclear what you try to achieve, but the way you do it with this code is not possible. Sorry. Probably if you would clarify what you try to do i could give proper answer. Commented Dec 10, 2015 at 9:34
  • @Fedcomp I am trying to render a bootstrap modal depending upon which product a user clicks on. The modal should vary depending upon the product_id. Commented Dec 10, 2015 at 9:39
  • You should put bootstrap modal window with prerendered html and visible: hidden on the page html and then use bootstrap method to make it popup. You should only use javascript here to actually trigger it. Erb will render it alone, without any necessity of javascript. Commented Dec 10, 2015 at 9:42

1 Answer 1

2

If you wanted to fix the code you've provided, you'd be best doing the following:

<script>
$(document).ready(function(){
    $(".test").click(function(){
      var productID = 5;
      $("#productModal").html("<%=j render('layouts/modal', product_id: " + productID + ").html_safe %>"); 
      $("#modal").modal();
    });
});
</script>

However there are several big problems with this code.


Unobtrusive

Firstly, why are you using <script> tags?

This would suggest you've put this inside a view, which is very bad practice.

The most effective way is to use the unobtrusive javascript pattern, which is what the Rails asset pipeline is able to support:

#app/assets/javascripts/application.js
$(document).on("click", ".test", function(e) {
    var productID = 5;
    $("#productModal").html(productID); 
    $("#modal").modal();
});

This not only means you're able to use this code throughout your application (modularity), but also ensures you're able to call the correct

--

Accessibility

Secondly, you'll notice I have excluded the escape_javascript code above.

This is because the asset pipeline does not handle ERB / Rails code.

Javascript is client-side; Rails is server-side.

This is important, as when you call render etc - your javascript will not be able to do this unless it's called from the server, especially if the partial requires a variable which is set on DOM load.

For this, you'll need to use ajax:

#config/routes.rb
get "modal", to: "application#modal"

#app/controllers/application_controller.rb
class ApplicationController < ActionController::Base
   def modal
      @productID = params[:product_id]
      respond_to do |format|
         format.js { render "layouts/modal", product_id: @productID }
      end
   end
end

#app/assets/javascript/application.js
$(document).on("click", ".test", function(e) {
   var productID = 5;
   $.get("modal", {product_id: productID}, function(data) { 
      $("#productModal").html(data); 
      $("#modal").modal();
   });
});

A small caveat here is that if you append .erb to the JS file (javascripts/application.js.erb), you'll be able to use render. However, this can only happen when the file is invoked; thus passing any variable to the partial will not work.


Update

Here's what worked in the end:

#app/assets/javascripts/application.js
$(document).on("click", ".testinggg", function(e) { 
   var productID = 5; 
   $.get("modal", {product_id: productID}, function(data){ 
     $("#productModal").html(data); 
     $("#modal").modal(); 
   }, "html"); 
});
Sign up to request clarification or add additional context in comments.

7 Comments

Thanks for the suggestions. I will read through it carefully. However the fix you provided for the current code still just passes + productID +, not 5.
Hmm okay. If you read through the rest, I'll make an update for you
getting error after clicking: GET http://localhost:3000/modal?product_id=5 404 (Not Found)
Did you add the route?
hmm - can you do rake routes to see what the route is called please?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.