从头开始在消息传递系统上显示最后的正文消息



我从零开始在我的应用程序上做了一个消息系统。所有的工作都很好,但我只是想自定义更多。因此,我希望在视图中按user_id inbox显示每个Conversation的最后一条消息。我有两种模式:对话和信息。

我只能显示这个

<标题> 我迷路了。你能帮我吗?

messages_controller.rb

class MessagesController < ApplicationController
 before_action :authenticate_user!
  before_action do
   @conversation = Conversation.find(params[:conversation_id])
  end
def index
 @messages = @conversation.messages
  if @messages.length > 10
   @over_ten = true
   @messages = @messages[-10..-1]
  end
  if params[:m]
   @over_ten = false
   @messages = @conversation.messages
  end
 if @messages.last
  if @messages.last.user_id != current_user.id
   @messages.last.read = true;
  end
 end
@message = @conversation.messages.new
 end
private
 def message_params
  params.require(:message).permit(:body, :user_id)
 end
end

Conversations_controller.rb

class ConversationsController < ApplicationController
  before_action :authenticate_user!
  # GET /conversations
  # GET /conversations.json
  def index
    @users = User.all
    @camping = Camping.all
    # Restrict to conversations with at least one message and sort by last updated
    @conversations = Conversation.joins(:messages).distinct

  end
  private
    # Use callbacks to share common setup or constraints between actions.
    def conversation_params
      params.require(:conversation).permit(:sender_id, :recipient_id, :user_id)
    end
end

conversation.rb

class Conversation < ActiveRecord::Base
 belongs_to :sender, :foreign_key => :sender_id, class_name: 'User'
 belongs_to :recipient, :foreign_key => :recipient_id, class_name: 'User'
 belongs_to :user
  has_many :users, through: :messages

has_many :messages, dependent: :destroy

validates_uniqueness_of :sender_id, :scope => :recipient_id
scope :between, -> (sender_id,recipient_id) do
 where("(conversations.sender_id = ? AND conversations.recipient_id =?) OR (conversations.sender_id = ? AND conversations.recipient_id =?)", sender_id,recipient_id, recipient_id, sender_id)
 end

end

message.rb

class Message < ActiveRecord::Base
 belongs_to :conversation
 belongs_to :user
 validates_presence_of :body, :conversation_id, :user_id

/信息/index.html.erb

 <% @messages.each do |message| %>
  <% if message.body %>
   <% user = User.find(message.user_id) %>
<%= image_tag user.avatar(:thumb), class:"imageavatarmessage" %></div>
<%= user.prenom %>
      <%= message.message_time %>
      <div class="list">
         <%= message.body %>
          <% end %>
         <% end %>
<%= form_for [@conversation, @message], html: {class: "ui reply form"} do |f| %>
   <%= f.text_area :body, class: "form-control" %>
 </div>
 <%= f.text_field :user_id, value: current_user.id, type: "hidden" %>
   <%= f.submit "Add Reply", class: "ui blue labeled submit icon button" %>
<% end %>

问题在这里,我只是想显示每个对话的最后一条消息。/交流/index.html.erb

   <% @conversations.each do |conversation| %>
   <% if conversation.sender_id == current_user.id || conversation.recipient_id == current_user.id %>
    <% if conversation.sender_id == current_user.id %>
      <% recipient = User.find(conversation.recipient_id) %>
    <% else %>
      <% recipient = User.find(conversation.sender_id) %>
    <% end %>
    <p>Conversation avec <%= link_to recipient.prenom, conversation_messages_path(conversation)%></p>
    <p><%= image_tag recipient.avatar(:small), class:"imageavatarmessage" %></p>

   <%= @conversations.last %>

    <p>Il y a <%= time_ago_in_words(conversation.updated_at)%></p>

   <% end %>
  <% end %>

我想在我的/conversations/index.html中只显示每个会话的最后一条消息。我测试了这个<%= @conversations。

你能帮我吗?

编辑

My messages table

:body
:conversation, index: true
:user, index: true
:read, :default => false
t.timestamps

对话表

   :sender_id
  :recipient_id
   t.timestamps
  end
 end
end

编辑2

经过一些测试后,我可以显示最后一条消息。但是这条信息对于所有的对话都是一样的……我不能每次谈话都留最后一条信息。我不知道为什么……

Conversation_controller

 @conversa = Conversation.where("? IN (recipient_id, sender_id)", current_user.id)
     @test = Message.where(conversation_id: @conversa).group(:conversation_id).limit(1)

in my/conversations/index.html.erb

<% @test.each do |message| %>
       <%= message.body %>
 <%end%> 

我想我快找到答案了,你能帮帮我吗?

如果你的current_user可以直接引用他们所在的相关对话,你可以这样设置@conversations变量

def index
  ...
  @conversations = current_user.conversations.joins(:messages)
end

设置好之后你就可以遍历索引视图中的每一个

<% @conversations.each do |conversation| %>
  <div>
    Last Message for Conversation ID:<%= conversation.id %> 
    is <%= conversation.messages.last.body %>
  </div>
<% end %>

使用。count代替。length。

@messages = @conversation.messages
if @messages.count > 10
@over_ten = true
@messages = @messages[-10..-1]
end

或者你可以这样做:

def index    
@messages = Messages.all.order("created_at DESC").limit(10)
end

任何想要拥有最后消息对话列表的人:这是解决方案——我已经在Rails 5.2.6中完成了,通过API,我正在使用Serializer格式化JSON以获得更好的属性。

def conversations_list(user)
 @user= User.find(@user_id)  
 ids = Message.select("MAX(id) AS id")
       .group(:conversation_id).collect(&:id)
 @result = Message.order("created_at DESC").where(:id => ids)
           .joins(:conversation).order(id: :desc)
           .where("sender_id = ? OR recipient_id = ?", user.id, user.id)
           .paginate(page: params[:page],per_page:10)
    
 if @conversations.present?
  render json: {
   success: true, 
   result: ActiveModel::Serializer::CollectionSerializer.new(@result, each_serializer: ConversationSerializer),
  }
 else 
  @conversations= []
  render json: {
   success: true, 
   result: ActiveModel::Serializer::CollectionSerializer.new(@conversations, 
         each_serializer: ConversationSerializer),
     #micros: ActiveModel::Serializesr::CollectionSerializer.new(@micros, 
 each_serializer: MicroSerializer),
  }
 end 
end 

模型
# conversation.rb
  has_many :messages
  belongs_to :user
  belongs_to :sender, :foreign_key => :sender_id, class_name: 'User'
       belongs_to :recipient, :foreign_key => :recipient_id, class_name: 
       'User'
  has_one :last_message, -> { order(created_at: :DESC) },class_name: 
 "Message"
  scope :between, -> (sender_id, recipient_id) do
    where("(conversations.sender_id = ? AND conversations.recipient_id = ?) OR (conversations.sender_id = ? AND conversations.recipient_id = ?)", sender_id, recipient_id, recipient_id, sender_id)
  end
end 
# Message.rb
   belongs_to :conversation
   belong_to :user
   validates_presence_of :body, :conversation_id, :user_id
end 

最新更新