0

我正在开发自定义收件箱系统。当我去删除消息时,它会闪烁一条消息,该消息已被删除。但是,我仍然可以通过我的收件箱和输入 URL 来查看消息。如何设置操作以使消息实际永久删除或至少放置在删除文件夹中(我尚未创建)?

消息模型:

class Message < ActiveRecord::Base
    attr_accessible :subject, :body, :sender_id, :recepient_id, :read_at,:sender_deleted,:recepient_deleted
    validates_presence_of :subject, :message => "Please enter message title"

    belongs_to :sender,
    :class_name => 'User',
    :foreign_key => 'sender_id'
    belongs_to :recepient,
    :class_name => 'User',
    :foreign_key => 'recepient_id'

    # marks a message as deleted by either the sender or the recepient, which ever the user that was passed is.
    # When both sender and recepient marks it deleted, it is destroyed.
    def mark_message_deleted(id,user_id)
        self.sender_deleted = true if self.sender_id == user_id and self.id=id
        self.recepient_deleted = true if self.recepient_id == user_id and self.id=id
        self.sender_deleted && self.recepient_deleted ? self.destroy : save!
    end

    # Read message and if it is read by recepient then mark it is read
    def self.readingmessage(id, reader)
        message = find(id, :conditions => ["sender_id = ? OR recepient_id = ?", reader, reader])
        if message.read_at.nil? && (message.recepient.user_id==reader)
            message.read_at = Time.now
            message.save!
        end
        message
    end

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

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

     def self.not_recepient_deleted
       where("recepient_deleted = ?", false)
     end

end

消息控制器:

class MessagesController < ApplicationController

  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
  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.recepient_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])
  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

收件箱视图:

<h2>Your Inbox</h2>
<% if @messages.size == 0 %>
No messages in your Inbox
<% end %>
<% else %>
        <%= form_tag delete_multiple_user_messages_path, :method=>:post do %>
            <table  class="table table-bordered">
                    <tr>
                    <th>Delete?</th>
                    <th>Sent</th>
                    <th>Sender</th>
                    <th>Sent</th>
                </tr>
                    <% for message in @messages %>
                        <tr>
                            <td><%= check_box_tag "delete[]", message.id %></td>
                            <td>
                                <% if message.read? %>
                                  <%= link_to h(message.subject), user_message_path(@user, message) %>
                                <% else %>
                                  <%= link_to "#{h(message.subject)} (unread)", user_message_path(@user, message) %>
                                <% end %>
                            </td>

                            <td><%=h message.created_at.to_s(:long) %></td>
                        </tr>
            <% end %>
            </table>
        <%= submit_tag "Delete selected" %> |
        <% end %>
<%= link_to "Compose New Message", new_user_message_path(@user)%> | 
<%= link_to "View Sent Messages", user_messages_path(current_user, :mailbox => :sent)%>
4

1 回答 1

1

开始了...

一种方法是从数据库中永久删除消息,这样即使可以点击特定的 url,它们也不会进一步可用。

或者如果您想软删除消息而不是从数据库中永久删除,在这种情况下,您必须在show方法中添加一个条件,以便如果消息被软删除,则index页面。

所以你的show方法可能是

定义显示
  @message = Message.where(:id => params[:id], :deleted => false).first
  redirect_to messages_path 除非@message.present?
结尾

在上面的查询中,我假设这deleted是可以用来告诉天气一个人是否删除的字段。如果只有那deleted是假的,上面的查询将返回一条消息。

更新:

在模型中检查您的方法mark_message_deleted后,修改方法如下

def mark_message_deleted(id,user_id)
    self.sender_deleted = true if self.sender_id == user_id
    self.recepient_deleted = true if self.recepient_id == user_id
    (self.sender_deleted && self.recepient_deleted) ? self.destroy : self.save!
end

仅此一项就足够了。

于 2013-03-28T18:06:57.867 回答