0

I'm creating an image gallery website, and have some problems. I tried searching, but it's hard because i cannot really describe this problem.

i want to have thumbnail images on my index page, but i can't get it working.

why does this code inside /views/index.html.erb

    <% @galleries.each do |gallery| %>
    <%= image_tag gallery.photos(:small).last %>
    <%= link_to gallery.title, gallery_path(gallery) %>

doesn't show me photos, just blank spaces where photos supposed to appear. When i right click on them to open in new tab, it links to /images/#..... and gives No routes error.

On show.html.erb pictures are shown with this code:

      <% @gallery.photos.each do |photo|%>
      <%= image_tag photo.image(:medium) %>
     <% end %>

models are: gallery has many photos, photos belongs to gallery.

gallery controller: def index @galleries = Gallery.all.order(created_at: :desc) end

    def show
        @gallery = Gallery.find(params[:id])
    end

routes: resources :galleries resources :photos

1 Answer 1

1

@rubynuby: Just try this:

<% @galleries.each do |gallery| %>
<%= image_tag gallery.photos.last.image.url(:small) %>
<%= link_to gallery.title, gallery_path(gallery) %>

As what I observed form you question is:

in models/gallery.rb

class Gallery
  has_many :photos
end

in models/photo.rb

class Photo
  belongs_to :gallery
  has_attached_file :image, styles: {
                      small:  '150x150>',
                      medium: '600X600>'
                    }

end

So as per your requirement, you need call image on photo, i.e. photo.image.url(<style>)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.