简体   繁体   中英

Ruby on Rails image_tag not displaying the image

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

在此处输入图片说明

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" 

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM