通知gem

时间:2017-09-04 16:41:01

标签: ruby-on-rails ruby notifications

我使用 rails-engine https://github.com/rails-engine/notifications通知 gem。当我遍历控制器索引页面时,它似乎失败了。它声称我的导航部分中的链接显然是未定义的。 NotificationController是目前唯一一个遇到此问题的人。我从来没有在今天之前使用这个宝石我的代码在

之下

异常

Started GET "/notifications" for 127.0.0.1 at 2017-09-04 16:06:51 -0400
Processing by Notifications::NotificationsController#index as HTML
  User Load (1.0ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2  [["id", 1], ["LIMIT", 1]]
  Notification Load (0.0ms)  SELECT  "notifications".* FROM "notifications" WHERE "notifications"."user_id" = $1 ORDER BY id desc LIMIT $2 OFFSET $3  [["user_id", 1], ["LIMIT", 25], ["OFFSET", 0]]
  Rendering notifications/notifications/index.html.erb within layouts/application
  Rendered notifications/notifications/index.html.erb within layouts/application (96.1ms)
  Rendered nav_partials/_user_navbar.html.erb (2572.3ms)
Completed 500 Internal Server Error in 8155ms (ActiveRecord: 1.0ms)



ActionView::Template::Error (undefined local variable or method `dashboard_index_path' for #<#<Class:0xc0ceb08>:0xc0ccff0>):
     6: 
     7:     <ul class="navbar-nav mr-auto mt-2 mt-md-0 user-nav-li-padding">
     8:       <li class="nav-item white-nav-link mobile-nav-link">
     9:         <%= active_link_to dashboard_index_path, class_active: 'nav-link active', :class_inactive => 'inactive' do %>
    10:             <div class="left">
    11:               <%= inline_svg 'user_navbar_icons/home_icon_inactive.svg', width: 50, height: 50 %>
    12:             </div>

_navbar.html.erb

<nav class="navbar fixed-top navbar-toggleable-sm pull-nav-to-front">
  <button class="navbar-toggler navbar-toggler-right hidden-md-up" type="button" data-toggle="collapse" data-target="#navbar-collapse" aria-controls="#navbar-collapse" aria-expanded="false" aria-label="toggle navigation" style="background-color: #fdfeff;">&#9776;</button>
  <div class="collapse navbar-collapse mobile-nav-bg" id="navbar-collapse">

    <ul class="navbar-nav mr-auto mt-2 mt-md-0 user-nav-li-padding">
      <li class="nav-item white-nav-link mobile-nav-link">
        <%= active_link_to dashboard_index_path, class_active: 'nav-link active', :class_inactive => 'inactive' do %>
            <div class="left">
              <%= inline_svg 'user_navbar_icons/home_icon_inactive.svg', width: 50, height: 50 %>
            </div>
            <div class="right">
              <p>My Dashboard</p>
            </div>

        <% end %>

      </li>

      <li class="nav-item white-nav-link mobile-nav-link">
        <%= active_link_to posts_path, class_active: 'nav-link active', :class_inactive => 'inactive' do %>
            <div class="left">
              <%= inline_svg 'user_navbar_icons/post_icon_inactive.svg', width: 50, height: 50 %>
            </div>
            <div class="right">
              <p>Posts</p>
            </div>
        <% end %>
      </li>

      <li class="nav-item white-nav-link mobile-nav-link">
        <%= active_link_to mailbox_index_path, class_active: 'nav-link active', :class_inactive => 'inactive' do %>
            <div class="left">
              <%= inline_svg 'user_navbar_icons/mail_icon_inactive.svg', width: 50, height: 50 %>
            </div>
            <div class="right">
              <p>Messages</p>
            </div>
        <% end %>
      </li>
      <li class="nav-item white-nav-link mobile-nav-link">
          <small class="badge badge-default notifications-counter-badge"><%= notification_count_for(current_user) %></small>
        <%= active_link_to notifications_path, class_active: 'nav-link active', :class_inactive => 'inactive' do %>

            <div class="left">
              <%= inline_svg 'user_navbar_icons/notification_icon_inactive.svg', width: 50, height: 50 %>
            </div>
            <div class="right">
              <p>Notifications</p>
            </div>
        <% end %>
      </li>
      <li class="nav-item white-nav-link mobile-nav-link">
        <%= active_link_to favorites_index_path, class_active: 'nav-link active', :class_inactive => 'inactive' do %>
            <div class="left">
              <%= inline_svg 'user_navbar_icons/favorite_icon_inactive.svg', width: 50, height: 50 %>
            </div>
            <div class="right">
              <p>Favorites</p>
            </div>
        <% end %>
      </li>
      <li class="nav-item white-nav-link mobile-nav-link">
        <div class="dropdown">
          <%= active_link_to '#', active: false, class: 'btn dropdown-toggle', class_active: 'nav-link active ', :class_inactive => 'inactive', id: 'dropdownmenubtn', data: { toggle: 'dropdown' }, aria: {haspopup: true, expanded: false } do %>
              <div class="left">
                <%= inline_svg 'user_navbar_icons/settings_icon_inactive.svg', width: 50, height: 50 %>
              </div>
              <div class="right">
                <p>Settings</p>
              </div>
              <div class="dropdown-menu dropdown-menu-right settings-dropdown-menu-margin-pos dropdown-menu-font-color" aria-labelledby="dropdownmenubtn">
                <h6 class="dropdown-header"><%= current_user.username %></h6>
                <div class="dropdown-divider"></div>
                <%= link_to 'Account Settings', edit_user_registration_path, class: 'dropdown-item' %>
                <%= link_to 'Help', user_help_index_path, class: 'dropdown-item' %>
                <div class="dropdown-divider"></div>
                <%= link_to 'Logout', destroy_user_session_path, class: 'dropdown-item', method: :delete %>
              </div>
          <% end %>
        </div>

      </li>
    </ul>
  </div>
</nav>

<script type="text/javascript">
  $(document).on('turbolinks:load', function() {
      $('.dropdown-toggle').dropdown()
  });
</script>

application.html.erb

   <body>
    <% if user_signed_in? %>
        <%= render 'nav_partials/user_navbar' %>
    <% elsif admin_signed_in? %>
    <%= render 'nav_partials/admin_navbar' %>
    <% else %>
        <%= render 'nav_partials/navbar' %>
    <% end %>
<%= yield %>
    </body>

notifications_controller.rb

module Notifications
  class NotificationsController < Notifications::ApplicationController

    def index
      @notifications = notifications.includes(:actor).order('id desc').page(params[:page])

      unread_ids = @notifications.reject(&:read?).select(&:id)
      Notification.read!(unread_ids)

      @notification_groups = @notifications.group_by { |note| note.created_at.to_date }
    end

    def clean
      notifications.delete_all
      redirect_to notifications_path
    end

    private

    def notifications
      raise "You need reqiure user login for /notifications page." unless current_user
      Notification.where(user_id: current_user.id)
    end
  end
end

通知索引

<div class="notifications row">
  <div class="heading clearfix">
    <%= t('notifications.all_notifications') %>

    <span class="pull-xs-right">
      <%= link_to t('notifications.clean_all'), notifications.clean_notifications_path, class: 'btn btn-sm btn-secondary', method: 'delete' %>
    </span>
  </div>
  <div class="list">
    <% if @notifications.blank? %>
      <div class="no-records"><%= t('notifications.no_records') %></div>
    <% else %>
      <% @notification_groups.each do |group, notifications| %>
        <div class="notification-group">
          <div class="group-title"><%= group %></div>
          <%= render notifications %>
        </div>
      <% end %>
    <% end %>
  </div>
  <%= paginate @notifications %>
</div>

_notification.html.erb

<%= cache(['notifications', Notifications::VERSION, notification]) do %>
<div id="notification-<%= notification.id %>"
     data-id="<%= notification.id %>"
     class="media notification notification-<%= notification.notify_type %><%= ' unread' unless notification.read? %>">
  <div class="media-left">
    <% if notification.actor_profile_url && notification.actor_avatar_url %>
      <%= link_to image_tag(notification.actor_avatar_url), notification.actor_profile_url, title: notification.actor_name, class: 'user-avatar' %>
    <% end %>
  </div>
  <div class="media-body">
    <%= render partial: "/notifications/#{notification.notify_type}", locals: { notification: notification } %>
  </div>
  <div class="media-right">
    <%= l notification.created_at, format: :short %>
  </div>
</div>
<% end %>

通知_follow.html.erb

<div class="media-heading">
  <%= link_to notification.actor.username, notification.actor %> just followed you.
</div>

的routes.rb

  Rails.application.routes.draw do

  mount Notifications::Engine => "/notifications"


  mount Payola::Engine => '/payola', as: :payola
  mount CountryStateSelect::Rails::Engine, at: "/"

  devise_for :admins
  devise_for :users

  namespace :api do
    scope :v1 do
      mount_devise_token_auth_for 'User', at: 'user_auth'
    end
  end

  devise_scope :user do
    put 'user_change_plan', :to => 'users/registrations#user_change_plan'
    put 'user_update_credit_card', :to => 'users/registrations#user_update_credit_card'
    authenticated do
      root to: 'user_dashboard#index', as: 'authenticated_user_root'
    end

    unauthenticated do
      root to: 'home#index', as: 'unauthenticated_user_root'
    end
  end
  end

  devise_scope :admin do
    authenticated do
      root to: 'admin_dashboard#admin', as: 'authenticated_admin_root'
    end

    unauthenticated do
      root to: 'home#index', as: 'unauthenticated_admin_root'
    end
  end


  controller :home do
    get :index, to: 'home#index', as: 'home', :path => 'home'
    get :login_portal, to: 'home#login_portal', as: 'login_portal', :path => 'login_portal'
    get :signup_portal, to: 'home#signup_portal', as: 'signup_portal', :path => 'signup_portal'
  end

  resources :user_dashboard, as: 'dashboard', :only => [:index, :show]
  resources :comments
  resources :posts do
    member do
      post :share
      put 'like', to: 'posts#like'
      get 'like', to: 'posts#like'
    end
  end

  resources :users, only: [:show, :index, :update], path: 'u' do
    get 'users/:username' => 'users#show'
    patch 'users/:username', to: 'users#update'
    resources :follows, :only => [:create, :destroy]
  end
  controller :favorites do
    get 'favorites/index'
  end

  resource :user_profile, :only => [:edit, :update], path: 'profile'
    root 'home#index'
end

我尝试包含Notifications模块,而不是将控制器包装在其中。它也不起作用。

500 Internal Server Error If you are the administrator of this website, then please read this web application's log file and/or the web server's log file to find out what went wrong.

2 个答案:

答案 0 :(得分:0)

按以下方式更改路线:

resource :user_dashboard, as: 'dashboard', only: :show

您的链接看起来像这样

<%= active_link_to dashboard_path, class_active: 'nav-link active', :class_inactive => 'inactive' do %>

答案 1 :(得分:0)

您需要在导航栏上的每个链接上添加def your_dynamic_query_generator(self): return Q(num_shifts=0) 前缀,例如main_app ...

有关更多详细信息,请参见here