【问题标题】:Helper Method detect post - comment from user辅助方法检测用户发表的评论
【发布时间】:2016-02-01 08:44:38
【问题描述】:

我正在尝试创建一个辅助方法,如果用户尚未提交任何帖子,该方法将在用户的个人资料显示视图中显示{user.name} has no submitted posts.",并显示他们拥有的帖子数。目前在我的显示视图中,我有<%= render @user.posts %>,当有 0 个帖子提交时,它什么都不显示。

帖子的部分内容是:

<div class="media">
  <%= render partial: 'votes/voter', locals: { post: post } %>
  <div class="media-body">
    <h4 class="media-heading">
      <%= link_to post.title, topic_post_path(post.topic, post) %>
      <%= render partial: "labels/list", locals: { labels: post.labels } %>
    </h4>
    <small>
      submitted <%= time_ago_in_words(post.created_at) %> ago by <%= post.user.name %> <br>
      <%= post.comments.count %> Comments
    </small>
  </div>
</div>

我试过了:

  def no_post_submitted?(user)
      user.post.count(0)
      "{user.name} has not submitted any posts yet."
  end

在我的用户显示视图上:

<%= if no_post_submitted?(@user) %>
<%= render @user.posts %>

我很确定这是错误的,但我不知道如何实现这个方法。

【问题讨论】:

    标签: ruby-on-rails helpermethods


    【解决方案1】:

    你在哪里使用render @user.posts你可以添加一个简单的条件:

    <% if @user.posts.empty? %>
      <p><%= @user.name %> has no submitted posts</p>
    <% else %>
      <%= render @user.posts %>
    <% end %>
    

    除非您需要在多个地方使用它,否则为此创建一个帮助器没有多大意义。

    【讨论】:

    • 非常感谢您成功了。我想,但出于某种原因,我认为制作一个辅助方法会使这更容易,但不是真的。
    【解决方案2】:

    如果集合为空,则渲染集合返回 nil,因此您可以使用 ||运营商:

    <%= render @user.posts || "{@user.name} has not submitted any posts yet." %>
    

    或者如果有更多代码渲染另一个部分:

    <%= render @user.posts || render 'no_posts' %>
    

    【讨论】:

      【解决方案3】:

      在 Ruby 中方法会自动返回最后一个值,所以这个方法:

      def no_post_submitted?(user)
        user.post.count(0)
        "{user.name} has not submitted any posts yet."
      end
      

      将始终返回一个字符串 - 如果您在条件中使用字符串文字,它将被评估为 true,并发出警告 warning: string literal in condition。这也不是您使用 count 的方式 - 传递 0 将导致它查询第 0 列或只是错误。

      所以要修复你会做的方法:

      def no_post_submitted?(user)
        user.posts.empty?
      end
      

      但是,条件是如此简单,以至于它并不能真正保证辅助方法。相反,您只需编写:

      <%= if user.post.any? %>
        <%= render @user.posts %>
      <% else %>
        <%= "{user.name} has not submitted any posts yet." %>
      <% end %>
      

      【讨论】:

        【解决方案4】:

        您的解决方案存在几个问题。请记住,rails 更多的是关于约定而不是配置。

        您的方法no_post_submitted? 实际上应该返回true/false,因为它是一个以? 结尾的方法。为了清楚起见,它还应该命名为no_posts_submitted?。它应该看起来像这样:

          def no_post_submitted?(user)
            user.posts.count > 0
          end
        

        然后,应该有另一个帮助方法可以打印您所需的消息,例如:

        def no_posts_message(user)     
          "{user.name} has not submitted any posts yet."
        end
        

        最终你们都可以像这样插入它:

        <% if no_posts_submitted?(user) %>
         <%= no_posts_message(user) %>
        <% else>
         <%= render @user.posts %>
        <% end %>
        

        【讨论】:

          【解决方案5】:

          根据docs

          如果集合为空,render 将返回 nil,因此提供替代内容应该相当简单。

          <h1>Products</h1>
          <%= render(@products) || "There are no products available." %>
          

          --

          所以...

            <%= render(@user.posts) || "#{@user.name} has not submitted any posts yet." %>
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2010-12-24
            • 2011-12-18
            • 1970-01-01
            • 2011-08-03
            相关资源
            最近更新 更多