使用Webpacker
的
我正在为分页和rails-ujs实现一个无限滚动特性。
在初始页面视图中,部分加载良好。它显示结果(每个结果都是从名为“rooms_card.html.haml”的部分呈现的卡片。
将rooms_card部分在index.html.haml页面上呈现为集合。
= render partial: "rooms_card", collection: @rooms, as: :room, cached: true

但是,当我点击‘View’链接时,请求就会被发出,结果会被追加,但是它是原始的html,而不是解释的。

这是链接的代码。
= link_to_next_page @rooms, "View More", class: "view-more-link", remote: true}从我的index.js.haml文件
document.getElementById("rooms-cards").append("#{j render(partial: "rooms/rooms_card", collection: @rooms, as: :room) }")我对障碍的回应。rooms_controller.rb
respond_to do |format|
format.js
format.html
format.json { render json: @results, each_serializer: RoomSerializer }
end如何获得响应在浏览器中呈现而不是显示原始的html?
我一直盯着这一切我知道怎么做。对我错过了什么有什么想法吗?
谢谢!
戴夫
发布于 2020-03-23 07:01:11
如果将String (精确地说是DOMString)对象传递给append方法,它将作为Text节点插入。而是传递一个Node对象。
const wrapper = document.createElement('div');
wrapper.innerHTML = "#{j render(partial: 'rooms/rooms_card', collection: @rooms, as: :room)}"
document.getElementById("rooms-cards").append(wrapper);如果不想创建额外的div,可以使用DocumentFragment。
const fragment = document.createDocumentFragment();
fragment.innerHTML = "#{j render(partial: 'rooms/rooms_card', collection: @rooms, as: :room)}"
document.getElementById("rooms-cards").append(fragment);希望这能有所帮助。
https://stackoverflow.com/questions/60746963
复制相似问题