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

时光怂恿深爱的人放手 提交于 2020-02-03 04:17:14

问题


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.


回答1:


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




回答2:


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




回答3:


Try:

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

j is the alias for escape_javascript.

See documentation for escape_javascript helper.



来源:https://stackoverflow.com/questions/30022004/ruby-on-rails-how-to-render-partial-in-a-view-via-jquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!