2016-03-15 72 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 %> 
迭代