从头开始显示邮件系统上的最后一个正文消息

时间:2016-11-13 15:26:44

标签: ruby-on-rails message messaging

我在我的应用程序上从scractch创建了一个消息传递系统。一切都很好但我只是想定制更多。所以我想在我的视图中通过user_id收件箱显示每个Conversation的最后一条消息。我有2个模型:会话和消息。

我只能显示这个

我失败了。你能救我吗?

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

/messages/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 %>

问题在这里,我只想显示每个对话的最后一条消息。 /conversations/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.erb中仅显示每个会话的最后一条消息我测试了这个&lt;%= @ conversations.last%&gt;但它没有用......

你能帮助我吗?

修改

我的留言表

: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)

在我的/conversations/index.html.erb

<% @test.each do |message| %>

       <%= message.body %>

 <%end%> 

我认为,我接近解决方案,你能帮帮我吗?

3 个答案:

答案 0 :(得分:1)

如果您的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 %>

答案 1 :(得分:0)

使用.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

答案 2 :(得分:0)

任何想要拥有最新消息的对话列表的人: 这是解决方案---我已经在 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