通知未在我的rails应用中加载

时间:2017-01-10 01:35:38

标签: javascript jquery ruby-on-rails ajax notifications

我有一个rails应用程序,允许用户对某个任务发表评论,每次在任务上发布评论时,跟随该项目的用户都应该收到通知。点击通知图标后,我的ajax通知不会弹出:

notifications.js.coffee

class Notifications
    constructor: ->
        @notifications = $("[data-behavior='notifications']")
        @setup() if @notifications.length >0

    setup: ->
        $.ajax(
            url: "/notifications.json"
            dataType: "JSON"
            method: "GET"
            success: @handleSuccess
        )

    handleSuccess: (data) =>
        items = $.map data, (notification) ->
            "<a class='dropdown-item' href="#">#{notification.actor} #{notification.action} #{nofication.notifiable.type}</a>"
        $("[data-behavior='notification-items']").html(items)

jQuery ->
    new Notifications

_navigation_links.html.erb partial(我的通知图标所在的位置)

<button class="navbar-toggle" data-toggle="collapse" data-target=".navHeaderCollapse">
    <span class="glyphicon glyphicon-user white"></span>
</button>
<div class="collapse navbar-collapse navHeaderCollapse">
    <ul class="nav navbar-nav">

        <li><%= link_to "start a project", new_project_path %></li>

        <% if user_signed_in? %>

        <li class="nav-item btn-group" data-behavior="notifications">
            <a class="dropdown-toggle nav-link" type="button" id="dropdownMenu1" data-toggle="dropdown" aria-haspopup="true" aria-expanded="fa">
                <%= fa_icon "bell" %>
            </a>
            <div class="dropdown-menu" aria-labelledby="dropdownMenu1" data-behavior="notification-items">

            </div>
        </li>

        <div class="dropdown" style="float: right; margin-left: 20px; margin-top: 15px;">
          <li class="dropdown-toggle" id="dropdownMenu1" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true" style="cursor: pointer;">
           <%= current_user.username %>
           <span class="caret"></span>
          </li>
          <ul class="dropdown-menu" aria-labelledby="dropdownMenu1">
                <li><%= link_to 'My Profile',  users_following_user_path(current_user) %></li>
            <li><%= link_to 'Edit Profile', profile_user_path(current_user) %></li>
                <li><%= link_to 'Account Settings', edit_user_path(current_user) %></li>
            <li role="separator" class="divider"></li>
                <li><%= link_to 'Log out', destroy_user_session_path, method: :delete %></li>
          </ul>
        </div>
        <% elsif %>
            <li><%= link_to "Sign Up", new_user_registration_path %></li>
            <li><%= link_to "Sign In", new_user_session_path %></li>
        <% end %>

    </ul>
</div>

的routes.rb

resources :notifications

notification.rb模型

class Notification < ActiveRecord::Base
  belongs_to :recipient, class_name: "User"
  belongs_to :actor, class_name: "User"
  belongs_to :notifiable, polymorphic: true

  scope :unread, -> { where(read_at: nil) }
end

在我一直关注的几个教程中,当我点击导航链接中的铃铛图标时,会弹出通知。但javascript无法运行...

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

notifications.js.coffee文件中存在拼写错误,请将handleSuccess函数更改为以下内容(将href="#"更改为href='#'):

handleSuccess: (data) =>
    items = $.map data, (notification) ->
        "<a class='dropdown-item' href='#'>#{notification.actor} #{notification.action} #{nofication.notifiable.type}</a>"
    $("[data-behavior='notification-items']").html(items)

这应解决问题。