Max Rose-Collins
Max Rose-Collins

Reputation: 1924

Ruby on Rails, rendering a partial using jquery is just displaying the text

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?

Upvotes: 1

Views: 193

Answers (1)

user229044
user229044

Reputation: 239291

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.

Upvotes: 2

Related Questions