对于我的自定义收件箱消息系统,我正在设置已发送消息文件夹。我得到一个wrong number of arguments (1 for 0)
. 该页面的标题是 MessagesController#index 中的 ArgumentError。我似乎无法正确定义方法。任何帮助将不胜感激,因为我尝试了一些事情,但似乎无法让它发挥作用。我确信这是一个正常的小修复,但我是 Rails 新手,所以我还不知道所有方法。
用户.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
消息.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
结尾
消息控制器:
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
收件箱.html.erb:
<%= link_to "View Sent Messages", user_messages_path(current_user, :mailbox => :sent)%>