Ruby on Rails Ckeditor在部署时丢失了paperclip图像资产

时间:2015-09-21 12:00:28

标签: ruby-on-rails ruby-on-rails-4 ckeditor paperclip assets

我有一个Ruby on rails网站,我使用Ckeditor gem。 https://github.com/galetahub/ckeditor

我用回形针设置它,所以我也可以在编辑器中上传图像。 这没有任何问题。

问题是当我使用CKeditor部署我的cloud66服务器时。 它成功,所以通过ckeditor删除所有上传的图像。 (链接仍然相同,只有图像消失了)

如何解决这个问题?

代码: 型号> ckeditor> assets.rb

module Ckeditor
  class Asset < ActiveRecord::Base
    include Ckeditor::Orm::ActiveRecord::AssetBase
    include Ckeditor::Backend::Paperclip
  end
end

模型&gt; ckeditor&gt; attachment_file.rb

module Ckeditor
  class AttachmentFile < Ckeditor::Asset
    has_attached_file :data,
                      url: "/ckeditor_assets/attachments/:id/:filename",
                      path: ":rails_root/public/ckeditor_assets/attachments/:id/:filename"

    validates_attachment_presence :data
    validates_attachment_size :data, less_than: 100.megabytes
    do_not_validate_attachment_file_type :data

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

模型&gt; ckeditor&gt; picture.rb

module Ckeditor
  class Picture < Ckeditor::Asset
    has_attached_file :data,
                      url: "/ckeditor_assets/pictures/:id/:style_:basename.:extension",
                      path: ":rails_root/public/ckeditor_assets/pictures/:id/:style_:basename.:extension",
                      styles: { content: "800>", thumb: "118x100#" }

    validates_attachment_presence :data
    validates_attachment_size :data, less_than: 2.megabytes
    validates_attachment_content_type :data, content_type: /\Aimage/

    def url_content
      url(:content)
    end
  end
end

config&gt;初始者&gt; assets.rb

Rails.application.config.assets.version = "1.0"

# Add additional assets to the asset load path
# Rails.application.config.assets.paths << Emoji.images_path

# Precompile additional assets.
# application.js, application.css, and all non-JS/CSS in app/assets folder are already added.
# Rails.application.config.assets.precompile += %w( search.js )
Rails.application.config.assets.precompile += %w( club.css admin.css )
Rails.application.config.assets.precompile += %w( club.js admin.js )

Rails.application.config.assets.precompile += Ckeditor.assets
Rails.application.config.assets.precompile += %w(ckeditor/*)

config&gt;初始者&gt; ckeditor.rb

Ckeditor.setup do |config|

  require "ckeditor/orm/active_record"

end

config&gt; deploy.rb

# if you want to clean up old releases on each deploy uncomment this:
set :shared_children, shared_children + %w{public/ckeditor_assets}
after "deploy:restart", "deploy:cleanup"

namespace :deploy do
  task :start, :roles => :app do
    run "touch #{current_path}/tmp/restart.txt"
  end

  task :stop, :roles => :app do
    # Do nothing.
  end

  desc "Restart Application"
  task :restart, :roles => :app do
    run "touch #{current_path}/tmp/restart.txt"
  end

  after 'deploy:update_code' do
    #run "cd #{release_path}; RAILS_ENV=production rake db:create"

    run "cd #{release_path}; RAILS_ENV=production rake db:migrate"
    run "cd #{release_path}; RAILS_ENV=production rake db:seed"
    run "cd #{release_path}; RAILS_ENV=production rake assets:precompile"

    run "ln -s #{shared_path}/public/ckeditor_assets #{release_path}/public/ckeditor_assets"

  end

  desc "Update the crontab file"
  task :update_crontab, :roles => :db do
    run "cd #{release_path} && whenever --update-crontab #{application}"
    run "cd #{release_path}; tail -f log/cron_log.log"
  end

我在routes.rb mount Ckeditor :: Engine =&gt; “/ CKEditor的”

1 个答案:

答案 0 :(得分:1)

解决方案:

删除

url: "/ckeditor_assets/pictures/:id/:style_:basename.:extension",
path: ":rails_root/public/ckeditor_assets/pictures/:id/:style_:basename.:extension",

在图片模型中,这会将上传文件更改为公用文件夹内的系统文件夹,并防止其编译错误