如何建立分页url​​的路径?

当用户#1喜欢/评论用户#2时,用户#2会收到通知:

通知/ _notifications.html.erb

  commented on  

但是在用户#2点击通知后,由于我删除了activity_path ,因此它没有任何结果,如果我之前将其放回(notification.activity_id)我们会收到错误:

没有路由匹配{:action =>“show”,:controller =>“activities”,>:id => nil}缺少必需的键:[:id]

我不知道这是否可行,但点击通知用户#2将被带到活动的分页页面 。 通过gem will_paginate每页有20个活动,因此如果被注释的活动在第2页上,那么在点击活动时,用户#2应该被定向到:http: //0.0.0.0 : gem will_paginate /activities?page = 2

这至少会缩小注释对用户的活动供稿的位置。

活动/ index.html.erb

        activity.id), class: "btn", method: :post do %>  Like      

activities_controller.rb

 class ActivitiesController  params[:page], :per_page => 20) end def show redirect_to(:back) end end 

如果您发现可以这样做,请告诉我您是否需要更多代码:)

UPDATE

 class NotificationsController < ApplicationController def index @notifications = current_user.notifications @notifications.each do |notification| notification.update_attribute(:read, true) activity = Activity.find(notification.activity_id) #Gives "Couldn't find Activity with 'id'=". I then removed ".activity_id" to see what happens next. index = Activity.order(created_at: :desc).index(activity) page_number = (index / per_page.to_f).ceil #I am then told "undefined local variable or method `per_page'" so I tried making it :per_page to which I then get: "undefined method `to_f' for :per_page:Symbol" end end def destroy @notification = Notification.find(params[:id]) @notification.destroy redirect_to :back end end 

的routes.rb

 resources :activities do resources :comments resources :notifications member do post :like post :notifications end end 

更新#2

通知/ index.html.erb

     

No notifications... yet

comment.rb

 class Comment < ActiveRecord::Base after_create :create_notification has_many :notifications has_many :comment_likes has_many :likers, through: :comment_likes, class_name: 'User', source: :liker belongs_to :commentable, polymorphic: true belongs_to :user belongs_to :activity private def create_notification @activity = Activity.find_by(self.activity) @user = User.find_by(@activity.user_id).id Notification.create( activity_id: self.activity, user_id: @user, comment_id: self, read: false ) end end 

_create_notifications.rb

 class CreateNotifications < ActiveRecord::Migration def change create_table :notifications do |t| t.references :activity, index: true t.references :comment, index: true t.references :user, index: true t.boolean :read t.timestamps null: false end add_foreign_key :notifications, :activities add_foreign_key :notifications, :comments add_foreign_key :notifications, :users end end 

要确定activity所在的页面,您可以执行以下操作:

 activity = Activity.find(notification.activity_id) index = Activity.order(created_at: :desc).index(activity) 

以上将确定所有活动中的活动index

所以,假设你有85项活动。 你每页有20个活动,所以在这种情况下你会有5页,对吗? 好吧,让我们假设上面的index返回42.要计算页码,你必须这样做(假设你有一个名为per_page的变量是20):

 page_number = (index / per_page.to_f).ceil 

你有index 42.你必须除以每页活动的数量(它需要是一个浮点数!),所以这将是20.0 。 结果是2.1ceil ,你有你的页码,这将是3。

因此,要创建正确的分页路径,您现在可以执行以下操作:

 activities_path(page: page_number) 

更新

现在我们知道activity_id没有在notification上正确设置我们可以修复它(还要注意, comment_id也没有设置)。 将Comment模型的最后一部分更改为:

 ... belongs_to :activity validates :activity_id, presence: true validates :user_id, presence: true private def create_notification Notification.create(activity_id: self.activity_id, user_id: self.user_id, comment_id: self.id, read: false) end 

我在这里添加了两个validation,以确保设置了activity_iduser_id 。 正如我之前所说, comment_id也未设置。 这是因为id仅在save分配,在create您只是将其设置为保存。 因此,将after_create :create_notification更改为after_save :create_notification也可以设置comment_id

那应该设置activity_idcomment_id 。 现在是下一部分。 获取应添加到_notification.html.erb部分链接中的正确页码。

将这些方法添加到Activity类:

 def page_number (index / per_page.to_f).ceil end private def index Activity.order(created_at: :desc).index self end 

现在将您的notifications/_notification.html.erb的路径更改为:

 activities_path(page: notification.activity.page_number) 

注意:如果你在page_number方法中得到关于per_page的错误,你可能没有在模型本身中设置per_page值(就像这样 ;基本上将self.per_page = 20添加到class Activity < ActiveRecord::Base下面的模型中)

如果您决定这样做,可以删除, :per_page => 20中的, :per_page => 20部分。 如果没有,只需将per_page.to_f替换为20.to_f20.0

另外,从您之前注释掉的NotificationsController删除3行,以确定activity_id是否已设置。 你不再需要它们,因为我们已将它们放在Activity类中。