简体   繁体   English

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

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

I'm struggling with adding a .where-clause to an image_tag. 我正在努力为image_tag添加.where子句。 The goal is to show the Brand image from the table Brand when the current_user.brand is equal to an existing Brand. 目标是当current_user.brand等于现有Brand时,从Brand表中显示Brand图像。

Here is the code in my views: 这是我认为的代码:

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

But this does not work. 但这是行不通的。 I'm getting this error: 我收到此错误:

undefined method `logo' for Brand::ActiveRecord_Relation:0x007ffd06dc2458 品牌:: ActiveRecord_Relation:0x007ffd06dc2458的未定义方法`logo'

On the Brand index page itself I can show the logo with this: 我可以在“品牌索引”页面本身上显示徽标:

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

The DB-scheme for the Brand table: 品牌表的数据库方案:

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

Do you have any ideas how to achieve that? 您有什么想法要实现吗? Thanks in advance! 提前致谢!

undefined method `logo' for Brand::ActiveRecord_Relation:0x007ffd06dc2458 品牌:: ActiveRecord_Relation:0x007ffd06dc2458的未定义方法`logo'

You are calling logo on an AR relation , not on a single record , so is the error. 您是在AR关系上而不是在单个记录上调用logo ,因此错误也是如此。

Instead you can just define an instance variable like below 相反,您可以只定义一个实例变量,如下所示

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

and iterate through it 并遍历它

<% @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