Rails: how to display image from upload

11,608

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

<%= image_tag @user.image_url%>
Share:
11,608
Leahcim
Author by

Leahcim

I'm pretty hopeless at programming but I'm enjoying trying to learn it (rails, php, javascript, jquery)... Backbone underscore twitterbootsrap http://jsfiddle.net/mjmitche/RRXnK/119/

Updated on June 13, 2022

Comments

  • Leahcim
    Leahcim almost 2 years

    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)