【问题标题】:CKEDITOR + CARRIERWAVE + S3 no imageCKEDITOR + CARRIERWAVE + S3 无图像
【发布时间】:2017-05-03 04:03:45
【问题描述】:

我不明白为什么 CKEDITOR 不显示我的图像...我使用 Carrierwave 作为 S3 的上传器,如果我直接上传,显然已设置为都有自己的“uploader.rb”通过 Carrierwave,我可以毫无问题地从 S3 检索所述图像以显示。但是,如果我通过 CKEDITOR 上传所述图像或将其附加到 CKEDITOR 中,它只会显示它的源链接。这发生在生产和开发本地主机中。

上传者/ckeditor_attachement_file_uploader.rb

  # encoding: utf-8
require 'carrierwave'

class CkeditorAttachmentFileUploader < CarrierWave::Uploader::Base
  include Ckeditor::Backend::CarrierWave

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

  # Choose what kind of storage to use for this uploader:
  if Rails.env.production?
    storage :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/ckeditor/attachments/#{model.id}"
  end

  # Provide a default URL as a default if there hasn't been a file uploaded:
  # def default_url
  #   "/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

  # Add a white list of extensions which are allowed to be uploaded.
  # For images you might use something like this:
  def extension_white_list
    Ckeditor.attachment_file_types
  end
end

uploaders/ckeditor_picture_uploader.rb

# encoding: utf-8
class CkeditorPictureUploader < CarrierWave::Uploader::Base
  include Ckeditor::Backend::CarrierWave

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

  # Choose what kind of storage to use for this uploader:
  if Rails.env.production?
    storage :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/ckeditor/pictures/#{model.id}"
  end

  # Provide a default URL as a default if there hasn't been a file uploaded:
  # def default_url
  #   "/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

  process :extract_dimensions

  # Create different versions of your uploaded files:
  version :thumb do
    process resize_to_fill: [118, 100]
  end

  version :content do
    process resize_to_limit: [800, 800]
  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
    Ckeditor.image_file_types
  end
end

models/picture.rb

class Ckeditor::Picture < Ckeditor::Asset
  mount_uploader :data, CkeditorPictureUploader, mount_on: :data_file_name

  def url_content
    url(:content)
  end
end

模型/附件文件.r​​b

class Ckeditor::AttachmentFile < Ckeditor::Asset
  mount_uploader :data, CkeditorAttachmentFileUploader, mount_on: :data_file_name

  def url_thumb
    @url_thumb ||= Ckeditor::Utils.filethumb(filename)
  end
end

【问题讨论】:

  • 我已经解决了这个难题,我认为 html_safe 是必需的。例如

    当我将它投入生产时才发现它并显示它也在显示 html 标记

标签: ruby-on-rails ckeditor


【解决方案1】:

我知道我正在回答我自己的问题,但希望这会对您有所帮助。如果您正在阅读本文,那么您显然已经正确设置了所有存储,例如 S3(在我的情况下)。尝试通过 ckeditor ex 显示内容时,您的视图必须需要 html_safe 方法。

    <div class="col-xs-12">
<h1>Webur Blog</h1>

    <h2><%= @blogpost.title %></h2>
    <span><p><%= image_tag @blogpost.picture.url if @blogpost.picture? %></p></span>
    <p><%= @blogpost.content.html_safe %></p>
    <% if is_an_admin %>
    <%= link_to "Return to blog", blogposts_path, class: 'btn btn-primary' %>
    <%= link_to "Edit Post", edit_blogpost_path, class: 'btn btn-primary' %> | 
    <%= link_to "Delete Post", blogpost_path(@blogpost), 
                            method: :delete, data: {confirm: "Are you sure?"}, class: 'btn btn-danger' %>

    <% else %>
    <%= link_to "Return to blog", blogposts_path, class: 'btn btn primary' %>
    <% end %>
</div>

希望这对其他人有所帮助。 CKeditor 在他们的设置指南中没有提到这一点,如果你是像我这样的初学者,你可能无法捕捉到这样的微妙之处

【讨论】:

    猜你喜欢
    • 2012-11-25
    • 2012-01-22
    • 2013-06-17
    • 1970-01-01
    • 2018-08-24
    • 1970-01-01
    • 2019-01-06
    • 2015-09-26
    • 2012-06-19
    相关资源
    最近更新 更多