Answer the question
In order to leave comments, you need to log in
How to load chat on the page after the page is loaded?
There is a page, on the left side is the usual content, on the right side is a chat. I want the chat to load after the page is loaded. Let's say I want to render a page with posts:
class PostsController < ApplicationController
def index
@posts = Post.all
@messages = Message.all # приходится тянуть в контроллер подгрузку сообщений из чата
@message = Message.new # и новое сообщение для создания формы
end
end
Answer the question
In order to leave comments, you need to log in
Here's a fairly simple method, so for example the sketch used setTimeout() to load the comments:
show.html.erb
<div class="d-block" id="comments-show-columns">
<div id="open_comments" class="mt-4">
<div data-controller="comments">
Загрузка комментариев...
<%= link_to url_for(open: 'true'), class: 'hidden', remote: true, data: {target: "comments.opencomment"} do %>
<% end %>
</div>
</div>
</div>
<% if params[:open] %>
var opening = document.querySelector("#open_comments")
var pagin = document.querySelector("#comments-show-paginator")
pagin.classList.toggle("hidden")
opening.innerHTML = ('<%=j render(partial: "comments/comment", collection: @comments, locals: {full: true}, continue_thread: 10) %>')
<% end %>
import { Controller } from "stimulus";
export default class extends Controller {
static targets = ["opencomment"]
connect() {
setTimeout(() => this.opencommentTarget.click(), 500 )
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question