Rails 5回形针图像未显示

时间:2017-07-21 17:32:49

标签: image paperclip ruby-on-rails-5

图片未显示,只显示小图片图标。当我检查图像时,我得到以下路径:<img src="/system/protests/images/000/000/001/thumb/no_cuny_cuts.jpg?1500589981" alt="No cuny cuts">我认为我必须设置路径以查看公共文件夹,因为图像的URL从system开始。我试图使用所有问题,但没有运气。这是文件。

模型 protest.rb

class Protest < ApplicationRecord
  validates :name, :description, :location,
            :starts_at, :creator, presence: true

  has_attached_file :image, styles: { medium: "400x600#" }, default_url: "/images/default_:style_avatar.png"
  validates_attachment_content_type :image, content_type: /\Aimage\/.*\Z/

  belongs_to :creator, class_name: :User

  has_many :attendances
  has_many :users, through: :attendances

  has_many :transportations
end

配置/ application.rb中

module ProtestTrump
  class Application < Rails::Application
    # Initialize configuration defaults for originally generated Rails version.
    config.load_defaults 5.1

    # Settings in config/environments/* take precedence over those specified here.
    # Application configuration should go into files in config/initializers
    # -- all .rb files in that directory are automatically loaded.

    # Don't generate system test files.
    config.generators.system_tests = nil

    #paperclip
    config.paperclip_defaults = { storage: :fog, fog_credentials: { provider: "Local", local_root: "#{Rails.root}/public"}, fog_directory: "", fog_host: "localhost"}
  end
end

迁移

class AddImageColumnsToProtests < ActiveRecord::Migration[5.1]
  def up
   add_attachment :protests, :image
  end

  def down
   remove_attachment :protests, :image 
  end
end

1 个答案:

答案 0 :(得分:0)

有几个原因导致这种情况无法奏效。正如您在model / protest.rb中看到的那样,以下行

has_attached_file :image, styles: { medium: "400x600#" }, default_url: "/images/default_:style_avatar.png"

在这里你需要添加拇指:如果你想使用它。所以你必须改变这一行来添加拇指样式。

has_attached_file :image, styles: {thumb: "100x100#", medium: "400x600#" }, default_url: "/images/default_:style_avatar.png"。 添加thumb:

到您必须重置模型的样式。 我运行了以下命令

bundle exec rake paperclip:refresh:thumbnails CLASS=Protest

可以在以下链接中找到重置说明。

https://github.com/thoughtbot/paperclip/wiki/Thumbnail-Generation