简体   繁体   中英

How to enable CKEditor on a field in rails_admin?

I'm new to rails and I recently discovered rails_admin.

I added CKEditor using the command from rails_admin documentation, but I don't know how to enable it on a field.

Just had to figure this out today. This is how I got it to work. In my config/initializers/rails_admin.rb I have the following set up.

config.model MyModel do
  edit do
    field :description, :text do
      ckeditor do 
        true
      end
    end
  end
end

Change 'MyModel' with the name of your model and ':description' with the name of the field you want to use ckeditor on. Also in the edit block make sure that you have all of your other field config.

Update

The syntax above has been deprecated in newer versions of rails_admin.

config.model MyModel do
  edit do
   configure :name, :ck_editor
  end
end

is the new syntax of doing it.

To make sure all the fields show add this to your rails_admin.rb:

   config.model Car do
      include_all_fields
     field :content, :text do
      ckeditor true
     end 
  end

Regards

Robbie

Ok anyone reading this after 2015, the above solution is deprecated and will produce a runtime error. I tried it and got the following error:

The 'field(:foo){ ckeditor true }' style DSL is deprecated. Please use 'field :foo, :ck_editor' instead.

So, with the new syntax it's like this:

config.model MyModel do
  edit do
    field :description, :ck_editor, :text do
      label 'MyLabel'
    end
  end
end

Incidentally, this works just fine if you omit :text from arguments. Tested this solution with rails-4.0.2, rack-pjax-0.8.0, and ckeditor-4.1.4. Good luck!

@Kris, @tomcocca

I did the above snippet like tomcocca showed, but i had one major problem. The problem was that when i ran rake db:drop db:create db:migrate, rails would throw errors because the table was not yet initialized.

Second problem was that when you defined a model like this, you then have to define each and every field afterwards. so in this case, only "description" shows, unless you add the other fields.

In regards to the first issue, the author of this gem responded to my issue and wrote:

Ruby on rails, run a method on server start 2.3 Maybe this should be included in RailsAdmin? Can you work on a pull request?

re: a pull request (working on it)

but this solution works for both of my issues:

config.models do
  fields_of_type :text do
    ckeditor true
  end 
end

This way ckeditor get loaded on all my textareas, plus all the other model's fields, plus i don't get the issue with rake db:drop db:create...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM