最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Ruby on Rails: How to Render Partial in a view via Jquery - Stack Overflow

programmeradmin5浏览0评论

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.

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.

Share Improve this question asked May 4, 2015 at 3:04 Jackson CunninghamJackson Cunningham 5,0733 gold badges33 silver badges82 bronze badges 2
  • 1 I think your javascript file doesn't have the erb extension in it, if it is then rename from file.js to file.js.erb – Mohammad AbuShady Commented May 4, 2015 at 3:54
  • what's the name of this file anyways? cause it looks like js but you added .html_safe which is ruby, and you said form partial, so idk. – Mohammad AbuShady Commented May 4, 2015 at 3:59
Add a ment  | 

3 Answers 3

Reset to default 6

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

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

Try:

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

j is the alias for escape_javascript.

See documentation for escape_javascript helper.

发布评论

评论列表(0)

  1. 暂无评论