2013-04-17 20 views
0

對於我的自定義收件箱郵件系統,我正在設置發送的郵件文件夾。我得到一個wrong number of arguments (1 for 0)。 MessagesController#index中的頁面標題是ArgumentError。我似乎無法正確定義方法。任何幫助將不勝感激,因爲我已經嘗試了一些東西,但似乎無法讓它工作。我相信這是一個正常的小修補程序,但我是新手,所以我還不知道所有的方法。發送文件夾中0的參數數量錯誤

user.rb:

def received_messages 
     Message.received_by(self) 
    end 

def unread_messages? 
    unread_message_count > 0 ? true : false 
end 

def sent_messages 
    Message.sent_by(self) 
end 

message.rb:

 class Message < ActiveRecord::Base 
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) 
    user.messages.sent 
end 

messages_controller:

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 

inbox.html.erb:

<%= link_to "View Sent Messages", user_messages_path(current_user, :mailbox => :sent)%> 

回答

1

它看起來像你的Message.sent_by的定義不正確,請嘗試:

# message.rb 
def self.sent_by(user) 
    Message.where(:sender_id => user.id) 
end 
+0

這給了未定義的方法'消息 – pwz2000

+0

您可以包括從用戶和消息類所有的代碼? –

+0

增加更多代碼 – pwz2000

相關問題