8

I have a 'project' form as a partial. I'm trying to use jquery to render the partial when user clicks a button:

$('.projects').append("<%= render partial: 'projects/project') %>").html_safe

But using the above code is literally rendering "<%= render partial: 'projects/project') %>" on the page instead of the actual partial.

Jackson Cunningham
  • 4,973
  • 3
  • 30
  • 80

3 Answers3

6

I believe renaming your file extension from xxx.js to xxx.js.erb might solve your problem.

Bernie Chiu
  • 263
  • 1
  • 11
  • I think Bernie is correct. If the file ends in erb, then rails will render the view. If not, then it just treats it as any other static file. – Tim Holt May 04 '15 at 06:23
3

Try this:

$('#projects').html('<%= escape_javascript render 'projects/project' %>');

Note: As Bernie Chiu suggested you need to change file extension from xxx.js to xxx.js.erb

Gagan Gami
  • 10,121
  • 1
  • 29
  • 55
2

Try:

$('.projects').append("<%= j render partial: 'projects/project') %>");

j is the alias for escape_javascript.

See documentation for escape_javascript helper.

Prakash Murthy
  • 12,923
  • 3
  • 46
  • 74