0

在我的应用程序中,我使用 gem 邮箱来处理我的通知,并且我已经启动并运行了所有内容,但是我无法弄清楚如何链接到导致通知的对象。例如,我有一个属于多个模型的评论模型,我希望能够显示评论所属的可评论模型的链接。我不能只打电话<%= link_to "View", notification.notified_object %>,因为它会尝试链接到实际评论,我想要链接到它所属的状态/项目/事件,我不能只打电话commentable。关于如何做到这一点的任何想法?提前致谢。

控制器

class CommentsController < ApplicationController

  before_filter :authenticate_member!
  before_filter :load_commentable
  before_filter :find_member

  def index
    redirect_to root_path
  end

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

  def create
    @comment = @commentable.comments.new(params[:comment])
    @comment.member = current_member
    if @comment.save
      redirect_to :back
    else
      redirect_to :back
    end
  end

  def destroy
    @comment = Comment.find(params[:id])
    respond_to do |format|
      if @comment.member == current_member || @commentable.member == current_member
        @comment.destroy
        format.html { redirect_to :back }
      else
        format.html { redirect_to :back, alert: 'You can\'t delete this comment.' }
      end
    end
  end

  private

  def load_commentable
    klass = [Status, Medium, Project, Event, Listing].detect { |c| params["#{c.name.underscore}_id"] }
    @commentable = klass.find(params["#{klass.name.underscore}_id"])
  end

  def find_member
    @member = Member.find_by_user_name(params[:user_name])
  end

end

模型

class Comment < ActiveRecord::Base
  belongs_to :member
  belongs_to :commentable, polymorphic: true
  attr_accessible :content

  validates :content, presence: true,
            length: { minimum: 2, maximum: 280 }

  after_create :create_notification, on: :create

  def create_notification
    subject = "#{member.user_name}"
    body = "wrote you a <b>Comment</b> <p><i>#{content}</i></p>"
    commentable.member.notify(subject, body, self)
  end
end

看法

<% @notifications.each do |notification|%>
  <% @notification = notification %>

  <div>
    <%= link_to(notification.subject, "#{root_url}#{notification.subject}") %>&nbsp;<%= Rinku.auto_link(truncate(notification.body, :length => 400)).html_safe %>
    <span class="not_meta"><%= time_ago_in_words(notification.created_at) %></span>
  </div>

  <div class="view">
    <% if notification.notified_object_type == 'Comment' %>
      <%= link_to("#") do %>
        <i class="icon-eye-open icon-green"></i> View
      <% end %>
      </div>
    <% end %>

迁移

class CreateComments < ActiveRecord::Migration
  def change
    create_table :comments do |t|
      t.text :content
      t.belongs_to :commentable, polymorphic: true
      t.references :member

      t.timestamps
    end
    add_index :comments, [:commentable_id, :commentable_type]
    add_index :comments, :member_id
  end
end

#Notifications and Messages
create_table :mailboxer_notifications do |t|
  t.column :type, :string
  t.column :body, :text
  t.column :subject, :string, :default => ""
  t.references :sender, :polymorphic => true
  t.column :conversation_id, :integer
  t.column :draft, :boolean, :default => false
  t.string :notification_code, :default => nil
  t.references :notified_object, :polymorphic => true
  t.column :attachment, :string
  t.column :updated_at, :datetime, :null => false
  t.column :created_at, :datetime, :null => false
  t.boolean :global, default: false
  t.datetime :expires
end

我的评论资源路由嵌套在它所属的每个模型下。

4

1 回答 1

0

不知道我是怎么错过的,但我可以commentable像这样调用通知的对象notification.notified_object.commentable,这会给我我正在寻找的路线。

于 2014-06-23T00:44:01.780 回答