i'm using Paperclip to handle the uploading of my photos to my App but am having some issues retrieving them
I can add/remove/display all images successfully but how can I retrieve only one photo of my model? Like, the first one?
To retrieve them all, im using:
<%= form_for @hotel, :html => {:multipart => true} do |f| %>
<%= f.fields_for :hphotos do |hp|%>
<%= image_tag(hp.object.hphoto.url(:medium))%>
<%end%>
<%end%>
which works fine, but in my index, I only want to show one photo for each hotel displayed. im getting :
undefined method `Hphotos'
I also ran across: undefined method `url' when testing
I suspect the issue is in my controller, but as im not sure what, here it is (i'm also putting the create action, which works fine)
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
This works fine:
def new
@hotel = Hotel.new
2.times { @hotel.hphotos.build }
end
I'm doing this on my index view:
<% @hotels.each do |hotel|%>
(...)
<%= image_tag hotel.hphoto.url(:small) %>
(...)
<%end%>
Like I said, I only wished to retrieve the first image, but even in the index I can't get them all, so I think the issue is with the controller.
Thanks for reading, any tips are welcome, im kind of stuck :)
regards
In your index view, you're calling hotel.hphoto but it should be hphotos since it's a one to many relationship. If you want the first one, you should do:
hotel.hphotos.first.url(:small)
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.