我正在从头开始构建收件箱消息系统,当我将 self.id 添加到模型中的条件时,我在 MessagesController 中标题为 NoMethodError 的所有页面上都收到未定义的方法“id”错误。我尝试了一些解决方法,但我无法掌握它。
消息控制器:
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 = params[: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
用户.rb:
:conditions => ["messages.recipient_deleted = ? AND messages.recipient_id = ?",false,self.id]
..
..
def unread_messages?
unread_message_count > 0 ? true : false
end
# Returns the number of unread messages for this user
def unread_message_count
eval 'messages.count(:conditions => ["recepient_id = ? AND read_at IS NULL", self.user_id])'
end