当用户#1喜欢/评论用户#2时,用户#2会收到通知:
通知/ _notifications.html.erb
<%= link_to "", notification_path(notification.id), method: :delete, class: "glyphicon glyphicon-remove" %> <%= link_to Comment.find_by(notification.comment_id).user.name, user_path(Comment.find_by(notification.comment_id).user.id) %> commented on <%= link_to "your activity", (notification.activity_id) %>
但是,当用户#2点击通知时,自从我删除了activity_path
后,它就无法领先,如果我在(notification.activity_id)
之前将其放回去,我们会收到错误:
没有路线匹配 {:action =&gt;&#34; show&#34;,:controller =&gt;&#34;活动&#34;,&gt; :id =&gt; nil}缺少必需的键:[:id]
我不知道这是否可行,但点击通知用户#2将被带到活动i 的分页页面。通过gem will_paginate
每页有20个活动,因此,如果评论的活动在第2页上,那么在点击活动时,用户#2应该定向到:http://0.0.0.0:3000/activities?page=2
这至少会缩小评论在用户活动Feed上的位置。
活动/ 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: activity.comments %>
<%= 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
如果您发现可以执行以下操作,请告知我们。)
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
通知/ 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
答案 0 :(得分:0)
要确定activity
所在的页面,您可以执行以下操作:
activity = Activity.find(notification.activity_id)
index = Activity.order(created_at: :desc).index(activity)
以上内容将确定所有活动中index
的{{1}}。
所以,假设你有85项活动。你每页有20个活动,所以在这种情况下你会有5页,对吗?好吧,让我们假设上面的activity
返回42.要计算页码,你必须这样做(假设你有一个名为index
的变量是20):
per_page
你有page_number = (index / per_page.to_f).ceil
42。你需要除以每页活动的数量(需要成为一个浮动!),这样就是{ {1}}。这导致index
。 20.0
那你已经得到了你的页码,这就是3。
因此,要创建正确的分页路径,您现在可以执行此操作:
2.1
<强>更新强>
既然我们知道ceil
未在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
已设置。正如我在activity_id
之前所说的那样。这是因为user_id
仅在comment_id
上分配,在id
上,您只是将其设置为保存。因此,将save
更改为create
也可以设置after_create :create_notification
。
应设置after_save :create_notification
和comment_id
。现在是下一部分。获取应添加到activity_id
部分链接中的链接的正确页码。
将这些方法添加到comment_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)
错误,则可能未在模型中设置值per_page
(如this;基本上将page_number
添加到per_page
)
如果您决定这样做,可以删除self.per_page = 20
中的class Activity < ActiveRecord::Base
部分。如果没有,只需将, :per_page => 20
替换为ActivitiesController
或per_page.to_f
。
另外,请删除之前已注释掉的20.to_f
中的3行,以确定是否设置了20.0
。你不再需要它们了,因为我们把它们放在了NotificationsController
类中。