【问题标题】:How does an :as association work?:as 关联是如何工作的?
【发布时间】:2012-11-28 16:58:51
【问题描述】:

我的 cmets 模型非常简单,并且可以多态工作,但我现在添加了在这些多态关联中隐藏给定记录作者评论的功能。

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

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

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

【问题讨论】:

  • “我现在添加了隐藏给定记录作者的评论的功能” - 这是什么意思?隐藏特定用户提交的所有 cmets?还有,@request 是什么?
  • @MikeCampbell 在原始问题中添加了更多背景信息以进行澄清。
  • 我在努力,但是……你是什么意思?问题是什么?你不能只做comment.commentable.user 吗?
  • @m_x 我要允许隐藏评论的不是评论用户,而是被评论内容的作者。假设作者在帖子上创建帖子和评论者 cmets。我希望帖子的作者能够隐藏评论,而不是评论者。你是说comment.commentable.user 会打电话给帖子的作者吗?
  • @m_x 我在其中一个多态关联上遇到错误,因为它是一个嵌套路由,与其他路由不同。因此,您回答了最初的问题,如果您想发帖,我们很乐意为此感谢您。这个问题现在已经演变成一个新问题,我会尽力回答。

标签: ruby-on-rails polymorphism metaprogramming


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-07
    相关资源
    最近更新 更多