Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 根据多态关联,使用carrierwave条件重新调整大小_Ruby On Rails_Carrierwave_Polymorphic Associations - Fatal编程技术网

Ruby on rails 根据多态关联,使用carrierwave条件重新调整大小

Ruby on rails 根据多态关联,使用carrierwave条件重新调整大小,ruby-on-rails,carrierwave,polymorphic-associations,Ruby On Rails,Carrierwave,Polymorphic Associations,我有以下三种型号: class Product < ActiveRecord::Base has_many :images, :as => :imageable end class Category < ActiveRecord::Base has_many :images, :as => :imageable end class Image < ActiveRecord::Base mount_uploader :image, ImageUploa

我有以下三种型号:

class Product < ActiveRecord::Base
  has_many :images, :as => :imageable
end

class Category < ActiveRecord::Base
  has_many :images, :as => :imageable
end

class Image < ActiveRecord::Base
  mount_uploader :image, ImageUploader
  belongs_to :imageable, :polymorphic => true
end
类产品:可成像
结束
类类别:可成像
结束
类映像true
结束
我的图像控制器如下所示:

class Admin::ImagesController < AdminController
  before_filter :find_imageable

  def new
    @image = @imageable.images.new
  end

  def create
    if @image = @imageable.images.create(params[:image])
      redirect_to admin_images_path(:imageable_type => @imageable.class, :imageable_id => @imageable.id)
    else
      render 'new'
    end
  end

  private
  def find_imageable
    klass = params[:imageable_type].capitalize.constantize
    @imageable = klass.find(params[:imageable_id])
  end
end
class Admin::ImagesController@imageable.class,:imageable\u id=>@imageable.id)
其他的
呈现“新”
结束
结束
私有的
def find_可成像
klass=params[:imageable_type].capitalize.constantize
@imageable=klass.find(参数[:imageable\u id])
结束
结束
一切正常,但现在我想根据关联类型调整图像大小:

class ImageUploader < CarrierWave::Uploader::Base
  include CarrierWave::RMagick

  storage :file

  version :thumb, :if => :is_product? do
    process :resize_to_fill => [100, 100]
  end

  def store_dir
    "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
  end

  protected
  def is_product? image
    model.imageable.class == 'Product' # => imageable is nil!
  end
end
class ImageUploader:是否为产品?做
处理:将_调整为_填充=>[100100]
结束
def存储目录
“上载/#{model.class.to_s.下划线}/#{mounted_as}/#{model.id}”
结束
受保护的
def是一种新产品?形象
model.imageable.class=='Product'#=>imageable为零!
结束
结束
问题是,“model”与上传程序中的可成像对象没有关联。当我检查模型时,它看起来是这样的:

#<Image id: nil, name: "test", image: nil, imageable_id: nil, imageable_type: nil, created_at: nil, updated_at: nil>
#

有什么想法吗

我遇到了同样的问题。我确实找到了一个解决方法,但我仍然想知道是否有更好的方法来处理这个问题。

我通常会这样做

  version :thumb, :if => :thumb_version_enabled? do
    process :resize_to_fit => [100, 100]
  end

  version :big, :if => :big_version_enabled? do
    process :resize_to_fill => [600,600]
  end

  def include_version?(v)
    if self.model.respond_to?("only_versions")
      self.model.only_versions.collect(&:to_s).include? v.to_s
    else
      true
    end
  end

  protected 

  def method_missing(method_name, *args, &block)
    return super unless /^(.*)_version_enabled(=?)/ =~ method_name
    return include_version?(method_name.to_s.gsub("_version_enabled?","").gsub("_version_enabled",""))
  end
在目标模型中(装载了uploader),我将定义一个方法来定义启用的版本

class Product < ActiveRecord::Base

  ##....omiss
  mount_uploader :image, ImageUploader

  def self.only_versions
    [:thumb, :big, :small, :original]
  end

  def only_versions
    self.class.only_versions
  end
end
类产品
在多态关联的情况下,您可以将方法委托给关联的模型

class Image < ActiveRecord::Base
  mount_uploader :data, ImageUploader

  belongs_to :imageable, :polymorphic => true

  def only_versions
    if self.imageable && self.imageable.respond_to?("only_versions")
      self.imageable.only_versions
    else
      ImageUploader.versions.keys
    end
  end
类映像true
def-only_版本
如果self.imageable&&self.imageable.response_to?(“仅_版本”)
self.imageable.only_版本
其他的
ImageUploader.versions.keys
结束
结束
如果缺少“only_versions”方法,则默认情况下将启用中的所有版本