【问题标题】:How to create view partial for this notification如何为此通知创建部分视图
【发布时间】:2019-03-31 00:09:40
【问题描述】:

我已经设置了一个通知,每当用户对一本书发表评论时,book.user 都会在以下部分收到通知:views/notifications/_book.html.erb。同时,我尝试为用户的关注者创建通知,以便他们关注的用户发布新书。 IMO,应该创建一本书部分来呈现通知视图,但我已经有部分书的副本来显示评论通知。现在我不知道我是否仍然可以在同一个文件中进行调整或创建其他内容。我正在使用https://github.com/rails-engine/notifications

我已经实现了以下关系,这在本教程https://www.devwalks.com/lets-build-instagram-with-ruby-on-rails-part-6-follow-all-the-people/的应用程序上运行良好

这就是我到目前为止所做的代码

用户.rb

  has_many :books, dependent: :destroy
  has_many :chapters, dependent: :destroy
  has_many :reviews, dependent: :destroy
  has_many :genres
  has_many :ratings

review.rb

belongs_to :book
belongs_to :user

after_commit :create_notifications, on: :create

private

def create_notifications
    Notification.create do |notification|
       notification.notify_type = 'book'
       notification.actor = self.user
       notification.user = self.book.user
       notification.target = self
       notification.second_target = self.book
    end
end

视图/通知/_book.html.erb

<div class=''>
  <%= link_to notification.actor.username, main_app.profile_path(notification.actor.username) %> reviewed
  <%= link_to notification.second_target.title, main_app.book_path(notification.second_target) %>
</div>

<div class=''>
  <% unless notification.target.blank? %>
    <div class="review-rating" data-score="<%= notification.target.rating %>"></div>
    <%= notification.target.comment %>
  <% end %>
</div>

book.rb

belongs_to :user
has_many :chapters, dependent: :destroy

after_commit :create_notifications, on: :create

    def create_notifications
        self.user.followers.each do |follower|
          Notification.create(notify_type: 'book', 
                              actor: self.user,
                              user: follower, 
                              target: self, 
                              second_target: self.book)

        end
    end

那么,如果我必须渲染同一个部分,我应该怎么做?或者也许我必须用另一种方式来做?

【问题讨论】:

    标签: ruby-on-rails ruby


    【解决方案1】:

    经过大量研究,我知道必须创建一个 notify_type 部分,它也必须对应于本书的 notify_type。

    即我创建了一个

    notify_type: "new_book"
    

    部分名称='_new_book.html.erb'

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-14
      • 1970-01-01
      相关资源
      最近更新 更多