我有一個相當複雜的(在我)語句寫在Rails中。在我的Person.rb
這是用戶的個人資料的show.html.erb
下。每個post
在數據庫中有一個status
列,值爲draft
或published
。簡化這個條件語句
<% if @posts.blank? %>
No post.
<% else %>
<% if @person == current_user %>
<% @posts.each do |post| %>
Post title... (where all published and draft posts are shown)
<% end %>
<% else %>
<% @posts.each do |post| %>
<% if post.status == "published" %>
Post title (where only published posts are shown, draft posts are hide from other users)
<% end %>
<% end %>
<% end %>
<% end %>
問題出在這裏。假設User A
有1條已發佈帖子和1條發表帖子,工作正常;但是如果User B
有兩篇草稿文章,則右側的任何文章都不會顯示給其他用戶,但是我希望它像第一個2行代碼一樣顯示No post
。
我試過named_scope
在我的Post.rb
指導here,但是當我申請.published時,它只是返回了一個未定義的錯誤。
請教我如何做到這一點。非常感謝你。
這是rails 3嗎?我想在rails 3中使用範圍:published => where(:published => true) – corroded 2011-02-08 04:41:07
它是Rails 2.3.8。對不起,以前沒有說過。 – Victor 2011-02-08 05:19:48