【问题标题】:Ror 4 with carrierwave and rmagick带有载波和 rmagick 的 Ror 4
【发布时间】:2013-12-20 19:05:30
【问题描述】:

我的 rmagick 有问题。

更新程序>assets_uploader

   include CarrierWave::RMagick
  # include CarrierWave::MiniMagick

  # Include the Sprockets helpers for Rails 3.1+ asset pipeline compatibility:
  # include Sprockets::Helpers::RailsHelper
  # include Sprockets::Helpers::IsolatedHelper

  # 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
    "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
  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:
  #   # 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

控制器>预测

def new
        @prediction = Prediction.new
        2.times { @prediction.assets.build }
    end

def create
        @prediction = Prediction.new(prediction_params)
        if @prediction.save
        flash[:notice] = "Prediction has been created."
        redirect_to @prediction
        else
        flash[:alert] = "Prediction has not been created."
        render 'new'
        end
    end

private
    def prediction_params
        params.require(:prediction).permit(:match,:pick, :odds,:bookmaker,:result,assets_attributes: [:asset])
    end

模型>预测

class Prediction < ActiveRecord::Base
    validates :match, presence: true


    has_many :assets
    accepts_nested_attributes_for :assets
end

模型>资产

class Asset < ActiveRecord::Base
     belongs_to :prediction

        mount_uploader :asset, AssetUploader

end

数据库

我有一张桌子

class CreateAssets < ActiveRecord::Migration
  def change
    create_table :assets do |t|
        t.string :asset
        t.references :prediction

      t.timestamps
    end
  end
end

class AddAssetToPredictions < ActiveRecord::Migration
  def change
    add_column :predictions, :asset, :string
  end
end

预测表:

class CreatePredictions < ActiveRecord::Migration
  def change
    create_table :predictions do |t|
      t.string :match
      t.string :pick
      t.string :bookmaker
      t.string :odds
      t.string :result

      t.timestamps
    end
  end
end

我上传了 2 张照片。在节目或索引中只有一个图像是 50,50 另一个没有改变。有什么建议可以解决这个问题吗?

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-4 carrierwave image-uploading rmagick


    【解决方案1】:

    您不需要执行 assets.build 来上传多张照片。

    为您的应用程序设置资产库的最佳方法是首先为您的资产指定一个定制模型,即附件。然后,您可以利用多态 ActiveRecord 关联 (http://railscasts.com/episodes/154-polymorphic-association) 在多个模型之间共享 Attachment 模型。我还建议您使用Carrierwave 作为文件管理的选择。

    在这里,在这个例子中,我设置了一个名为“attachable”的多态关联:

    附件.rb

    def Attachment << ActiveRecord::Base
       attr_accessible :attachable_id, :attachable_type, :description, :file
       belongs_to :attachable, polymorphic: true
       mount_uploader :file, AssetUploader
    end
    

    我还为附件模型分配了两个新属性,它们将包含关联的模型 ID(在您的情况下为预测 ID)和附件类型。另外,请注意我已将 Carrierwave 中的 mount_uploader 分配给附件表(有关此内容的更多信息,请参阅 carrierwave 文档)。

    prediction.rb

    def Prediction << ActiveRecord::Base
       attr_accessible :title, :description, :attachments_attributes
       has_many :attachments, as: :attachable, :dependent => :destroy
       accepts_nested_attributes :attachments
    end
    

    现在,在我的预测模型中,我创建了一个称为附件的 has_many 关系,它引用附件表中的可附加多态关联。此外,关联的附件依赖于预测,如果预测被删除,关联的附件将被删除。

    我还将附件模型设置为可在预测模型中进行编辑。因此,您可以在预测表单中分配嵌套字段。

    我们的最后一步是配置 Carrierwave 上传器文件。我正在使用MiniMagickGem 将图像压缩并调整为不同的版本,同时利用'from_version' 方法来提高处理速度。见下文:

    app/uploaders/asset_uploader.rb

    class AssetUploader < CarrierWave::Uploader::Base
      include CarrierWave::Compatibility::Paperclip
      include CarrierWave::MiniMagick
      include Sprockets::Helpers::RailsHelper
      include Sprockets::Helpers::IsolatedHelper
    
      process resize_to_fit: [500,500]
    
      version :large do
        process resize_to_fill: [400,400]
      end
    
      version :medium, :from_version => :large do 
        process resize_to_fill: [150,150]
      end
    
      version :small, :from_version => :medium do
        process resize_to_fill: [50,50]
      end
    
    end
    

    这应该足以让您入门。

    评论更新

    我之前的评论是展示处理资产上传的最佳实践,因为它允许以后进行可扩展性。

    至于调整图像大小,我一直使用MiniMagick,因为它的设置总是快速且容易,几乎没有问题。过去我对 RMagick 的运气并不好。您是否有特定原因要使用 RMagick?

    评论 #2 更新

    您可以在asset_uploader.rb 中设置允许的文件类型列表:

    def extension_white_list
        %w(jpg jpeg gif png)
    end
    

    【讨论】:

    • 在 rails 4 你不能做 attr_accessible。
    • 这是我在 Rails 3.2 应用程序中编写的示例。您可以轻松修改 rails 3.2/4 之间的细微差别,但这个概念仍然很强大。
    • 我编辑我的问题。你的意思是我认为是我的资产表,因为我引用了:prediction,所以他得到了他的 ID。看看,请让我知道。我是对的,请编辑您的答案,以便我接受它
    • p.s:问题不在于上传,而在于尺寸@JellyFishBoy
    • 添加了对我的回答的回复。
    猜你喜欢
    • 2015-10-18
    • 2015-02-22
    • 2014-01-10
    • 2013-07-19
    • 1970-01-01
    • 2011-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多