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

标签 ruby-on-rails

我有一个 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/

相关文章:

mysql - 从多属性搜索的条件中删除搜索字符串/查询属性。从 where 条件中删除空白参数的空白属性

ruby-on-rails - 为什么在 Heroku 上,如果我点击 "About your application’ s environment”,它会说找不到页面?

ruby-on-rails - 如何使用机架重写将 www 添加到 url?

ruby-on-rails - 更新 TopicsController 以允许版主更新主题,但不能创建或删除

html - Link_to 在 html 中复制链接

ruby-on-rails - SimpleCov rspec 和 Cucumber 分开

css - 在 Rails 的 Assets 管道中将图像编码为 css

ruby-on-rails - 通过关联更好地查找

ruby-on-rails - 通过 ArraySerializer 新语法传入选项哈希

HTML/CSS -( float : left) does not work