<%=if%> of erb does not work properly in render partial in ruby on rails actioncable

Asked 1 years ago, Updated 1 years ago, 74 views

Currently, we are creating dynamic chat using actioncable on ruby on rails.
The chat function is not sufficient, but the function is almost complete.
When you think of sending messages like LINE and receiving messages as right and left,
The messages pulled from the database are divided into right and left, but when entered, they are inserted into the database, but the other party is not notified, and the other party's screen remains unchanged, and the message does not appear.
What's the code when you run it?

MessageBroadcastJob

class MessageBroadcastJob<ApplicationJob
  queue_as —default

  def perform(message)
    if message.user_id.nil?
      # Stream to room_channel if there is no broadcast destination for all chat.
      ActionCable.server.broadcast "room_channel", message:render_message(message)
    else
      # The streamroom channel ends with a user ID and is broadcast when the destination is specified.
      # Stream to the sender
      ActionCable.server.broadcast "room_channel#{message.user_id}", message:render_message(message)
      # Stream to yourself
      ActionCable.server.broadcast "room_channel#{message.userid}", message:render_message(message)
    end
  end

  private
    defrender_message(message)
      ApplicationController.render.render(partial: 'messages/message', locals: {message:message})
    end
end

※ userid —My userID user_id —The other userID
  I'm sorry it's hard to understand.

view is the following code:

room.html.erb

<h1><%=Message to @user_id_to.name%>

        <divid="messages">
          <%=render@messages%>
        </div>

        <form>
          <label>Say something:</label><br>
          <input type="hidden" value=<%=@user_id_to%>data-behavior="room_speaker">
          <input type="text" data-behavior="room_speaker">
        </form>

render destination
_message.html.erb

<%cache message do%>
<div class="message">
    <%if message.userid==current_user.id%>
    <p class="right_balloon">
    <%else%>
    <p class="left_balloon">
    <%end%>
    <%=message.content%>
    </p>
</div>
<%end%>

However, if you set the render destination to the following code, it could be sent and received dynamically.

<%cache message do%>
<div class="message">
    <p class="left_balloon">
    <%=message.content%>
    </p>
</div>
<%end%>

We look forward to hearing from you about your comments.
I am sorry for the long sentence, but I appreciate your cooperation.

ruby-on-rails erb

2022-09-30 21:22

1 Answers

As verified, there seems to be a problem with _message.html.erb.
So if you're just looking at that file:

<div class="message">
    <%if message.userid==current_user.id%>
    <p class="right_balloon">
      <%=message.content#Additional %>
    </p><!--- Add -->
    <%else%>
    <p class="left_balloon">
      <%=message.content#Additional %>
    </p><!--- Add -->
    <%end%>
    <%=#message.contentDelete%>
    <!--</p>Delete-->
</div>

Also, if you write the closing tag together for each branch, it will work.

Alternatively, you may be able to write if with fewer lines as follows.

<div class="message">
  <%class_name=message.userid.eql?(current_user.id)?'right_balloon':'left_balloon'%>
  <%=content_tag(:p,message.content,class:class_name)%>
</div>

I hope it will be helpful.


2022-09-30 21:22

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.