到底如何将 ckeditor 与 Paperclip 集成,以便它可以上传图像文件?

发布于 2024-09-18 12:24:44 字数 181 浏览 9 评论 0 原文

如何让 http://github.com/galetahub/rails-ckeditor 正常工作,以便您可以上传图像文件?我不认为我会使用 s3 存储...

任何帮助将不胜感激。

How do you get http://github.com/galetahub/rails-ckeditor working so you can upload image files? I don't think I'll use the s3 storage...

any help would be appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

表情可笑 2024-09-25 12:24:44

是的,你可以。我假设您已经为 S3 设置了回形针。因此,您只需编辑模型目录(app/model/ckeditor/)中的 picture.rb 和 Attachement_file.rb 并将这些行替换

  has_attached_file :data,
                    :url => "/ckeditor_assets/attachments/:id/:filename",
                    :path => ":rails_root/public/ckeditor_assets/attachments/:id/:filename"

为您的 papeclip 版本 has_attached_file:

  has_attached_file :data, :styles => { :content => '575>', :thumb => '80x80#' },
    :storage => :s3, :s3_credentials => "#{Rails.root}/config/s3.yml", :path => ":attachment/:id/:style.:extension",
    :url => ":s3_domain_url"

就是这样。顺便说一句:这是 Rails 3 的示例。

Yes you can. I assume that you have paperclip already set up for S3. So you have only edit the picture.rb and attachement_file.rb in you model directory (app/model/ckeditor/) and replace these lines

  has_attached_file :data,
                    :url => "/ckeditor_assets/attachments/:id/:filename",
                    :path => ":rails_root/public/ckeditor_assets/attachments/:id/:filename"

with your papeclip version has_attached_file:

  has_attached_file :data, :styles => { :content => '575>', :thumb => '80x80#' },
    :storage => :s3, :s3_credentials => "#{Rails.root}/config/s3.yml", :path => ":attachment/:id/:style.:extension",
    :url => ":s3_domain_url"

That's it. Btw: this is example from Rails 3.

如果没有 2024-09-25 12:24:44

我会遵循您提到的rails-ckeditor插件的README。如果您不需要 SWFUpload,则只需 通过编写自定义文件上传器和自定义文件浏览器来集成 CKEditor 和 Paperclip,并通过指定 url 和回调函数将它们连接到编辑器。

有一个例子总是有用的,作者制作了一个示例应用。不幸的是,其中有一些错误。请考虑以下几点以使其运行

  1. 更改 config/environment.rb 中的以下行

    config.gem '回形针', :version =>; '2.3.3'

    config.gem 'ckeditor', :version =>; '3.4.1'

  2. 删除公共文件index.html

  3. add config/routes.rb 的根路由

    map.root:控制器=> "pages"

I would follow the README for the rails-ckeditor plugin you mentioned. If you don't need the SWFUpload, you can simply integrate the CKEditor and Paperclip, by writing a custom file uploader and custom file browser, and connect them to the editor by specifying urls and callback functions.

It is always useful to have an example, the author has made an example app. Unfortunately, there are a few errors in it. Consider the following points to make it running

  1. change the following lines in config/environment.rb

    config.gem 'paperclip', :version => '2.3.3'

    config.gem 'ckeditor', :version => '3.4.1'

  2. delete the file index.html in public

  3. add a root route to config/routes.rb

    map.root :controller => "pages"

巴黎夜雨 2024-09-25 12:24:44

Rails 4.2.0解决方案:

如何获取http://github.com/galetahub/rails-ckeditor可以上传图像文件吗?

事实上,CKEditor 允许您嵌入现有的图像 URL,但为了让 CKEditor 和 Paperclip 一起工作以便您可以上传图像,您将需要 ImageMagick。据我了解,它处理上传图像数据、为上传的图像数据创建图像 URL 引用以及嵌入上传的图像数据的 URL。


CKEditor

gem "ckeditor" 添加到您的 Gemfile

,然后运行 ​​$ bundle install 命令。


将其添加到 /app/assets/javascripts/application.js

//= require jquery
//= require jquery_ujs
//= require turbolinks
//= require ckeditor/init  <--------------- THIS
//= require_tree . <----------------------- ABOVE THIS

中: https://github.com/galetahub/ckeditor#how-to-generate-models-to-store-uploaded-files

将其添加到:

/config/routes.rb
我把它放在使用它的资源之前

mount Ckeditor::Engine => '/ckeditor'  

使用“form_for" 并设置了一个带有 title:string 和 text:text 的“Article”模型
/app/views/articles/_form.html.erb

  <p>
    <%= f.label :text %><br>
    <%= f.cktext_area :text, rows: 10  %> # <-------- "cktext_area"
  </p>

使用“simple_form_for

  <div class="form-group">
    <%= f.input :body, :as => :ckeditor, input_html: {:ckeditor => {:toolbar => 'FULL'}}, class: "form-control" %>
  </div>

回形针

:< a href="https://richonrails.com/articles/getting-started-with-ckeditor" rel="nofollow noreferrer">https://richonrails.com/articles/getting-started-with-ckeditor

gem“paperclip”添加到您的Gemfile中并$bundle install

然后运行以下两个命令:

$railsgenerateckeditor:install--orm=active_record--backend=paperclip

$rakedb:migrate


ImageMagick

对于macOS Sierra:

$ brew install imagemagick

对于其他ImageMagick 安装选项:https://www.imagemagick.org/script/install-source。 php

Rails 4.2.0 solution:

How do you get http://github.com/galetahub/rails-ckeditor working so you can upload image files?

As is, CKEditor allows you to embed existing image URLs, but for CKEditor and Paperclip to work together so you can upload images, you will need ImageMagick. As I understand, it handles uploading the image data, making an image URL reference for the uploaded image data and the embedding of the uploaded image data's URL.


CKEditor

Add gem "ckeditor" to your Gemfile

then run the $ bundle install command.


Add this to /app/assets/javascripts/application.js

//= require jquery
//= require jquery_ujs
//= require turbolinks
//= require ckeditor/init  <--------------- THIS
//= require_tree . <----------------------- ABOVE THIS

per: https://github.com/galetahub/ckeditor#how-to-generate-models-to-store-uploaded-files

Add this to:

/config/routes.rb
I put it before the resources which utilize it

mount Ckeditor::Engine => '/ckeditor'  

Using "form_for" and having set up an "Article" model with a title:string and text:text
/app/views/articles/_form.html.erb

  <p>
    <%= f.label :text %><br>
    <%= f.cktext_area :text, rows: 10  %> # <-------- "cktext_area"
  </p>

Using "simple_form_for"

  <div class="form-group">
    <%= f.input :body, :as => :ckeditor, input_html: {:ckeditor => {:toolbar => 'FULL'}}, class: "form-control" %>
  </div>

Paperclip

per: https://richonrails.com/articles/getting-started-with-ckeditor

Add gem "paperclip" to your Gemfile and $ bundle install.

Then run the following two commands:

$ rails generate ckeditor:install --orm=active_record --backend=paperclip

and

$ rake db:migrate


ImageMagick

For macOS Sierra:

$ brew install imagemagick

For other ImageMagick install options: https://www.imagemagick.org/script/install-source.php

别靠近我心 2024-09-25 12:24:44

使用以下对我有用的东西,但你应该在 Amazon 上拥有 s3 存储帐户和正确的端点,你可以参考下面的

code.`gem 'aws-sdk', '~> 2'
gem 'aws-s3'
gem 'aws-sdk-v1'
gem 'paperclip'

class Ckeditor::Picture < Ckeditor::Asset

  AWS_CONFIG = YAML.load(ERB.new(File.read("#{Rails.root}/config/aws.yml")).result)[Rails.env]

  has_attached_file :data,
                    s3_credentials: {
                        access_key_id: AWS_CONFIG['access_key_id'],
                        secret_access_key: AWS_CONFIG['secret_access_key'],
                        bucket: AWS_CONFIG['bucket'],
                    },
                    s3_host_name: 's3.amazonaws.com',
                    :s3_endpoint => 's3.amazonaws.com',
                    storage: :s3,
                    s3_headers:     { "Cache-Control" => "max-age=31557600" },
                    s3_protocol:    "https",
                    bucket:         AWS_CONFIG['bucket'],
                    url: ':s3_domain_url',
                    path: '/:class/:attachment/:id_partition/:style/:filename',
                    default_url:   "/:class/:attachment/:id/:style/:basename.:extension",
                    default_style: "medium"

  validates_attachment_size :data, :less_than => 2.megabytes
  validates_attachment_presence :data

  def url_content
    url(:content)
  end
end

`

注释此行 require "ckeditor/orm/active_record" from /config/initializers

finally put <%= f.cktext_area :body %> 视图文件中的这一行。

Use following things it working for me but you should have account on Amazon for s3 storage and correct endpoint you can refer following

code.`gem 'aws-sdk', '~> 2'
gem 'aws-s3'
gem 'aws-sdk-v1'
gem 'paperclip'

class Ckeditor::Picture < Ckeditor::Asset

  AWS_CONFIG = YAML.load(ERB.new(File.read("#{Rails.root}/config/aws.yml")).result)[Rails.env]

  has_attached_file :data,
                    s3_credentials: {
                        access_key_id: AWS_CONFIG['access_key_id'],
                        secret_access_key: AWS_CONFIG['secret_access_key'],
                        bucket: AWS_CONFIG['bucket'],
                    },
                    s3_host_name: 's3.amazonaws.com',
                    :s3_endpoint => 's3.amazonaws.com',
                    storage: :s3,
                    s3_headers:     { "Cache-Control" => "max-age=31557600" },
                    s3_protocol:    "https",
                    bucket:         AWS_CONFIG['bucket'],
                    url: ':s3_domain_url',
                    path: '/:class/:attachment/:id_partition/:style/:filename',
                    default_url:   "/:class/:attachment/:id/:style/:basename.:extension",
                    default_style: "medium"

  validates_attachment_size :data, :less_than => 2.megabytes
  validates_attachment_presence :data

  def url_content
    url(:content)
  end
end

`

comment this line require "ckeditor/orm/active_record" from /config/initializers

finally put this line in <%= f.cktext_area :body %> view file.

七秒鱼° 2024-09-25 12:24:44

除了 Zaparka 的响应之外,我还必须删除 #{Rails.root},因为我收到了统一的常量错误。所以我输入了“/config/s3.yml”,这样就成功了。

In addition to Zaparka's response, I had to remove #{Rails.root} as I was getting an unitialized constant error. SO instead I put "/config/s3.yml", and that worked.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文