我正在尝试使用CarrierWave在本地上传(用于开发)。我的文件上传并保存到
/public/uploads/news_page/hero_img/#{model.id}
但是,当我尝试在视图中调用此图像时,我得到了这条路径:
No route matches [GET] "/uploads/news_page/hero_img/13/7388aa89cc3aef55b4de707420fcd1d7.jpeg"
但那是图像所在的位置,它是由该名称存储在数据库中的(只是图像名称)。我不知道为什么它在视图中显示了破碎的图像。
view.rb
figure
= image_tag @page.hero_img, alt: 'hero'
hero_img_uploader.rb
class HeroImgUploader < CarrierWave::Uploader::Base
include CarrierWave::RMagick
CarrierWave::SanitizedFile.sanitize_regexp = /[^[:word:]\.\-\+]/
# storage :fog
include CarrierWave::MimeTypes
process :set_content_type
storage :file unless Rails.env == "production"
# storage :fog unless Rails.env == "development"
# storage :fog unless Rails.env == "test"
# Override the directory where uploaded files will be stored.
# This is a sensible default for uploaders that are meant to be mounted:
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
# def store_dir
# "pages/#{model.id}/hero/"
# end
# Process files as they are uploaded:
process :resize_to_limit => [1500, 500]
version :md do
process :resize_to_limit => [900, 300]
end
version :sm do
process :resize_to_limit => [420, 140]
end
# Add a white list of extensions which are allowed to be uploaded.
# For images you might use something like this:
def extension_white_list
%w(jpg jpeg gif png)
end
def filename
if original_filename
@name ||= Digest::MD5.hexdigest(File.dirname(current_path))
"#{@name}.#{file.extension}"
end
end
end
development.rb
Rails.application.configure do
# Settings specified here will take precedence over those in config/application.rb.
config.cache_classes = false
# Do not eager load code on boot.
config.eager_load = false
config.serve_static_files = false
config.assets.compile = true
# Show full error reports and disable caching.
config.consider_all_requests_local = true
config.action_controller.perform_caching = false
config.action_mailer.raise_delivery_errors = false
config.active_support.deprecation = :log
config.active_record.migration_error = :page_load
# Debug mode disables concatenation and preprocessing of assets.
config.assets.debug = true
config.assets.digest = true
config.assets.raise_runtime_errors = true
# Configurations for MailCatcher
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = { :address => "localhost", :port => 1025 }
config.action_mailer.default_url_options = { host: 'localhost', port: 5000 }
CarrierWave.configure do |config|
config.storage = :file
end
end
来自&#34; rake路线的相关路线&#34;
pages GET /pages(.:format) pages#index
POST pages(.:format) pages#create
new_page GET pages/new(.:format) pages#new
edit_page GET pages/:id/edit(.:format) pages#edit
page GET pages/:id(.:format) pages#show
PATCH pages/:id(.:format) pages#update
PUT pages/:id(.:format) pages#update
答案 0 :(得分:1)
案件结案!只需更改此行:
config.serve_static_files = false
对此:
config.serve_static_files = true
在config/development.rb
文件中。
可能你之前无意中改变了(在production
环境中它有意义,因为你有另一台服务器来提供资产,例如nginx或Apache)。