0

I am making a inbox messaging for a rails app. In my _inbox.html file I have "<% @user.received_messages.each do |message| %>" but it is not displaying any of the messages I have sent. There's no error message, just a blank space.

Is there another line of coding I should be using to display all messages from inbox?

_inbox.html:

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

message model:

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

message controller:

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
4

2 に答える 2

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>
于 2013-03-28T16:04:33.793 に答える
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 %>
于 2013-03-27T20:07:16.647 に答える