开发者

How to enable CKEditor on a field in rails_admin?

开发者 https://www.devze.com 2023-02-02 12:51 出处:网络
I\'m new to rails and I recently discovered rails_admin. I added CKEditor using the command from r开发者_高级运维ails_admin documentation, but I don\'t know how to enable it on a field.Just had to fi

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

I added CKEditor using the command from r开发者_高级运维ails_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...

0

精彩评论

暂无评论...
验证码 换一张
取 消