将 CKEditor 与 Rails 3.1 Asset Pipline 集成

发布于 2024-12-12 07:56:26 字数 736 浏览 0 评论 0原文

我是资产管道的新手,刚刚从 Rails 3.0 迁移过来。我正在尝试将 CKEditor 纳入管道,但它的所有宝石都不清楚它们是如何工作的,并且很少或根本没有使用说明。

我更愿意在不使用 gem 的情况下执行此操作,因为似乎我所要做的就是将源文件放入 vendor/assets 目录中,然后将它们包含在 application.js< /代码>。我已经尝试过了,但是,当我预编译并推送到生产环境时,似乎找不到某些文件(例如 editor.css),并且编辑器也没有找到完全显示出来(只是空白区域)。

application.js

//= require jquery
//= require jquery_ujs
//= require ckeditor/ckeditor
//= require_self

它与 vendor/assets/javascript/ckeditor/ 中的源文件一起,并指向 ckeditor.js。我只是不知道从这里该去哪里。该代码在开发中工作正常,但在生产中不起作用。在添加并提交到 git 之前,我正在运行 rake assets:precompile ,然后推送到 heroku。 这是发生的客户端错误的屏幕截图

I'm new to the Asset Pipeline, having just migrated over from Rails 3.0. I'm trying to get CKEditor into the pipeline, but all the gems for it are really unclear about how they work, and have little or no usage instructions.

I would prefer to do this without using a gem, since it seems that all I have to do is drop the source files into the vendor/assets directory and then include them in application.js. I've tried that, however, when I precompile and push to production, it seems that some of the files aren't being found (editor.css, for example), and the editor doesn't show up at all (just blank area).

application.js

//= require jquery
//= require jquery_ujs
//= require ckeditor/ckeditor
//= require_self

That's with the source files in vendor/assets/javascript/ckeditor/, and is pointing to ckeditor.js. I'm just not sure where to go from here. This code works fine in development but does not work in production. I am running rake assets:precompile before adding and committing to git, and then pushing to heroku. Here's a screenshot of the client-side errors that occur

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

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

发布评论

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

