首页 文章

rails中的管理员和用户通知

提问于
浏览
0

我已经在我的RoR应用程序中创建了一个帮助票据部分,我想在管理员回复他们的票证(帖子类型)时通知用户注释 . 并且在管理员方面通知管理员创建故障单时以及用户在故障单上注释新评论 .

我根据current_user显示通知有困难 . 我希望current_user仅在current_users创建的故障单上看到来自管理员的通知,并且我希望管理员在用户创建和评论故障单时查看来自用户的通知 .

我创建了一个表:

通知表:

Ticket.reference.
Comment.reference
User.reference
Read.boolean

在我的评论中.rb

def create_notification
  @ticket = self.ticket
  @user = @ticket.created_by
  @user.notifications.create(
    ticket_id: self.ticket_id,
    comment_id: self
  )
end

notification.rb里

class Notification < ActiveRecord::Base
  belongs_to :ticket
  belongs_to :comment
  belongs_to :user

  scope :is_read, -> {
    where(read: true)
  }

  scope :unread, -> {
   where(read: false)
  }
  attr_accessible :read,
                  :ticket_id,
                  :comment_id
end

通知控制器:

class NotificationsController < ApplicationController

  before_filter :find_user, :is_it_me?

  def display_name
    "Notifications"
  end

  def display_desc
    ""
  end

  def index
    @notifications = @user.notifications
    @notifications.each do |notification|
      notification.update_attribute(:read, true)
    end
  end

  def destroy
    @notification = Notification.find(params[:id])
    @notification.destroy
    @notifications = @user.notifications
    redirect_to user_notifications_path(@user)
  end

  def find_user
      @user = User.find(params[:user_id]) unless params[:user_id].nil?
      flash.now[:error] = "Utilisateur introuvable"
      redirect_to dashboard_path if @user.nil?
  end

   def is_it_me?
     redirect_to dashboard_path unless @user = current_user
   end
end

意见

= link_to user_notifications_path(current_user), :class => "dropdown-toggle" do
        i.icon-envelope style ="font-size: 26px;" alt = "ticket"
        span.badge
          = "#{current_user.notifications.unread.count}"

1 回答

  • 0

    看看public_activity帮我完成并运行了这些东西 .

相关问题