2016-03-15 25 views
0

我很努力與一個image_tag添加一個.where子句。我們的目標是在current_user.brand等於現有品牌時展示Brand品牌的品牌形象。Rails:回形針寶石/ where-clause裏面image_tag

這裏是我的意見代碼:

<%= image_tag ((Brand.where(company: current_user.brand)).logo.url(:thumb)) %>

但是,這是行不通的。我得到這個錯誤:

undefined method `logo' for Brand::ActiveRecord_Relation:0x007ffd06dc2458

在品牌索引頁面本身,我可以告訴這個標誌:

<% @brands.each do |brand| %> 
    <%= image_tag brand.logo.url(:thumb) %> 
<% end %> 

爲品牌表中的DB-方案:

t.string "logo_file_name" 
t.string "logo_content_type" 
t.integer "logo_file_size" 
t.datetime "logo_updated_at" 

你有什麼想法如何實現? 在此先感謝!

回答

2

undefined method `logo' for Brand::ActiveRecord_Relation:0x007ffd06dc2458

你是一個AR關係調用logo,不是在單個記錄,所以是錯誤。

相反,你可以只定義一個實例變量,像下面

@user_brands = Brand.where(company: current_user.brand) 

,並通過它

<% @user_brands.each do |brand| %> 
    <%= image_tag brand.logo.url(:thumb) %> 
<% end %> 
迭代