2012-11-28 36 views
0

我的评论模型非常简单并且可以多形地工作,但我现在增加了隐藏特定记录的作者对这些多态关联进行评论的功能。作为协会如何工作?

class Comment < ActiveRecord::Base 
    attr_accessible :content, :show 
    belongs_to :commentable, :polymorphic => true 
    belongs_to :user 
end 

所以,要求,问题,帖子,提交等等都有意见,并访问没有问题的评论模板,但我想允许这些模型的内容的作者,以显示或隐藏评论(而不是标记),当应用程序将它们标识为正在评论的内容的作者时。

class Request < ActiveRecord::Base 
    has_many :comments, :as => :commentable, :dependent => :destroy 
end 

所以,我有一切工作的时候,只有一个模式,通过调用作者:@ request.user, 但我不知道如何使用元编程调用一个作家,所以注释视图(帮助)可以确定当前使用评论视图的模型。

我已经对元编程做了一些研究,但还没有找到答案。

这里是调用的作者(@ request.user)代码:

<% if @comments %> 
     <h1 class="mtop20">Comments</h1> 
     <% for comment in @comments %> 
      <% if signed_in? %> 
       <% if comment.show == true %> 
        <div class="well comment mtop10"> 
         <% if current_user == @request.user or current_user.has_role? :admin %> 
          <%= simple_form_for [@commentable, comment] do |f| %> 
           <div class =""> 
           <%= f.input :show, :as => :hidden, :input_html => { :value => false } %> 
           <%= f.submit "Hide Comment", :class => 'btn btn-mini pull-right' %> 
           </div> 
          <% end %> 
         <% end %> 
         <span> 
          <%= image_tag comment.user.image.source(:header) %> 
          <%= link_to comment.user.name, comment.user %></span> 
          Posted <%= time_ago_in_words(comment.created_at) %> ago 
         </span> 
         <p class="mleft20 mtop10"><%= comment.content %></p> 
         <% if signed_in? %> 
          <% if current_user.id == comment.user_id or current_user.has_role? :admin %> 
           <%= link_to 'Edit', polymorphic_path([ comment.commentable, comment], :action => :edit), 
                :class => 'btn btn-mini mtop5 mleft10' %> 
           <%= link_to 'Delete', [comment.commentable, comment], 
                :confirm => 'Are you sure?', 
                method: :delete, 
                :class => 'btn btn-mini mtop5' %> 
          <% end %> 
         <% end %> 
        </div> 
       <% end %> 
       <% if comment.show == false %> 
        <p>A comment by <%= comment.user.name %> has been hidden by <%= @request.user.name %></p> 
        <% if current_user == @request.user or current_user.has_role? :admin %> 
        <%= simple_form_for [@commentable, comment] do |f| %> 
         <div class =""> 
         <%= f.input :show, :as => :hidden, :input_html => { :value => true } %> 
         <%= f.submit "Show Comment", :class => 'btn btn-mini btn-success' %> 
         </div> 
        <% end %> 
        <% end %> 
       <% end %> 
      <% end %> 
     <% end %> 
    <% end %> 
    <%= render "comments/form" %> 
+0

“我现在增加了隐藏给定记录的作者的评论的能力” - 你是什么意思?隐藏特定用户提交的所有评论?另外,什么是@request? –

+0

@MikeCampbell在原始问题中增加了更多背景信息以阐明。 –

+0

我在努力,但是...你是什么意思?什么是问题?你不能只做'comment.commentable.user'吗? –

回答

1

使用comment.commentable.user访问您的帖子的作者。