10

I am receiving this error:

Can't resolve image into URL: undefined method `to_model' for #<PortfolioUploader:0x007fd1538a2a60>
Did you mean?  to_xml

I am using carrierwave rails and when I check my form looks like this:

  <% @portfolios.each do |portfolio_item| %>
  <p><%= portfolio_item.title %> </p>
  <p><%= image_tag portfolio_item.thumb_image unless portfolio_item.thumb_image.nil? %> </p>
  <p><%= portfolio_item.body %> </p>
  <p><%= link_to "Edit", edit_portfolio_path(portfolio_item) if logged_in?(:site_admin)%></p>
  <p><%= link_to "Delete", portfolio_path(portfolio_item), method: :delete, data: { confirm: 'Are you sure?' } if logged_in?(:site_admin)%></p>
  <% end %>

And on my form:

<%= render 'form', portfolio: @portfolio %>




 <div class="field">
    <%= f.file_field :main_image %>
  </div>

  <div class="field">
    <%= f.file_field :thumb_image %>
 </div>

On my model I put the ff:

  mount_uploader :thumb_image, PortfolioUploader
  mount_uploader :main_image, PortfolioUploader

Any idea what am I missing?

Promise Preston
  • 24,334
  • 12
  • 145
  • 143

5 Answers5

12

If you just upgraded to Rails 5.2, the behaviour of image_tag changed between Rails 5.1 and 5.2.

In Rails 5.1, you could pass an uploader object, like in the question asked:

# Rails 5.1
image_tag(portfolio_item.thumb_image)

But in Rails 5.2, image_tag now expects a url:

# Rails 5.2
image_tag(portfolio_item.thumb_image_url)
jibai31
  • 1,663
  • 18
  • 22
9

I suppose it should be

<%= image_tag portfolio_item.thumb_image.url unless portfolio_item.thumb_image.nil? %>
Vasilisa
  • 4,604
  • 3
  • 20
  • 25
1

I had a similar issue, resolved using an interpolated string of the image url. Instead of

= image_tag portfolio_item.thumb_image

I used

= image_tag "#{portfolio_item.thumb_image}"
Hecatonchier
  • 103
  • 3
  • 6
0

This should work perfectly for you, even when no image is uploaded:

<%= image_tag(portfolio_item.thumb_image_url) if portfolio_item.thumb_image_url %>

Note: The if statement is to avoid errors when no image is present or has been uploaded.

That's all.

I hope this helps

Promise Preston
  • 24,334
  • 12
  • 145
  • 143
-1

Usually I do in this way

<%= image_tag(portfolio_item.thumb_image_url) unless portfolio_item.thumb_image.nil? %>
Mauro
  • 1,241
  • 22
  • 26