Rob
Rob

Reputation: 35

Ruby on Rails Ckeditor loses paperclip image assets on deploy

I got a Ruby on rails website where i use the Ckeditor gem. https://github.com/galetahub/ckeditor

I have it setup with paperclip so i can also upload images in the editor. This works without any problems.

The problem is when i deploy my cloud66 server with CKeditor. It makes it, so that all the uploaded images trough the ckeditor are deleted. (the links are still the same, only the images are gone)

How to solve this?

Code: Model > ckeditor > assets.rb

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

Model > ckeditor > 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

Model > ckeditor > 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 > initialers > 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 > initialers > ckeditor.rb

Ckeditor.setup do |config|

  require "ckeditor/orm/active_record"

end

config > 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

and i got in routes.rb mount Ckeditor::Engine => "/ckeditor"

Upvotes: 2

Views: 909

Answers (1)

Rob
Rob

Reputation: 35

Solution:

remove the

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

at the picture model, this will change the upload file to the system folder inside the public folder, and prevent it from compiling wrong

Upvotes: 1

Related Questions