rails 4 with CKeditor

和自甴很熟 提交于 2019-12-03 03:57:03

问题


I cannot get the galetahub ckeditor gem to work with Rails 4 for me. I searched for any problems online but cannot find any. I'm following the instructions exactly.

  • I include gem "ckeditor" in my Gemfile
  • I include gem "carrierwave" and gem "mini_magick"
  • I run rails generate ckeditor:install --orm=active_record --backend=carrierwave
  • I run rake db:migrate
  • Inside application.rb I include config.autoload_paths += %W(#{config.root}/app/models/ckeditor)
  • Inside routes.rb I have mount Ckeditor::Engine => '/ckeditor'
  • I'm using SimpleForm so I paste the following ERB <%= f.input :description, as: :ckeditor %> in my view.

And I think that's it. But my text area does not convert to a CKeditor area for some reason.


回答1:


I have the same problem using rails 4 and apparently the problem is that the form helper

form.cktext_area

Or in your case

f.input :description, as: :ckeditor

it's not generating what it supposed to generate, and you don't have to load the editor manually, the only thing you need to do is to is to add the class 'ckeditor' to your textarea and it will load automatically, like this:

f.cktext_area :body, :class => 'ckeditor'



回答2:


STEP 1: Add gem 'paperclip' and gem "ckeditor" in your gemfile.

STEP 2: Bundle Install.

STEP 3: rails generate ckeditor:install --orm=active_record --backend=paperclip

STEP 4: Place config.autoload_paths += %W(#{config.root}/app/models/ckeditor) in application.rb

STEP 5: Place mount Ckeditor::Engine => "/ckeditor" if not present in routes.rb already and run db:migrate

STEP 6: Open application.html.erb and place this <%= javascript_include_tag 'ckeditor/ckeditor.js' %> in header.

STEP 7: Place this in footer(above the body tag) in application.html.erb

<script type="text/javascript">$(document).ready(function() {
    if ($('textarea').length > 0) {
        var data = $('textarea');
        $.each(data, function(i) {
            CKEDITOR.replace(data[i].id);
        });
    }
});</script>

STEP 8: Restart the WEBrick SERVER.

That's it.

Else

Download the CKEditor Zip file, extract the files and place them in the sub directory “javascripts/ckeditor”, add the main JS file to the layout..

javascript_include_tag 'ckeditor/ckeditor.js'

Place this in footer(above the body tag) in application.html.erb

<script type="text/javascript">$(document).ready(function() {
    if ($('textarea').length > 0) {
        var data = $('textarea');
        $.each(data, function(i) {
            CKEDITOR.replace(data[i].id);
        });
    }
});</script>



回答3:


Meanwhile the Galetahub gem has been updated, but it has to be updated in your app manually. Read the github page: https://github.com/galetahub/ckeditor.




回答4:


ajkumar basically answered the question well already, but if you are still lost, all you need to do is download the js file, include it in your html, have a script snippet included in the HTML to activate ckeditor on a certain textarea tag ID, and then change the class of the "textarea" tag you want to change to ckeditor. Quick sample below

<!DOCTYPE html>
<html>
    <head>
        <title>A Simple Page with CKEditor</title>
        <!-- Make sure the path to CKEditor is correct. -->
        <script src="../ckeditor.js"></script>
    </head>
    <body>
        <form>
            <textarea name="editor1" id="editor1" rows="10" cols="80">
                This is my textarea to be replaced with CKEditor.
            </textarea>
            <script>
                // Replace the <textarea id="editor1"> with a CKEditor
                // instance, using default configuration.
                CKEDITOR.replace( 'editor1' );
            </script>
        </form>
    </body>
</html>



回答5:


The galetahub gem is currently broken on Rails 4. This one is working fine though: https://github.com/tsechingho/ckeditor-rails




回答6:


In case you are having trouble making it work with active admin, make sure to put this:

config.register_javascript 'ckeditor/ckeditor.js'
config.register_javascript 'ckeditor/init.js'

Into config/initializers/active_admin.rb



来源:https://stackoverflow.com/questions/18752766/rails-4-with-ckeditor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!