已完成500内部服务器错误... NameError(未初始化的常量PictureUploader :: Uploader

时间:2016-12-31 08:21:56

标签: ruby-on-rails ruby-on-rails-4 locale carrierwave puma

我很想在这里提出这个问题,希望我能正确遵守这些规则:

我在开发环境中遇到了500个服务器错误,它声明NameError带有未初始化的常量用于图片上传器,但大多数时候,如果我重新启动浏览器(Chrome或Safari),这个问题就会消失。

我正在使用: Rails 4.2.7.1, ruby 2.3.1p112, 'carrierwave','〜> 1.0, 'mini_magick', '4.3.6', 美洲狮上的导弹

这个问题可能与I18n语言环境有关吗?因为我安装了I18n gem之后就开始出现这个错误了,如果我更改了locales / *。yml内容,就会发生这种错误。

谢谢你

错误讯息:

Started GET "/" for 127.0.0.1 at 2017-01-02 01:29:47 +0800
Processing by StaticPagesController#home as HTML
Completed 500 Internal Server Error in 105ms (ActiveRecord: 1.1ms)

NameError (uninitialized constant PictureUploader::Uploader70149836016940):
  activesupport (4.2.7.1) lib/active_support/inflector/methods.rb:263:in `const_get'
  activesupport (4.2.7.1) lib/active_support/inflector/methods.rb:263:in `block in constantize'
....

PictureUploader文件位于app文件夹中,作为'uploaders'文件夹,与资产/控制器/助手相同......

应用程序/上传/ picure_uploader:

# app/uploaders/picture_uploader.rb
class PictureUploader < CarrierWave::Uploader::Base

  include CarrierWave::MiniMagick

  # I am still using local storage as development now
  if Rails.env.production?
  #  storage :fog
    storage :file # not yet switch to cloud/fog
  else
    storage :file
  end

  # 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

  # set the largest photo size
  process resize_to_limit: [800, 800]

  # 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)
  #   resize_to_limit: [width, hight]
  # end

  # Create different versions of your uploaded files:
  version :thumb do
    #  process :resize_to_fit => [50, 50] # this one create different photo size
    process resize_and_pad: [50,50,:transparent,'center'] # have to be same size and transparent background, work good!
  end

  version :blog do
    # process :resize_to_pad => [400, 400, "#000000", 'Center'] # error in not finding resize_to_pad
    # process resize_and_pad: [400,400,"#CCCCCC",'center'] # this is ok
    # process resize_and_pad: [400,400,:transparent,'center'] # try to be transparent background, work good!
    process resize_to_limit: [400,400]
  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

_ form formforfor个人资料图片上传

<%= form_for([@pc_roles], html: { multipart: true }) do |f| %>

... too long ... cut it for simple reading

  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
  <span class="picture">
    <%= f.file_field :picture, accept: 'image/jpeg,image/gif,image/png' %>
    <p> photo size upto 800 in wide and tall, oversize will resized. </p>
    <%= image_tag @pc_roles.picture.url if @pc_roles.picture? %>
  </span>
<% end %>

<script type="text/javascript">
  $('#pc_role_picture').bind('change', function() {
    var size_in_megabytes = this.files[0].size/1024/1024;
    if (size_in_megabytes > 5) {
      alert('Maximum file size is 5MB. Please choose a smaller file.');
    }
  });
</script>

模型

class PcRole < ActiveRecord::Base
  belongs_to :pc_user
  mount_uploader :picture, PictureUploader
  validate  :picture_size

... cut it ... too long ... for simple reading ...

  def picture_size
    if picture.size > 5.megabytes
      errors.add(:picture, "should be less than 5MB")
    end
  end
end
顺便说一句,我还有另一个上传者调用image_uploader.rb,在picture_uploader的同一文件夹中,在app / uploaders文件夹下,这两个几乎完全相同:

# app/uploaders/image_uploader.rb
class ImageUploader < CarrierWave::Uploader::Base

  # Include RMagick or MiniMagick support:
  # include CarrierWave::RMagick
  include CarrierWave::MiniMagick

# Choose what kind of storage to use for this uploader:
  if Rails.env.production?
  #  storage :fog
    storage :file # not yet switch to cloud/fog
  else
    storage :file
  end

  # 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

  # try to fix the mobile photo rotate 90 degree issue
  def fix_exif_rotation #this is my attempted solution
    manipulate! do |img|
      img.tap(&:auto_orient)
    end
  end

  process :fix_exif_rotation


  # set the largest photo size
  process resize_to_limit: [800, 800]

  # Process files as they are uploaded:
  # process :scale => [200, 300]
  #
  # def scale(width, height)
  #   resize_to_limit: [width, hight]
  # end

  def extension_white_list
    %w(jpg jpeg gif png)
  end

end

我只为TinyMCE使用image_uploader

  <div class="field">
    <%= f.label :content %><br>
    <%= f.text_area :content, :class => "tinymce", :rows => 30, :cols => 120 %>
    <%= tinymce :uploadimage_hint => @pc_blog.id %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>

由于它们是不同的上传者,我假设它是Picture Uploader问题。

0 个答案:

没有答案