简体   繁体   中英

Rails: Paperclip gem / where-clause inside image_tag

I'm struggling with adding a .where-clause to an image_tag. The goal is to show the Brand image from the table Brand when the current_user.brand is equal to an existing 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

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

You are calling logo on an AR relation , not on a single record , so is the error.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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