控制器中未定义的方法`unread_messages'

时间:2013-04-23 15:28:08

标签: ruby-on-rails

我正在处理收件箱邮件系统的未读文件夹。当我转到/ users / 1 / messages?mailbox = unread并且它指向消息控制器时,我收到了一个未定义的方法`unread_messages'。但是我在user.rb中定义了unread_messages,以便它在收件箱中显示未读消息,所以我假设代码也适用于未读文件夹。任何帮助将不胜感激。

user.rb:

class User < ActiveRecord::Base
  has_secure_password
  attr_accessible :role, :age, :age_end, :password_confirmation, :about_me, :feet, :inches, :password, :birthday, :career, :children, :education, :email, :ethnicity, :gender, :height, :name, :password_digest, :politics, :religion, :sexuality, :user_drink, :user_smoke, :username, :zip_code
  validates_uniqueness_of :email
  validates_format_of :email, with: /^[-a-z0-9_+\.]+\@([-a-z0-9]+\.)+[a-z0-9]{2,4}$/i
  validates_presence_of :password, :on => :create
  has_many :galleries
  has_many :photos, :through => :galleries
  before_create { generate_token(:auth_token) }
  ROLES = %w[admin user guest banned]

  # models/user.rb
  after_create :setup_gallery

  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

 # Returns the number of unread messages for this user
 def unread_message_count
   eval 'messages.count(:conditions => ["recipient_id = ? AND read_at IS NULL", self.user_id])'
 end

  def to_s; username
  end

  def has_role?(role_name)
    role.present? && role.to_sym == role_name.to_sym
  end

  def send_password_reset
    generate_token(:password_reset_token)
    self.password_reset_sent_at = Time.zone.now
    save!
    UserMailer.password_reset(self).deliver
  end

  def generate_token(column)
    begin
      self[column] = SecureRandom.urlsafe_base64
    end while User.exists?(column => self[column])
  end

  private
  def setup_gallery
     self.galleries << Gallery.create
   end
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
    if params[:mailbox] == "unread"
    @messages = @user.unread_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.recipient_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])
     @message.readingmessage if @message.recipient == current_user
   end

   def destroy
     @message = Message.find(params[:id])
     @message.destroy
     flash[:notice] = "Successfully deleted message."
     redirect_to user_messages_path(@user, @messages)
   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

message.rb:

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)
    Message.where(:sender_id => user.id)
  end

end

2 个答案:

答案 0 :(得分:2)

首先,您已定义unread_messages?方法,而不是unread_messages

其次,您不应在? :方法中使用unread_messages?

unread_messages_count > 0

就够了。

您应该在用户模型中定义unread_messages方法。我无法在此方法中显示内部代码,因为我不知道您的模型关系。

答案 1 :(得分:0)

您已定义

unread_messages? # note the question mark

然后将其称为

@user.unread_messages # no question mark

你可能想要一个方法

def unread_messages ## which holds the actual unread messages and does not return boolean