2013-04-03 57 views
3

我正在構建收件箱郵件系統。我的所有消息都顯示爲未讀,大多數應該標記爲「已讀」。我已經在我的消息模型中設置了它來檢查未讀或未讀。所以我不明白爲什麼代碼不能用於未讀/讀取。如何更改收件箱郵件標記從未讀到讀取

message.rb:

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

    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)%> 
+0

任何錯誤信息? –

+0

你可以把你的控制器行動嗎? – Zippie

+0

控制器添加了 – pwz2000

回答

5

您的形式交往都顯示爲未讀,因爲read_at場不會從nil更改知道。

它看起來像你的readingmessage方法應該改變的價值,但它永遠不會被調用。

(這將是有意義的調用它在你的控制器的show方法。)

型號:

def readingmessage 
    self.read_at ||= Time.now 
    save 
end 

控制器:

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

我不知道如何在控制器中調用它,因爲我之前沒有做過類似的事情。也不理解爲什麼我不得不在模型中定義某些東西,然後在控制器中定義相同的方法。 – pwz2000

+0

消息仍然標記爲未讀。也許它與收件箱的視圖模板有關。 – pwz2000

+0

如果當前用戶實際上不是收件人,或者您實際上沒有使用「show」操作查看郵件,則將其標記爲未讀的唯一原因是。代碼太簡單了,不會出錯。 – bdares

0

此方法是做太多:

# 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 

我不明白你爲什麼要調用此方法時,發送者讀取消息,但如果這是你需要什麼,我會去這樣的事情:

# 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]) 
    message 
end 

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

我假設你的控制器/圖當收件人讀取消息和消息有關的ID,在這種情況下mark_read可以簡化大大

+0

我必須仔細檢查你的代碼才能理解你想要做什麼。無論如何,它用「未定義的方法」鍵打破了應用程序?爲零:NilClass「。將讓你知道我想出了什麼 – pwz2000