js文件中的MultiLine Html代码:Ruby on Rails

时间:2014-07-24 09:43:37

标签: javascript ruby-on-rails ruby

我想在.html函数中添加text / body / ruby​​代码,但每当我尝试使用回车键正确对齐文本时,它都无法正常运行。

$('div.showcomment').html('<% @comments.each do |comment| %><%= comment.body %><%= link_to 'Delete Comment', articles_deletecomment_path(:id =>comment.id ,:articleid=>@article.id), method: :delete, :remote => true,data: { confirm: 'Are you sure?' } %><a onClick="editComment(<%= comment.id%>)">Edit Comment</a></br><% end %>');

如何更改此代码,以便可以扩展多行以提高可读性

1 个答案:

答案 0 :(得分:2)

使用partial并将@comments作为集合传递

$('div.showcomment').html('<%= escape_javascript render(partial: 'comments/comment', collection: @comments, locals: { article: @article } %>')

# app/views/comments/_comment.html.erb
<%= comment.body %>
<%= link_to 'Delete Comment', articles_deletecomment_path(id: comment.id, article_id: article.id), method: :delete, remote: true, data: { confirm: 'Are you sure?' } %>
<a onclick="editComment(<%= comment.id%>)">Edit Comment</a>
<br>