[英]Ruby on Rails - Paperclip getting Image
我正在使用Paperclip處理將照片上傳到我的應用程序,但是在檢索照片時遇到了一些問題
我可以成功添加/刪除/顯示所有圖像,但是如何僅獲取模型的一張照片? 喜歡,第一個?
要檢索它們,我使用:
<%= form_for @hotel, :html => {:multipart => true} do |f| %>
<%= f.fields_for :hphotos do |hp|%>
<%= image_tag(hp.object.hphoto.url(:medium))%>
<%end%>
<%end%>
效果很好,但在我的索引中,我只想為顯示的每家酒店展示一張照片。 我越來越 :
undefined method `Hphotos'
I also ran across: undefined method `url' when testing
我懷疑問題出在我的控制器中,但我不確定這是什么(我也放了create動作,效果很好)
def index
@hotels= Hotel.search(params)
@hotels= <pagination>
#I think the issue is here
@hotels.Hphotos.build #changing to @hotels.Hphoto.build or @hotels.hphotos.build won't work and I think my issue is here.
respond_to do |format|
format.html
format.json { render :json => @hotels }
end
end
這工作正常:
def new
@hotel = Hotel.new
2.times { @hotel.hphotos.build }
end
我正在索引視圖上執行此操作:
<% @hotels.each do |hotel|%>
(...)
<%= image_tag hotel.hphoto.url(:small) %>
(...)
<%end%>
就像我說的,我只希望檢索第一個圖像,但是即使在索引中,我也無法全部獲取它們,因此我認為問題出在控制器上。
感謝您的閱讀,歡迎任何提示,即時消息卡住了:)
問候
在索引視圖中,您調用的是hotel.hphoto,但它應該是hphotos,因為它是一對多的關系。 如果您想要第一個,則應該執行以下操作:
hotel.hphotos.first.url(:small)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.