Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby on Rails: How to Render Partial in a view via Jquery

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.

like image 430
Jackson Cunningham Avatar asked May 04 '15 03:05

Jackson Cunningham


3 Answers

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

like image 154
Bernie Chiu Avatar answered Nov 19 '22 02:11

Bernie Chiu


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

like image 3
Gagan Gami Avatar answered Nov 19 '22 02:11

Gagan Gami


Try:

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

j is the alias for escape_javascript.

See documentation for escape_javascript helper.

like image 2
Prakash Murthy Avatar answered Nov 19 '22 04:11

Prakash Murthy