Ruby on rails rails渲染文件的jQuery选择器

Ruby on rails rails渲染文件的jQuery选择器,ruby-on-rails,ajax,jquery,Ruby On Rails,Ajax,Jquery,我正在用Rails实现Ajax,在运行时添加一篇新文章,而不刷新页面。 问题是我想通过在create.js.erb $('<%=render(:file => "posts/post.html.erb)%>').insertBefore($('.posts').children('div.post-box').first()); 您只需添加escape\uJavaScript方法,如下所示 $('<%= escape_javascript(render(:file =

我正在用Rails实现Ajax,在运行时添加一篇新文章,而不刷新页面。 问题是我想通过在
create.js.erb

 $('<%=render(:file => "posts/post.html.erb)%>').insertBefore($('.posts').children('div.post-box').first());

您只需添加
escape\uJavaScript
方法,如下所示

$('<%= escape_javascript(render(:file => "posts/post.html.erb"))%>').insertBefore($('.posts').children('div.post-box').first());
$(''posts/post.html.erb'))%%>).insertBefore($('.posts').children('div.post-box').first();

您正在使用哪些rails/ruby版本。这段代码是从哪里来的?@Max ruby 1.9.3,rails 3.2.5,我把create操作放上去了,这就是你需要的。看看帮助器方法:link_to_add_fields()和js函数add_fields。所以基本上你是想用AJAX添加一篇新文章?不,这不是我需要的,我需要的只是如何在post.html.erb视图中选择render html??我不确定我是否理解你需要什么。此
$('“posts/post.html.erb)%>')
构造错误。您不能将Ruby与JS混合使用。也不确定控制器应该做什么。。。
$('<%= escape_javascript(render(:file => "posts/post.html.erb"))%>').insertBefore($('.posts').children('div.post-box').first());