评论(8

自此以后,行同陌路 2024-12-19 07:56:26

我通过以下方式实现了此工作(部署在 Heroku 上):

  1. 将 ckeditor 代码包含在供应商/assets/javascripts/ckeditor 中
  2. config.assets.precompile += ['ckeditor/*'] 添加到我的产品中。 rb
  3. 在 application.html.erb 中设置 ckeditor 基本路径 var CKEDITOR_BASEPATH = '/assets/ckeditor/'; 在包含之前application.js
  4. 在 application.js 中,包含 //= require ckeditor/ckeditor

I got this working (deployed on Heroku), by:

  1. Including the ckeditor code in vendor/assets/javascripts/ckeditor
  2. Adding config.assets.precompile += ['ckeditor/*'] to my production.rb
  3. Setup your ckeditor base path in the application.html.erb var CKEDITOR_BASEPATH = '/assets/ckeditor/'; before the include of the application.js
  4. In application.js, include //= require ckeditor/ckeditor
沧笙踏歌 2024-12-19 07:56:26

硬着头皮使用宝石。这里有两个选项:

CKEditor Engine

https://github.com/galetahub/ckeditor

它作为引擎运行,并在资产中包含其自己的可安装 CKEditor。它还公开了 Ckeditor.assets,您可以将其添加到资产路径中。这引用了 CKEditor 所需的所有图像、插件、语言文件和杂项垃圾。

它可以处理图像上传,并且还可以与ActiveAdmin很好地集成。

CKEditor Rails

https://github.com/tsechingho/ckeditor-rails

这做得更少,你包括它在您的资产管道中,它会为您完成其余的工作。漂亮、简单,足以满足所有基本用例。

结果

我已经在现场项目中使用了这两种方法并且都完成了工作。如果您计划使用 ActiveAdmin 并且希望顺利进行,请使用前者。如果您喜欢最小化,请使用后者。

CKEditor 相当丑陋。保持一定距离,然后当您需要升级时,可以将其更换为另一个。

Bite the bullet and use a gem. Two options here:

CKEditor Engine

https://github.com/galetahub/ckeditor.

This runs as an engine and includes its own mountable CKEditor in assets. It also exposes Ckeditor.assets which you can add to your assets path. This references all the images, plugins, language files and miscellaneous little bits of junk that CKEditor requires.

It has a shot at handling image uploads and it also integrates nicely with ActiveAdmin.

CKEditor Rails

https://github.com/tsechingho/ckeditor-rails

This does less, you include it in your asset pipeline and it does the rest for you. Nice and simple and sufficient for all basic use cases.

Upshot

I have used both of these on live projects and both do the job. Use the former if you plan on using ActiveAdmin and you want a smooth ride. Use the latter if you prefer minimal.

CKEditor is pretty ugly. Keep it at arms length, then when you need to upgrade you can swap it out for another.

千寻… 2024-12-19 07:56:26

如果您使用的是 Rails 3.1.0,则应升级到 3.1.1。在此版本中,预编译 rake 任务将资产编译为原始文件名和摘要文件名。这样,不了解管道的第三方代码仍然可以工作。

您需要将 ckeditor 目录及其所有子目录添加到预编译数组中,以便预编译任务知道要编译它们。

<代码>
config.assets.precompile += your_files

your_files 可以是文件、正则表达式或过程的数组 - 捕获 ckeditor 文件名称所需的任何内容。我没有方便的 ckeditor 来计算预编译中需要的内容,所以如果您发布您的想法,其他人可能会很感激!

需要注意的一件事是,如果您为网络服务器上的 /assets 目录设置了远期标头,则需要排除 CKeditor 目录。由于这些文件不会被指纹识别,因此当您更新 CKeditor 时,某些客户端可能无法获取更新的代码,因为它们有一个标记为仅在未来某个时间过期的缓存副本,因此可能会出现问题。

If you are on Rails 3.1.0, you should upgrade to 3.1.1. In this version the precompile rake task compiles assets into both original and digested filenames. This is so third-party code that is not pipeline aware will still work.

You will need to add the ckeditor directory and all its child directories to the precompile array so that the precompile task knows to compile them.


config.assets.precompile += your_files

your_files can be an array of files, regexs or Procs - whatever is need to capture the names of the ckeditor files. I don't have ckeditor handy to work out what needs to go in precompile, so others might appreciate it if you post what you come up with!

One thing to watch is that if you have far-future headers set for the /assets directory on your webserver, you'll need to exclude the CKeditor directory. Because those files won't be fingerprinted, there may be issues when you update CKeditor with some clients not getting the updated code because they have a cached copy that marked to only expire some time in the future.

天涯沦落人 2024-12-19 07:56:26

有类似问题。对我来说,它是通过覆盖默认预编译任务来修复的(我使用 Rails 4 和 CkEditor 4)。

  1. 添加到 application.rb config.assets.precompile += ['ckeditor/*']
  2. 在 application.js //= require ckeditor/init
  3. 创建文件 lib /tasks/precompile_hook 并粘贴此答案中的文本预编译挂钩

Have similar issue. For me it was fixed by overriding default precompile task (I used Rails 4 and CkEditor 4).

  1. Add to application.rb config.assets.precompile += ['ckeditor/*']
  2. In application.js //= require ckeditor/init
  3. Create file lib/tasks/precompile_hook and paste text from this answer Precompile hook
有木有妳兜一样 2024-12-19 07:56:26

遇到了同样的问题,我已经调整了生产中尚未消化的资产的回退,直到它被修复:

config/environments/production.rb

 config.assets.compile = true

Had the same issue, I've adjusted fallback in production for the assets which had not digest until it will be fixed:

config/environments/production.rb

 config.assets.compile = true
送舟行 2024-12-19 07:56:26

/public 中的 ckeditor_assets 目录怎么样?上传的照片和附件会转到这些目录,默认情况下在 app/models/ckeditor/[attachment.rb,photo.rb] 中定义

ckeditor_assets 位于资产之外,并且图像/文件不可访问(类似于 http://yourdomain.com/ckeditor_assets/pictures/1/file.jpg 不起作用,但文件在那里)

what about ckeditor_assets directory in /public ? uploaded photos and attachments go to those directories, as defined by default in app/models/ckeditor/[attachment.rb,photo.rb]

ckeditor_assets is outside of assets and images/files are not accessible (url like http://yourdomain.com/ckeditor_assets/pictures/1/file.jpg will not work, but the file is there)

寂寞清仓 2024-12-19 07:56:26

我花了一些时间让 ckeditor_rails gem 工作;也许我可以为其他尝试做同样事情的人节省一些时间。

该 gem 在开发中开箱即用,运行良好,但当使用 Phusion Passenger 下的预编译资产部署到生产环境时,情况却并非如此。我很清楚,问题在于它正在寻找以下资产:

http://myhost.com/assets/ckeditor

实际上它需要在以下位置寻找资产:

http://myhost.com/my_app_name/assets/ckeditor

我也很清楚,我需要以某种方式进行设置:

var CKEDITOR_BASEPATH = '/my_app_name/assets/ckeditor'

但无论我在哪里或如何尝试这个,用不着。

最后,我在 gem wiki

您可以创建 app/assets/javascripts/ckeditor/basepath.js.erb 来
有你自己的CKEDITOR_BASEPATH。

我按照指定创建了文件(以及用于配置编辑器的 config.js 文件),将 CKEDITOR_BASEPATH 设置添加到文件中,重新编译了我的资产,一切都很好。

I spent some time getting the ckeditor_rails gem to work; maybe I can save some time for others trying to do the same.

The gem worked just fine out-of-the-box in development, but when deployed to production using precompiled assets under Phusion Passenger it did not. It was clear to me that the problem was that it was looking for assets under:

http://myhost.com/assets/ckeditor

where in fact it needed to be looking under:

http://myhost.com/my_app_name/assets/ckeditor

It was also clear to me that I needed somehow to set:

var CKEDITOR_BASEPATH = '/my_app_name/assets/ckeditor'

but no matter where or how I tried to do this, it wouldn't take.

Finally, I found on this key sentence on the gem wiki:

You can create app/assets/javascripts/ckeditor/basepath.js.erb to
have your own CKEDITOR_BASEPATH.

I created the file as specified (alongside my config.js file for configuring the editor), added my CKEDITOR_BASEPATH setting to the file, re-compiled my assets, and all was good.

誰認得朕 2024-12-19 07:56:26

在你的 config/development.rb 中确保设置

config.assets.precompile += ['ckeditor/*']

以及设置

config.assets.debug = true

In your config/development.rb make sure to set

config.assets.precompile += ['ckeditor/*']

as well as set

config.assets.debug = true

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