gpt4 book ai didi

ruby-on-rails - rails + 载波 : How to link to file?

转载 作者:行者123 更新时间:2023-12-04 05:38:05 25 4
gpt4 key购买 nike

我有一个 Catalog它有一个名为 file 的属性:

class Catalog < ActiveRecord::Base
mount_uploader :file, CatalogUploader

我将如何创建一个链接来在浏览器中显示该文件?

我试过
<%= link_to "zzzz", catalog.file_url %>

但是当我单击该链接时,它只会给我一个路由错误
Routing Error

No route matches [GET] "/Users/emai/Documents/mysite/catalogs/2/vegetarian.png"

而且 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

最佳答案

根据 the documentation ,你应该调用方法 url像这样的文件对象:

<%= link_to "zzzz", catalog.file.url %>

关于ruby-on-rails - rails + 载波 : How to link to file?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23478918/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com