[英]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.