删除 post 方法不适用于已注意到的 gem rails
delete post method not working with noticed gem rails
我有一个带有评论的简单模块查询,我使用通知 gem 向评论添加通知,但是当我删除查询 post 时,它会停止服务器,页面无法正常工作,并删除整个帐户,就像我无法使用该帐户登录一样 again.i 是这个 gem 的新用户。
inquest.rb
class Inquest < ApplicationRecord
acts_as_votable
belongs_to :user, dependent: :destroy
has_many :comments, -> {order(:created_at => :desc)}, dependent: :destroy
has_many_attached :images
validates :description, presence: true
has_noticed_notifications model_name: 'Notification'
has_many :notifications, through: :user, dependent: :destroy
validates :title, :presence => true, :length => {
:maximum => 250,
:minimum => 25,
:tokenizer => lambda { |str| str.scan(/\w+/) },
:too_long => "Please limit your summary to %{count} words"
}
end
notification.rb
class Notification < ApplicationRecord
include Noticed::Model
belongs_to :recipient, polymorphic: true
end
comment.rb
class Comment < ApplicationRecord
belongs_to :inquest
belongs_to :user
validates :content, presence: true
after_create_commit :notify_recipient
before_destroy :cleanup_notifications
has_noticed_notifications model_name: 'Notification'
private
def notify_recipient
Commentnotification.with(comment: self, inquest: inquest).deliver_later(inquest.user)
end
def cleanup_notifications
notifications_as_comment.destroy_all
end
end
user.rb
has_many :notifications, as: :recipient
评论notification.rb
def message
@inquest = Inquest.find(params[:comment][:inquest_id])
@comment = Comment.find(params[:comment][:id])
@user = User.find(@comment.user_id)
"#{@user.user_name} commented on #{@inquest.title.truncate(10)}"
end
def URL
inquest_path(Inquest.find(params[:comment][:inquest_id]))
end
问题是您的协会中有 dependent: :destroy
:
class Inquest < ApplicationRecord
belongs_to :user, dependent: :destroy
end
删除了 Inquest
的用户,因此帐户被删除。一个 User
可以有很多 Inquest
并且通常与 User
有很多相关联,所以用其他对象破坏它是一个坏主意。相反,User
模型应该 dependent: :destroy
与一般情况下的关联。
我有一个带有评论的简单模块查询,我使用通知 gem 向评论添加通知,但是当我删除查询 post 时,它会停止服务器,页面无法正常工作,并删除整个帐户,就像我无法使用该帐户登录一样 again.i 是这个 gem 的新用户。 inquest.rb
class Inquest < ApplicationRecord
acts_as_votable
belongs_to :user, dependent: :destroy
has_many :comments, -> {order(:created_at => :desc)}, dependent: :destroy
has_many_attached :images
validates :description, presence: true
has_noticed_notifications model_name: 'Notification'
has_many :notifications, through: :user, dependent: :destroy
validates :title, :presence => true, :length => {
:maximum => 250,
:minimum => 25,
:tokenizer => lambda { |str| str.scan(/\w+/) },
:too_long => "Please limit your summary to %{count} words"
}
end
notification.rb
class Notification < ApplicationRecord
include Noticed::Model
belongs_to :recipient, polymorphic: true
end
comment.rb
class Comment < ApplicationRecord
belongs_to :inquest
belongs_to :user
validates :content, presence: true
after_create_commit :notify_recipient
before_destroy :cleanup_notifications
has_noticed_notifications model_name: 'Notification'
private
def notify_recipient
Commentnotification.with(comment: self, inquest: inquest).deliver_later(inquest.user)
end
def cleanup_notifications
notifications_as_comment.destroy_all
end
end
user.rb
has_many :notifications, as: :recipient
评论notification.rb
def message
@inquest = Inquest.find(params[:comment][:inquest_id])
@comment = Comment.find(params[:comment][:id])
@user = User.find(@comment.user_id)
"#{@user.user_name} commented on #{@inquest.title.truncate(10)}"
end
def URL
inquest_path(Inquest.find(params[:comment][:inquest_id]))
end
问题是您的协会中有 dependent: :destroy
:
class Inquest < ApplicationRecord
belongs_to :user, dependent: :destroy
end
删除了 Inquest
的用户,因此帐户被删除。一个 User
可以有很多 Inquest
并且通常与 User
有很多相关联,所以用其他对象破坏它是一个坏主意。相反,User
模型应该 dependent: :destroy
与一般情况下的关联。