How do I create a notification partial for this view?
opened this issue · comments
I've already setup a notification for whenever a user posts a review on a book, the book.user gets notified in this partial: views/notifications/_book.html.erb. At the same time, I trying to create notification to a user's followers for whenever the user they're following posts a new book. IMO, supposed to create a book partial to render the notification view but I already have a duplicate of the book partial to show reviews notifications. Now I don't know if I can still tweak in in the same file or create something else.
I've implemented following relationships which is working well on my app from this tutorial https://www.devwalks.com/lets-build-instagram-with-ruby-on-rails-part-6-follow-all-the-people/
This is what I've done so far with the codes
user.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
views/notifications/_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
So, if I've to render in the same partial, how should I? or maybe I've to do it another way?
The notify_type
means the action.
By your case,
- a user posts a review on a book, the
notify_type
you can usenotify_type: "review_book"
- to a user's followers for whenever the user they're the following posts a new book, you can use
notify_type: "new_book"
Use the different notify_type
names for the different cases.