Reputation:
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?
Upvotes: 11
Views: 13352
Reputation: 113
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}"
Upvotes: 1
Reputation: 1885
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)
Upvotes: 14
Reputation: 28920
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
Upvotes: 0
Reputation: 1271
Usually I do in this way
<%= image_tag(portfolio_item.thumb_image_url) unless portfolio_item.thumb_image.nil? %>
Upvotes: -1
Reputation: 4640
I suppose it should be
<%= image_tag portfolio_item.thumb_image.url unless portfolio_item.thumb_image.nil? %>
Upvotes: 9