简体   繁体   中英

MultiLine Html Code in js file :Ruby on Rails

I want to add text/body/ruby code in .html function but whenever I try to align my text properly by using enter key it is not functioning properly.

$('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 %>');

How can i change this code so that it can expanded over multiple lines for better readibility

use a partial and pass @comments as a collection

$('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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM