简体   繁体   中英

Rails - Render each do block in js partial

How can I render a `.each do block in a js partial in Rails?

In my messages.js.erb file, I currently have:

<% @conversations.each do |convo| %>
    <%= j render "message_listing", convo: convo %>
<% end %>
$('#conversations-body-container').append(
    // APPEND RESULTS OF PREVIOUS .EACH DO RENDER
);

How can I go about appending the first block of code in my HTML using j render?

You could do like below:

<% @conversations.each do |convo| %>
    $('#conversations-body-container').append('<%= j render "message_listing", convo: convo %>');
<% end %>

Or

<% @conversations.each do |convo| %>
    $('<%= j render "message_listing", convo: convo %>').appendTo('#conversations-body-container');
<% end %>

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