我想創建一個幫助器方法,將顯示{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 %>
其即時通訊超過肯定是錯誤的,但我有不知道如何實現這個方法。
非常感謝你這並獲得成功。我想,但由於某種原因,我認爲製作一個輔助方法會使這個更容易,但不是真的。 –