Ruby on rails 轨道&x2B;Carrierwave:如何链接到文件?
我有一个Ruby on rails 轨道&x2B;Carrierwave:如何链接到文件?,ruby-on-rails,Ruby On Rails,我有一个目录,它有一个名为file的属性: class Catalog < ActiveRecord::Base mount_uploader :file, CatalogUploader 而且URL看起来也不正确:http://localhost:3000/Users/emai/Documents/mysite/catalogs/2/vegetarian.png。我知道图片的存在是因为我制作了一个下载文件的链接,并且工作正常。然而,我想显示图片,而不是 编目员: # encodin
目录
,它有一个名为file的属性:
class Catalog < ActiveRecord::Base
mount_uploader :file, CatalogUploader
而且URL看起来也不正确:http://localhost:3000/Users/emai/Documents/mysite/catalogs/2/vegetarian.png
。我知道图片的存在是因为我制作了一个下载文件的链接,并且工作正常。然而,我想显示图片,而不是
编目员:
# encoding: utf-8
class CatalogUploader < 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
# storage :fog
# 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
if Rails.env.test? || Rails.env.cucumber?
"#{Rails.root}/public/test/file_uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
else
"#{Rails.root}/catalogs/#{model.id}"
end
end
# Provide a default URL as a default if there hasn't been a file uploaded:
# def default_url
# # For Rails 3.1+ asset pipeline compatibility:
# # ActionController::Base.helpers.asset_path("fallback/" + [version_name, "default.png"].compact.join('_'))
#
# "/images/fallback/" + [version_name, "default.png"].compact.join('_')
# end
# Process files as they are uploaded:
# process :scale => [200, 300]
#
# def scale(width, height)
# # do something
# end
# Create different versions of your uploaded files:
# version :thumb do
# process :scale => [50, 50]
# 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
# Override the filename of the uploaded files:
# Avoid using model.id or version_name here, see uploader/store.rb for details.
# def filename
# "something.jpg" if original_filename
# end
end
编码:utf-8
类编目器加载程序url
:
<%= link_to "zzzz", catalog.file.url %>
更新
目录阅读器
中的存储目录
方法,如下所示:
def store_dir
if Rails.env.test? || Rails.env.cucumber?
"#{Rails.root}/public/test/file_uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
else
"#{Rails.root}/public/catalogs/#{model.id}" ## Should be in public folder
end
end
class Catalog < ActiveRecord::Base
mount_uploader :file, CatalogUploader, mount_on: :file
图像必须存在于public
文件夹中,以便从web访问。如果不是,Rails将尝试在routes.rb
中定位其路由
此外,我认为您需要更改目录
型号,如下所示:
def store_dir
if Rails.env.test? || Rails.env.cucumber?
"#{Rails.root}/public/test/file_uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
else
"#{Rails.root}/public/catalogs/#{model.id}" ## Should be in public folder
end
end
class Catalog < ActiveRecord::Base
mount_uploader :file, CatalogUploader, mount_on: :file
class目录
根据文件:
:mount_on=>符号
如果要序列化到的列的名称不同,可以使用此选项覆盖它
试试
你能分享目录阅读器的代码吗@Pavan link\u默认为GET。你也可以在错误中看到。我用我的上传程序更新了这个问题file@KirtiThorat哦,好的,谢谢!我试过了,但它仍然返回相同的链接和相同的路由错误。似乎我错了file.url
和file\u-url
是一样的。很乐意帮助。如果解决了问题,请接受答案。:)