如何建立分页网址​​的路径?

编程入门 行业动态 更新时间:2024-10-28 02:24:08
本文介绍了如何建立分页网址​​的路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

When user #1 likes/comments on user #2, user #2 gets a notification:

notifications/_notifications.html.erb

<%= link_to "", notification_path(notification.id), method: :delete, class: "glyphicon glyphicon-remove" %> <%= link_to Comment.find_by(notificationment_id).user.name, user_path(Comment.find_by(notificationment_id).user.id) %> commented on <%= link_to "your activity", (notification.activity_id) %>

,但是当用户#2单击该通知后,由于我删除了activity_path,因此该通知不会出现在任何地方,如果我将其放回(notification.activity_id)之前,则会收到错误消息:

but upon user #2 clicking the notification it doesn't lead anywhere since I removed the activity_path, if I put it back before (notification.activity_id) we get an error:

没有路线匹配 {:action =>显示",:controller =>活动",>:id => nil}缺少必需的键:[:id]

No route matches {:action=>"show", :controller=>"activities", > :id=>nil} missing required keys: [:id]

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

I don't know if this is possible, but upon clicking on the notification user #2 would be taken to the paginated page where the activity is. There are 20 activities per page via the gem will_paginate so if the activity that was commented on is on page 2 then upon clicking on the activity, user #2 should be directed to: 0.0.0.0:3000/activities?page=2

这至少会缩小用户对活动供稿的评论的位置.

This would at least narrow down where the comment is on the activity feed for the user.

activities/index.html.erb

<% @activities.each do |activity| %> <%= link_to activity.user.name, activity.user %> <%= render "activities/#{activity.trackable_type.underscore}/#{activity.action}", activity: activity %> <% activity.activity_likers.each do |user| %> <%= user.name %> <% end %> <%= link_to like_activity_path(:id => activity.id), class: "btn", method: :post do %> <span class='glyphicon glyphicon-thumbs-up'></span> Like <% end %> <%= render "comments/comments", comments: activityments %> <%= render "comments/form", new_comment: Comment.new(commentable_id: activity.id, commentable_type: activity.class.model_name), create_url: :activity_comments_path %> <% end %> <%= will_paginate @activities %>

activities_controller.rb

class ActivitiesController < ApplicationController def index @activities = Activity.order("created_at desc").paginate(:page => params[:page], :per_page => 20) end def show redirect_to(:back) end end

如果发现可以执行此操作,请告诉我是否需要更多代码:)

Please let me know if you need anymore code if you find that this is possible to do :)

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

notifications/index.html.erb

<% if !@notifications.blank? %> <%= render partial: "notifications/notification", collection: @notifications %> <% else %> <p>No notifications... yet</p> <% end %>

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位于哪一页,您可以执行以下操作:

To determine on which page the activity is located, you could do the following:

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

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

The above will determine the index of the activity within all of the activities.

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

So, let's say you've got 85 activities. You've got 20 activities per page, so in this case you would have 5 pages, right? Alright, let's assume the above index returns 42. To calculate the page number you would have to do this (assuming that you've got a variable called per_page which is 20):

page_number = (index / per_page.to_f).ceil

您有index42.您必须将其除以每页的活动数量(需要为浮动对象!),因此为20.0 .结果为2.1. ceil这样,您已经获得了页码,它是3.

You've got index 42. Which you'll have to divide by the number of activities per page (it needs to be a float!), so that would be 20.0. That results in 2.1. ceil that and you've got your page number, which would be 3.

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

So, to create the correct paginated path, you can now do this:

activities_path(page: page_number)

更新

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

Now that we know that the activity_id isn't correctly set on the notification we can fix that (also note that the comment_id isn't set either). Change the last part of your Comment model to this:

... 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

我在此处添加了两个验证,以确保设置了activity_id和user_id.现在,正如我之前所说,comment_id也未设置.这是因为id仅在save上分配,在create上您只是将其设置为要保存.因此,将after_create :create_notification更改为after_save :create_notification以便也可以设置comment_id.

I've added two validations here to make sure the activity_id and user_id are set. Now as I said before the comment_id isn't set either. That's because the id is only assigned on save, on create you are just setting it up to be saved. So, change the after_create :create_notification to after_save :create_notification to be able to set the comment_id as well.

应该设置activity_id和comment_id.现在进入下一部分.获取正确的页码,该页码应添加到您的_notification.html.erb部分的链接中.

That should set the activity_id and comment_id. Now for the next part. Getting the correct page number which should be added to the link in your _notification.html.erb partial.

将这些方法添加到您的Activity类中:

Add these methods to your Activity class:

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部分中的路径更改为:

Now change the path in your notifications/_notification.html.erb partial to:

activities_path(page: notification.activity.page_number)

注意:如果在page_number方法中遇到关于per_page的错误,则可能未在模型本身中设置值per_page(例如此;基本上将self.per_page = 20添加到模型中的class Activity < ActiveRecord::Base下方)

Note: If you get an error about the per_page in the page_number method you probably haven't set the value per_page in the model itself (like this; Basically add self.per_page = 20 to your model right below class Activity < ActiveRecord::Base)

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

If you decide to do it like this, you can remove the , :per_page => 20 part in your ActivitiesController. If not, simply replace per_page.to_f with 20.to_f or 20.0.

此外,从您之前注释过的NotificationsController中删除3行,以了解是否设置了activity_id.您已经不再需要它们了,因为我们已经将它们放在了Activity类中.

Also, remove the 3 lines from your NotificationsController which you've commented out previously to find out whether the activity_id was set or not. You don't need them anymore, since we've placed them in the Activity class.

更多推荐

如何建立分页网址​​的路径?

本文发布于:2023-11-01 07:27:10,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1548662.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:分页   路径   网址

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!