1

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" 
7

2 Answers 2

3

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

1
  • 1
    I restarted it, it didn't fix the problem. Do I need the public folder in the route?
    – cloudviz
    Jul 12, 2014 at 9:44
1

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
3
  • Are you in production mode? That will affect things. Are you on heroku or something? Jul 12, 2014 at 10:23
  • Production is heroku. Right now I'm in my local development machine.
    – cloudviz
    Jul 12, 2014 at 10:32
  • Just a heads up here... DEPRECATION WARNING: The configuration option config.serve_static_assets has been renamed to config.serve_static_files to clarify its role (it merely enables serving everything in the public folder and is unrelated to the asset pipeline). The serve_static_assets alias will be removed in Rails 5.0. Please migrate your configuration files accordingly.
    – brntsllvn
    Jun 26, 2015 at 16:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.