1
votes

In the gemfile I have installed carrierwave to allow a user to upload a profile image. I sort of followed the instructions in this railscast - http://railscasts.com/episodes/253-carrierwave-file-uploads

gem 'carrierwave'

However image is not displaying with image_tag. I can see the image directory by testing it without the image_tag. I also navigated in the image directory to ensure that image is stored. I can see it in the director "/uploads/user/image/110/"

enter image description here

In the view it shows the image url "/uploads/user/image/110/thumb_IMG_0017.JPG"

<%= @user.image_url(:thumb).to_s %>

Adding an image tag displays nothing

<%= image_tag @user.image_url(:thumb).to_s %>

ImageUploader

class ImageUploader < CarrierWave::Uploader::Base

  # Include RMagick or MiniMagick support:
  #include CarrierWave::RMagick
  include CarrierWave::MiniMagick

  # Choose what kind of storage to use for this uploader:
  storage :file

  def store_dir
    "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
  end

  version :thumb do
    process :resize_to_limit => [200, 200]
  end


end

Controller

def show
    @user = User.find(params[:id])
    ...
end

private

    def user_params
      params.require(:user).permit(:name, :email, :password, :password_confirmation, :image)
    end

Model

class User < ActiveRecord::Base
    ...

    mount_uploader :image, ImageUploader

    ...
end

Querying the database directly for example for user 110 displays the following

User.find(110).image_url(:thumb)
User Load (2.1ms)  SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1  [["id", 110]]
 => "/uploads/user/image/110/thumb_IMG_0017.JPG" 
2
Any errors / logs you can report back with? - Richard Peck
The image just comes back with 404 or something? what happens on the page itself? - Richard Peck
Ok I tried doing this - localhost:3000/uploads/user/image/110/thumb_IMG_0017.JPG. I get No route matches [GET] "/uploads/user/image/110/thumb_IMG_0017.JPG" - cloudviz
Do you have the uploads folder in your /public directory? - Richard Peck
uploads folder is under the public folder directory. Am I meant to do this localhost:3000/public/uploads/user/image/110/… still get the same route matches get error - cloudviz

2 Answers

3
votes

You need to restart your server after setting carrierwave up. That will fix everything.

1
votes

Ok I checked the server logs

Checked the server log looks like a routing issue - Started GET "/uploads/user/image/111/thumb_IMG_1355.JPG" for 127.0.0.1 at 2014-07-12 11:00:29 +0100

ActionController::RoutingError (No route matches [GET] "/uploads/user/image/111/thumb_IMG_1355.JPG"):

It looks like the issue will get fix by going into config/environments/production.rb and development.rb and setting the following to true!

config.serve_static_assets = true