0

私のアプリでは、gem メールボックスを使用して通知を処理しており、すべてを起動して実行していますが、通知の原因となったオブジェクトに link_to する方法がわかりません。たとえば、複数のモデルに属するコメント モデルがあり、そのコメントが属するコメント可能なモデルへのリンクを表示できるようにしたいと考えています。<%= 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 に答える