簡體   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