繁体   English   中英

Rails:回形针宝石/ image_tag中的子句

[英]Rails: Paperclip gem / where-clause inside image_tag

我正在努力为image_tag添加.where子句。 目标是当current_user.brand等于现有Brand时,从Brand表中显示Brand图像。

这是我认为的代码:

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

但这是行不通的。 我收到此错误:

品牌:: ActiveRecord_Relation:0x007ffd06dc2458的未定义方法`logo'

我可以在“品牌索引”页面本身上显示徽标:

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

品牌表的数据库方案:

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

您有什么想法要实现吗? 提前致谢!

品牌:: ActiveRecord_Relation:0x007ffd06dc2458的未定义方法`logo'

您是在AR关系上而不是在单个记录上调用logo ,因此错误也是如此。

相反,您可以只定义一个实例变量,如下所示

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

并遍历它

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM