不同模型的不同通知部分?

时间:2015-06-01 23:39:51

标签: ruby-on-rails ruby notifications

通知/索引<%= render partial: "notifications/notification", collection: @notifications %>,其中包含:

<%= link_to "", notifications_habit_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 habit", habit_path(notification) %> 

显示:

enter image description here

这是有问题的,因为它应该说3x &#34; .com评论了你的习惯&#34; 和2x &#34; .com评论了你的价值&#34; < / em>的

我们需要创建两个单独的部分 notifications / _habits &amp; 通知/ _values

我的困惑是如何让代码知道什么时候根据习惯或价值来指导习惯部分或价值部分。

notifications_controller

def index
  @habits = current_user.habits
  @valuations = current_user.valuations #aka values
  @notifications = current_user.notifications
  @notifications.each do |notification|
    notification.update_attribute(:read, true)
end

通知基于用户是否评论您的某个习惯或价值观:

comment.rb

class Comment < ActiveRecord::Base
    after_save :create_notification
  has_many :notifications
    belongs_to :commentable, polymorphic: true
    belongs_to :user
  validates :user, presence: true

private

  def create_notification
      Notification.create(
       user_id: self.user_id,
       comment_id: self.id,
       read: false
      )
  end
end

我遵循了本教程,但它基于仅使用一个模型:http://evanamccullough.com/2014/11/ruby-on-rails-simple-notifications-system-tutorial/

VALADAN的更新

class CommentsController < ApplicationController
    before_action :load_commentable
  before_action :set_comment, only: [:show, :edit, :update, :destroy, :like]
  before_action :logged_in_user, only: [:create, :destroy]

    def index
        @comments = @commentable.comments
    end

    def new
        @comment = @commentable.comments.new
    end

    def create
        @comment = @commentable.comments.new(comment_params)
        if @comment.save
            redirect_to @commentable, notice: "comment created."
        else
            render :new
        end
    end

    def edit
        @comment = current_user.comments.find(params[:id])
    end

    def update
        @comment = current_user.comments.find(params[:id])
        if @comment.update_attributes(comment_params)
            redirect_to @commentable, notice: "Comment was updated."
        else
            render :edit
        end
    end

    def destroy
        @comment = current_user.comments.find(params[:id])
        @comment.destroy
        redirect_to @commentable, notice: "comment destroyed."
    end

  def like
    @comment = Comment.find(params[:id])
    @comment_like = current_user.comment_likes.build(comment: @comment)
    if @comment_like.save
            @comment.increment!(:likes)
        flash[:success] = 'Thanks for liking!'
    else
        flash[:error] = 'Two many likes'
      end  
        redirect_to(:back)
  end

private

  def set_comment
    @comment = Comment.find(params[:id])
  end

    def load_commentable
        resource, id = request.path.split('/')[1, 2]
        @commentable = resource.singularize.classify.constantize.find(id)
    end

    def comment_params
        params[:comment][:user_id] = current_user.id
        params.require(:comment).permit(:content, :commentable, :user_id, :like)
    end
end

2 个答案:

答案 0 :(得分:1)

您的通知与评论相关联,并且评论可以评论类型为Habit或Value(您没有显示这两个模型,因此我们称之为Habit和Value模型)。 因此,您可以通过检查可评论类型来检查通知是否适用于习惯或价值:

Comment.find_by(notification.comment_id).commentable.class == Habit

或检查其值通知:

Comment.find_by(notification.comment_id).commentable.class == Value

类似的方法是检查注释上的多态类型,例如:

Comment.find_by(notification.comment_id).commentable_type == 'Habit'

所以最后,你实际上不需要两个部分只有IF和两个不同的link_to,一个用于值,一个用于习惯。

<%= link_to "", notifications_habit_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 
<% if Comment.find_by(notification.comment_id).commentable.class == Habit %>
<%= link_to "your habit", habit_path(notification) %>
<% else %>
<%= link_to "your value", value_path(notification) %>
<% end %>

答案 1 :(得分:0)

我需要

typedef NSComparisonResult (^NSComparator)(id obj1, id obj2);

并在评论模型中:

<% if notification.habit_id %>
        <%= link_to "your habit", habit_path(notification) %>
<% elsif notification.valuation_id %>
        <%= link_to "your value", valuation_path(notification) %>
<% elsif notification.quantified_id %>
        <%= link_to "your stat", quantified_path(notification) %>
<% elsif notification.goal_id %>
        <%= link_to "your goal", goal_path(notification) %>
<% end %>