Search code examples
ruby-on-railsmodelcarrierwavelink-to

undefined method 'to_model' when linking thumbnail to original image


I'm creating an album that I'm planning to display with Masonry and ImgZoom so that the visitors can have a bigger image when they click on it.

According to ImgZoom, to make the zoom work, you need to do the following:

<a href="path/to/real/image.png">
    <img src="path/to/image's/thumbnail.png class="thumbnail" />
</a>

So I generated an uploader, with the following inside it:

class ImageUploader < CarrierWave::Uploader::Base

  include CarrierWave::MiniMagick

  storage :file

  def store_dir
    'portfolio/photos'
  end

  version :thumb do
    process :resize_to_fit => [220, nil]
  end
end

Everything works perfectly, I can call both the versions without trouble, but when I try to follow ImgZoom's instructions by doing the following:

<%= @portfolio.photos.each do |p| %> 
#This is a nested form inside the portfolio form, so I need to do this to get my images

<%= link_to image_tag p.image.thumb.url, p.image %>

or:

<%= link_to p.image do %>
    <%= image_tag p.image.thumb.url, :class => 'thumbnail' %>
<% end %>

I'm getting the following error: undefined method 'to_model' for #<ImageUploader:0x0000000c35f4d8>

I found a similar subject on stack overflow but the asker wasn't clear and was invited to ask an other question on the forum, which I couldn't find. I can individually reach 'p.image' and 'p.image.thumb.url', but I can't make a link from one to another, which would be perfectly doable with simple html.

What am I doing wrong?

Thank you in advance


Solution

  • First, to create the class "thumbnail" in the link, you need to declare it properly. I edited the link:

    <%= link_to p.image.url do %>
        <%= image_tag p.image.url, class: "thumbnail" %>
    <% end %>
    

    Second, you need to check if you created an appropriate route for viewing the image. This can be either done by linking to a static assets properly (as your image is not under "public") or via a template view.

    If the files where stored under "public", your way of linking should work just fine.

    Check out how image_path works in the docs: image_path (and more)