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

Different notification partials for different models?

notifications/index<%= 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) %> 

显示:

这是有问题的,因为它应该说 3x ".com 评论了你的习惯" 和 2x ".com 评论了你的价值"=47= ].

我们需要创建两个单独的部分 notifications/_habits & notifications/_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/

瓦拉丹更新

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

您的通知与评论相关联,评论可以包含习惯或价值类型的评论(您没有显示这两个模型,所以我们称它们为习惯和价值模型)。 因此,您可以通过检查可评论类型来检查通知是针对习惯还是价值:

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 %>

我需要

<% 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 %>

并在评论模型中:

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