2013-08-19 72 views
0

我有一個問題&答案功能,我剛剛完成它。我不知道如何在另一個用戶向他們詢問問題時向用戶發送消息的代碼。例如,用戶A向用戶B發送問題,收件箱消息被髮送給用戶B,通知他們他們有新的問題。發送用戶收件箱消息從問答部分

有兩種方式我想如何去做這件事。

方法#1 消息,將與問題就可以鏈接用戶的頁面,因此可以回答

方法2 的消息中,將包括爲這個問題裏面問。用戶可以回覆將提交的消息作爲他們的答案。

問題控制器:

respond_to :js, :html 

    def index 
    @questions = Question.all 
    respond_with(@questions) 
end 


def show 
    @question = Question.find(params[:id]) 
    @questions = Question.order("created_at DESC") 
    respond_with(@questions) 
end 


def new 
    @question = Question.new 
    respond_with(@question) 
end 


def create 
    @question = Question.new(params[:question]) 
    if @question.save 
    redirect_to questions_path, notice: 'Your question was saved successfully. Thanks!' 
    else 
    render :new, alert: 'Sorry. There was a problem saving your question.' 
    end 
end 
end 

答案控制器:

def new 
    @question = Question.find(params[:question_id]) 
    end 

    def create 
    @question = Question.find(params[:question_id]) 
    if @question.update_attributes(params[:question]) 
     redirect_to questions_path 
    else 
     render :new 
    end 
    end 
end 

消息控制器:

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] == "archived" 
    # @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_sender_id(params[:reply_to]) 
     unless @reply_to.nil? 
     @message.recipient_id = @reply_to.sender_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 

消息模型:

attr_accessible :subject, :body, :sender_id, :recipient_id, :read_at,:sender_deleted,:recipient_deleted 
    validates_presence_of :subject, :message => "Please enter message title" 
    has_many :notifications, as: :event 
    scope :unread, -> {where('read_at IS NULL')} 
    scope :not_deleted_by_recipient, where('messages.recipient_deleted IS NULL OR messages.recipient_deleted = ?', false) 
    scope :not_deleted_by_sender, where('messages.sender_deleted IS NULL OR messages.sender_deleted = ?', false) 


    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 

     def previous(same_recipient = true) 
     collection = Message.where('id <> ? AND created_at > ?', self.id, self.created_at).order('created_at ASC') 
     collection = collection.where(recipient_id: self.recipient_id) if same_recipient 
     collection = collection.not_deleted_by_recipient 
     collection.first 
     end 


     def next(same_recipient = true) 
     collection = Message.where('id <> ? AND created_at < ?', self.id, self.created_at).order('created_at DESC') 
     collection = collection.where(recipient_id: self.recipient_id) if same_recipient 
     collection = collection.not_deleted_by_recipient 
     collection.first 
     end 
    end 

    private 
    def send_notification(message) 
     message.notifications.create(user: message.recipient) 
    end 

回答

2

我已經解決了這個問題。

問題控制器:

def create 
    @question = Question.new(params[:question]) 
    if @question.save 
    @message = Message.create(:subject => "Hey you have a message from #{@question.sender_id}", 
          :sender_id => @question.sender_id, 
          :recipient_id => @question.recipient_id, 
          :body => @question.question) 

    @question.message = @message 
    @question.save 
    redirect_to questions_path, notice: 'Your question was saved successfully. Thanks!' 
    else 
    render :new, alert: 'Sorry. There was a problem saving your question.' 
    end 
end 
0

每次需要通知時,都應該創建它。它會去創建一個Notification模型,該模型至少需要創建通知的對象,通知要使用的用戶以及是否已讀取該標記的標誌。

要顯示的通知,當然最簡單的方法是將整合到您的佈局文件,做這樣的事情:

Notification.where(:user_id => @current_user).size > 0 
    <%= "You have #{Notification.where(:user_id => @current_user).size} notification(s)" %> 
end 

並以nicen東西,你可以通知對象的內部創建範圍這會給你一個特定用戶的通知。當然,如果你想像Stackoverflow這樣的所有魔術都通過AJAX調用來更新通知,你將不得不做一些JavaScript提升。

相關問題