Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails: how to display image from upload

I'm trying to follow along with Ryan Bates CarrierWave Rails Cast http://railscasts.com/episodes/253-carrierwave-file-uploads, but some things have appeared to change since he made it.

Ryan installs carrier wave on the Painting class

class Painting < ActiveRecord::Base
  attr_accessible :gallery_id, :name, :image
  mount_uploader :image, ImageUploader
end

and then to display the image he does this

<%= image_tag painting.image_url%>

I assume that CarrierWave provides the painting method. I installed Carrier Wave on the User class

class User < ActiveRecord::Base

    attr_accessible :name, :email, :image
    mount_uploader :image, ImageUploader
end

When I tried to do this

 <%= image_tag user.image_url %>

I got an "undefined local variable or method for 'user'" error message

When I tried this

<%= image_tag User.image_url %>

I got undefined methodimage_url' for # Class:0x0000010248e560>`

This latter error message surprised me, because when I did rake routes it showed me this url

image GET    /images/:id(.:format)      {:action=>"show", :controller=>"images"}

This is the file path to the uploaded image

/uploads/user/image/3/cadman.png

but I can't figure out how to display it using a Rails method (i.e. not just img src)

like image 557
Leahcim Avatar asked Jan 14 '12 20:01

Leahcim


1 Answers

Use instance variable @user instead of local variable (that is undefined):

<%= image_tag @user.image_url%>
like image 116
Mark Huk Avatar answered Nov 05 '22 21:11

Mark Huk