Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

undefined method 'content_type' for #Hash #16

Open
andisthejackass opened this issue Mar 21, 2015 · 1 comment
Open

undefined method 'content_type' for #Hash #16

andisthejackass opened this issue Mar 21, 2015 · 1 comment

Comments

@andisthejackass
Copy link

I am trying to use publify with this gem. I could upload resources locally, but when I switched to carrierwave-dropbox I get the error Undefined method 'content_type' for #<Hash:0x0000000683a990> while the images are normally uploaded to my dropbox folder. Any idea why this is happening?
Here's the app trace

NoMethodError in Admin::ResourcesController#upload
undefined method `content_type' for #<Hash:0x0000000683a990>

app/uploaders/resource_uploader.rb:42:in `image?'
app/controllers/admin/resources_controller.rb:15:in `upload'

resource_uploader.rb

class ResourceUploader < CarrierWave::Uploader::Base
  storage :dropbox
  # To handle Base64 uploads...
  class FilelessIO < StringIO
    attr_accessor :original_filename

    def initialize(data, filename)
      super(data)
      @original_filename = filename
    end
  end

  include CarrierWave::MiniMagick
  include CarrierWave::MimeTypes

  process :set_content_type

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

  version :thumb, :if => :image? do
    process :dynamic_resize_to_fit => :thumb
  end

  version :medium, :if => :image? do
    process :dynamic_resize_to_fit => :medium
  end

  version :avatar, :if => :image? do
    process :dynamic_resize_to_fit => :avatar
  end

  def dynamic_resize_to_fit(size)
    blog = Blog.default
    resize_setting = blog.send("image_#{size}_size").to_i

    resize_to_fit(resize_setting, resize_setting)
  end

  def image?(new_file)
    new_file.content_type && new_file.content_type.include?('image')
  end
end

resources_controller.rb

require 'fog'

class Admin::ResourcesController < Admin::BaseController
  cache_sweeper :blog_sweeper

  def upload
    if ! params[:upload].blank?
      file = params[:upload][:filename]

      unless file.content_type
        mime = 'text/plain'
      else
        mime = file.content_type.chomp
      end
      @up = Resource.create(:upload => file, :mime => mime, :created_at => Time.now)
      flash[:success] = I18n.t('admin.resources.upload.success')
    else
      flash[:warning] = I18n.t('admin.resources.upload.warning')
    end

    redirect_to :action => "index"
  end

  def index
    @r = Resource.new
    @resources = Resource.order('created_at DESC').page(params[:page]).per(this_blog.admin_display_elements)
  end

  def get_thumbnails
    position = params[:position].to_i
    @resources = Resource.without_images.by_created_at.limit(10).offset(position)

    render 'get_thumbnails', :layout => false
  end

  def destroy
    begin
      @record = Resource.find(params[:id])
      mime = @record.mime
      return(render 'admin/shared/destroy') unless request.post?

      @record.destroy
      flash[:notice] = I18n.t('admin.resources.destroy.notice')
      redirect_to :action => 'index'
    rescue
      raise
    end
  end
end
@n-studio
Copy link

store! and retrieve! should return a CarrierWave::SanitizedFile instance instead of a hash.

Working on a fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants