【发布时间】:2016-10-26 22:23:27
【问题描述】:
我正在尝试使用操作电缆在 rails 5 中创建通知。想知道是否有人可以帮助解决我的问题。
目前我有我的通知表
架构
create_table "notifications", force: :cascade do |t|
t.string "activity"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "user_id"
t.integer "recipient_id"
t.string "action"
t.string "notifiable_type"
t.integer "notifiable_id"
t.index ["user_id"], name: "index_notifications_on_user_id"
end
我的通知模型
class Notification < ApplicationRecord
belongs_to :user
belongs_to :recipient, class_name: "User"
belongs_to :notifiable, polymorphic: true
after_create_commit { NotificationBroadcastJob.perform_later(Notification.count,self)}
validates :user_id, presence: true
end
我无法理解如何在控制器和视图中表示用户通知。
例如,当创建特定操作时,我有通知创建方法。
class Comment < ApplicationRecord
after_create_commit { create_notification }
private
def create_notification
Notification.create action: "Your comment has been created", user_id: comment.user, recipient_id: comment.user
end
end
在这里,我尝试在应用程序控制器的辅助方法中将用户和通知联系在一起。
helper_method :current_notifications
def current_notifications
if current_user
@notifications = current_user.notifications.all
else
end
end
我收到的错误是
SQLite3::SQLException: no such column: notifications.recipient_type: SELECT COUNT(*) FROM "notifications" WHERE "notifications"."recipient_id" = ? AND "notifications"."recipient_type" = ?
我的问题再次在于如何表示用户的通知。我很确定我对如何将事情联系在一起感到困惑。我正在尝试遵循我在下面列出的 Chris Oliver 教程。
https://gist.github.com/excid3/4ca7cbead79f06365424b98fa7f8ecf6
任何帮助或更正都会有所帮助
【问题讨论】:
标签: ruby-on-rails ruby-on-rails-5 actioncable