在gemfile中,我安装了carrierwave,允许用户上传个人资料图片。我按照此railscast中的说明进行了操作 - http://railscasts.com/episodes/253-carrierwave-file-uploads
gem 'carrierwave'
但是图像不会与image_tag一起显示。我可以通过在没有image_tag的情况下测试它来查看图像目录。我还在图像目录中导航以确保存储图像。我可以在导演“/ uploads / user / image / 110 /”
中看到它
在视图中显示图片网址“/ uploads / user / image / 110 / thumb_IMG_0017.JPG”
<%= @user.image_url(:thumb).to_s %>
添加图片代码不会显示任何内容
<%= 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
控制器
def show
@user = User.find(params[:id])
...
end
private
def user_params
params.require(:user).permit(:name, :email, :password, :password_confirmation, :image)
end
模型
class User < ActiveRecord::Base
...
mount_uploader :image, ImageUploader
...
end
直接查询数据库,例如用户110,显示以下内容
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"
答案 0 :(得分:3)
您需要在设置carrierwave之后重新启动服务器。这将解决所有问题。
答案 1 :(得分:1)
好的,我检查了服务器日志
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"):
通过进入config / environments / production.rb和development.rb并将以下内容设置为true,看起来问题会得到解决!
config.serve_static_assets = true