2013-03-27 85 views
0

我正在爲Rails應用程序發送收件箱郵件。在我的_inbox.html文件中,我有「<%@ user.received_messages.each do | message |%>」,但它沒有顯示我發送的任何消息。沒有錯誤信息,只是一個空白區域。@ user.received_messages不顯示收件箱郵件

是否有另一行編碼我應該用來顯示收件箱中的所有郵件?

_inbox.html:

<h2>Your Inbox</h2> 
<% @user.received_messages.each do |message| %> 
No messages in your Inbox 
<% end %> 

消息模型:

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 


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.readingmessage(params[:id],@user_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 

回答

0

我不知道你在哪裏打印的消息。嘗試類似:

<h2>Your Inbox</h2> 
<% if @user.received_messages.empty? %> 
    No messages in your Inbox 
<% end %> 

<% @user.received_messages.each do |message| %> 
    <%= message.body %> 
<% end %> 
+0

這表明空也。這很棘手。 – pwz2000 2013-03-27 20:13:08

+1

我沒有看到方法的位置 - 在您的模型中定義的'received_messages'。 檢查您是否能夠看到錯誤消息 - 它們不是由救援零或某物「吞噬」。 – Magnuss 2013-03-27 20:20:56

+0

您的權利未在消息模型中定義。我會看看我是否可以想出一種方法讓它在模型中工作。感謝您指出。 – pwz2000 2013-03-27 20:37:20

2

我終於解決了我遇到的問題。

在user.rb我不得不刪除「has many:received messages」塊。然後高清received_messages如下:

def received_messages 
     Message.received_by(self) 
    end 

之後,在_inbox的意見,又刪除了此行的編碼有:

<td><%= "#{message.sender.first_name} #{message.sender.last_name}" %></td> 
+0

如果它已修復,請將您的答案標記爲正確。 – CherryPlaysRoblox 2017-12-27 15:51:27

相關問題