2013-10-09 31 views
0

項目我使我的項目的通過<%= render @items%>渲染只與特定的ID

我添加了一個遷移AddCatIdToItems cat_id:string

CAT_ID的:種皮,TESTB,TESTC。

我該如何渲染只有特定ID爲testA的物品?

例如爲:<&= @items.where(:cat_id => '1')&>

我使用的種子文件來填充項。例如:

Tag.create([ 
      { name: 'example' }, 
      { name: 'example2' }, 
      { name: 'example3' } 
      ]) 

我該如何將cat_id傳遞給那個?

{ name: 'example', cat_id:1 }可用?

+0

你爲什麼不過濾你的記錄,當你從數據庫獲取它們? –

+0

你是什麼意思? :) –

+0

請顯示如何在控制器中設置'@ items'變量。 –

回答

2

我不確定你在問什麼。但我想這是你想要的

@items = Item.where("cat_id LIKE '%testA%'") 

如果您想選擇所有具有特定CAT_ID的項目,然後

@items = Item.where(:cat_id => 'testA') 
+0

我正在尋找一種只用特定的cat_id渲染項目的方法。 –

+0

更新了我的答案,這對你有幫助嗎? – usha

+0

我仍然需要render @items才能正常工作。我有4個選項卡,其中我需要呈現4個特定的cat_id。該代碼進入我的控制器嗎? –