2014-06-20 2 views
0

내 앱에서 내 메일 알림을 처리하기 위해 보석 사서함을 사용하고 있으며 모든 기능을 갖추고 있지만 알림을 유발 한 객체를 link_to하는 방법을 파악하는 데 문제가 있습니다. 예를 들어, 여러 모델에 속한 의견 모델이 있는데 의견이 속한 의견 모델에 대한 링크를 표시 할 수 있기를 원합니다. 나는 단지 <%= link_to "View", notification.notified_object %>이라고 부를 수는 없으므로 실제 댓글에 링크를 시도 할 것이며 상태/프로젝트/이벤트가 속한 링크를 원합니다. 단지 commentable으로 전화 할 수 없습니다. 이 작업을 수행하는 방법에 대한 아이디어가 있습니까? 미리 감사드립니다.Rails 알림 라우팅

컨트롤러

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

Migrati 기능

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 

내 의견 자원 경로는이 속한 각 모델에서 중첩됩니다.

답변

0

잘 모르겠습니다. 그러나 commentable을 알려주는 객체 (예 : notification.notified_object.commentable)를 호출하면 해당 경로를 찾을 수 있습니다.