2012-09-18 23 views
0

現在我正在使用下面的代碼列出所有用戶的微博。RoR:如何只列出在一列中具有特定屬性的微博?

<div class="span8"> 

      <% if @user.microposts.any? %> 
      <h3>Purchases I am interested in (<%= @user.microposts.count %>)</h3> 
       <ol class="microposts"> 
       <%= render @microposts %> 
       </ol> 
       <%= will_paginate @microposts %> 
      <% end %> 
      </div> 

,並呈現爲_micropost.html.erb的觀點如下

<li> 
    <span class="content"><%= micropost.content %></span> 
    <span class="timestamp"> 
    Posted <%= time_ago_in_words(micropost.created_at) %> ago. 
    </span> 
    <% if current_user?(micropost.user) %> 
    <%= link_to "delete", micropost, method: :delete, 
            confirm: "You sure?", 
            title: micropost.content %> 
    <% end %> 
</li> 

所以這個工作得很好,但是我改變的事情了。每個micropost都有一個hidden_​​tag_field,它是一個名爲kind的字符串(和數據庫中的一列)。它可以是「購買」或「銷售」。我想在一個地方列出所有購買微博,並在另一個地方列出所有銷售微博。我怎樣才能改變微觀視圖來做到這一點?

回答

0

控制器:

@purchases = @microposts.where(:kind => 'purchase') 
@sales = @microposts.where(:kind => 'sale') 

查看:

<h2>Purchases</h2> 
<%= render @purchases %> 

<h2>Sales</h2> 
<%= render @sales %> 
+0

這是一個很好的主意。我仍然得到一個錯誤,但我決定我需要重寫這裏的問題(http://stackoverflow.com/questions/12469755/ror-nil-is-not-an-activemodel-compatible-object-that-returns-a-有效-局部的) – BigBoy1337

相關問題