1

I am using the following jQuery to render a partial when a button is clicked

$(document).ready(function() {
  $(".service-type").click(function(e) {
    e.preventDefault();
    $('#details').html("<%= escape_javascript( render(:partial => 'my_partial',
        :locals => { f: f })) %>");
  });
});

but instead of it displaying the partial it inserts the following into my page

<%= escape_javascript( render(:partial => "my_partial", :locals => { f: f })) %>

Any ideas where I am going wrong?

5
  • You need to process that file with ERB, it looks like you're probably serving it out of the asset pipeline instead. If you give us more information on where that code is and what its file is named, we could offer more help. Commented Feb 27, 2014 at 16:07
  • Also, just render 'my_partial', f: f. You haven't needed the :partial/:locals since Rails 2.3. Commented Feb 27, 2014 at 16:07
  • the file is named _my_partial.html.erb Commented Feb 27, 2014 at 16:08
  • Not the partial, the file that contains that JavaScript. Commented Feb 27, 2014 at 16:09
  • Oh okay, sorry. The JS is in assets/javascripts/tasks.js Commented Feb 27, 2014 at 16:10

1 Answer 1

2

The JS is in assets/javascripts/tasks.js

You need to move your javascript into whichever file defines your form. You can make your file evaluate the embedded Ruby by adding a .erb extension, but that isn't going to help, as the form builder (f) doesn't exist in the context of this request.

You should move that blob of JavaScript directly into your view, probably inside your form_for block, so that it has access to the variable f, assuming f is a FormBuilder.

Sign up to request clarification or add additional context in comments.

Comments

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.