2

我创建了一个收件箱消息系统,它运行良好。但是我不知道如何为消息实现上一条和下一条功能(因此用户可以在查看一条消息时转到下一条或上一条消息)。这些是在 Internet 上查看消息的正常功能。上一个应该是消息创建时间之前的意思。任何帮助,将不胜感激。

消息控制器:

  before_filter :set_user

  def index
    if params[:mailbox] == "sent"
      @messages = @user.sent_messages
    elsif params[:mailbox] == "inbox"
      @messages = @user.received_messages
    #elsif params[:mailbox] == "archieved"
     # @messages = @user.archived_messages
    end
    if params[:mailbox] == "unread"
    @messages = @user.unread_messages
  end
  end

  def new
    @message = Message.new
    if params[:reply_to]
      @reply_to = User.find_by_user_id(params[:reply_to])
      unless @reply_to.nil?
        @message.recipient_id = @reply_to.user_id
      end
    end
  end

  def create
    @message = Message.new(params[:message])
    @message.sender_id = @user.id
    if @message.save
      flash[:notice] = "Message has been sent"
      redirect_to user_messages_path(current_user, :mailbox=>:inbox)
    else
      render :action => :new
    end
  end

  def show
     @message = Message.find(params[:id])
     @message.readingmessage if @message.recipient == current_user
   end

   def destroy
     @message = Message.find(params[:id])
     @message.destroy
     flash[:notice] = "Successfully deleted message."
     redirect_to user_messages_path(@user, @messages)
   end

  def delete_multiple
      if params[:delete]
        params[:delete].each { |id|
          @message = Message.find(id)
          @message.mark_message_deleted(@message.id,@user.id) unless @message.nil?
        }
        flash[:notice] = "Messages deleted"
      end
      redirect_to user_messages_path(@user, @messages)
  end

  private
    def set_user
      @user = current_user
    end
end

消息模型:

attr_accessible :subject, :body, :sender_id, :recipient_id, :read_at,:sender_deleted,:recipient_deleted
    validates_presence_of :subject, :message => "Please enter message title"

    belongs_to :sender,
    :class_name => 'User',
    :foreign_key => 'sender_id'
    belongs_to :recipient,
    :class_name => 'User',
    :foreign_key => 'recipient_id'

    # marks a message as deleted by either the sender or the recipient, which ever the user that was passed is.
    # When both sender and recipient marks it deleted, it is destroyed.
    def mark_message_deleted(id,user_id)
         self.sender_deleted = true if self.sender_id == user_id
         self.recipient_deleted = true if self.recipient_id == user_id
         (self.sender_deleted && self.recipient_deleted) ? self.destroy : self.save!
     end
    # Read message and if it is read by recipient then mark it is read
    def readingmessage
      self.read_at ||= Time.now
      save
    end

    # Based on if a message has been read by it's recipient returns true or false.
    def read?
        self.read_at.nil? ? false : true
    end

    def self.received_by(user)
       where(:recipient_id => user.id)
     end

     def self.not_recipient_deleted
       where("recipient_deleted = ?", false)
     end

     def self.sent_by(user)
        Message.where(:sender_id => user.id)
      end

    end

show.html(消息视图):

    <strong>From:</strong> 
    <%= @message.sender %>
</p>

<p>
    <strong>Received:</strong> 
    <%= @message.created_at.to_s(:long) %>
</p>
<p>
    <strong>To:</strong> 
    <%= @message.recipient %>
</p>

<p>
    <strong>Message</strong><br />
    <%=h @message.body %>
</p>
<p>
    <% if @message.recipient == @user %>

    <%= link_to "Reply", new_user_message_path(@user, :reply_to => @message.sender) %>

     |
    <% end %>
    <%= link_to "Inbox", user_messages_path(current_user, :mailbox=>:inbox)%>
    |
    <%= link_to "Delete", [current_user, @message], :confirm => 'Are you sure you want to delete this message?', :method => :delete %>

</p>
4

2 回答 2

3

在消息模型中:

def previous(same_recipient = true)
  collection = Message.where('id <> ? AND updated_at > ?', self.id, self.updated_at).order('updated_at ASC')
  collection.where(recipient_id: self.recipient_id) if same_recipient
  collection.first
end

def next(same_recipient = true)
  collection = Message.where('id <> ? AND updated_at < ?', self.id, self.updated_at).order('updated_at DESC')
  collection.where(recipient_id: self.recipient_id) if same_recipient
  collection.first
end

这就是我们在日历系统中使用的内容,对于使用 Apppointments 进行上一个/下一个非常有用。

这依赖于updated_at列,这有点糟糕(如果我更新旧消息,它会混淆下一个/上一个)。也许您想使用该created_at列,或者您自己的。有了这个,你可以在你的视图中做:

# [...]
<p>
  <strong>Message</strong><br />
  <%=h @message.body %>
</p>

<p>
  <%= link_to 'Next', user_message_path(current_user, @message.next) %>
  <%= link_to 'Previous', user_message_path(current_user, @message.previous) %>
</p>
于 2013-05-07T21:23:27.447 回答
0

在用于查看消息的控制器中,从数据库中获取 3 条消息,并将中间条目显示为当前消息。将上一条和下一条消息的 ID 设置为变量。

然后在您的视图中创建两个按钮:上一个和下一个,它们的链接 url 是您保存的 ID 的路径。

于 2013-05-07T20:57:02.610 回答