Create Content With Multiple Features With CKEditor Gem
Setup & Usage
First, we need to add the CKEditor gem to our Gemfile. Open up your Gemfile and add the line listed below:
Gemfile
1
gem"ckeditor"
Next, open up the terminal and run a bundle install to install the gem:
1
bundleinstall
Great, now lets create a sample model and accompanying controller that will be used to store our data. Open up and run the command below to create the Magazine model and migrate the database:
Now, open your routes file and add the following line to your routes:
routes.rb
1
rootto:"magazines#index"
Now, lets add the CKEditor javascript include to our application.js. Modify your application.js file so that it looks like the code listed below:
application.js
1234567891011121314151617
//Thisisamanifestfilethat'll be compiled into application.js, which will include all the files// listed below.//// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,// or any plugin'svendor/assets/javascriptsdirectorycanbereferencedhereusingarelativepath.////It's not advisable to add code directly here, but if you do, it'llappearatthebottomofthe//compiledfile.JavaScriptcodeinthisfileshouldbeaddedafterthelastrequire_*statement.////ReadSprocketsREADME(https://github.com/rails/sprockets#sprockets-directives) for details//aboutsupporteddirectives.////=requirejquery//=requirejquery_ujs//=requireckeditor/init//=requireturbolinks//=require_tree.
Great, now we need to add some code to the magazines controller. Add the following code to the magazines controller:
classMagazinesController<ApplicationControllerdefindex@magazines=Magazine.order("created_at DESC")enddefshow@magazine=Magazine.find(params[:id])enddefnew@magazine=Magazine.newenddefcreate@magazine=Magazine.new(magazine_params)if@magazine.saveredirect_tomagazines_path,notice:"The magazines has been successfully created."elserenderaction:"new"endenddefedit@magazine=Magazine.find(params[:id])enddefupdate@magazine=Magazine.find(params[:id])if@magazine.update_attributes(magazine_params)redirect_tomagazines_path,notice:"The magazine has been successfully updated."elserenderaction:"edit"endendprivatedefmagazine_paramsparams.require(:magazine).permit(:title,:body)endend
This code enables the ability to read, write, and update the magazines in our example. Now for the views, first lets create the index view:
index.html.erb
123456789
<%= link_to "New Magazine", new_magazine_path %><% @magazines.each do |magazine| %> <h3><%=magazine.title.html_safe%></h3><p><%= magazine.body.html_safe %></p> <%=link_to"Edit Magazine",edit_magazine_path(magazine)%> <% if magazine != @magazines.last %><hr/> <% end %><% end %>
Now, lets create a partial to store the form. Create a file called app/views/_form.html.erb and add the code listed below:
_form.html.erb
123456789101112131415161718192021222324
<% if @magazine.errors.any?%> <ul><%= @magazine.errors.full_messages.each do |message| %> <li><%=message%></li><% end %> </ul><% end %><%= form_for @magazine do |f| %><div><%= f.label :title %> </div> <div> <%=f.text_field:title%> </div><div><%= f.label :body %> </div> <div> <%=f.cktext_area:body,rows:10%> </div><div><%=f.submit%> </div><% end %>
Now, lets create the new view. Create the app/views/magazines/new.html.erb file and add the code listed below:
new.html.erb
12
<h3>NewMagazine</h3><%= render "form" %>
Now, if you visit the new magazines page on your development server you will see that CKEditor appears.
Next lets create the edit view. Create the app/views/magazines/edit.html.erb file and add the code listed below:
Great, now when you click ‘edit magazine’ on any magazine, it will show the CKEditor for editing.
Images Using Paperclip
In order to integrate images via paperclip, a few more steps are required. Note that you must have ImageMagick installed for this to work. First, lets include the paperclip gem:
Gemfile
1
gem"paperclip"
Next, we need to run a generator provided by ckeditor. This generator will create the necessary models that will be used to store image data. Run the command below:
So far so good, if you restart your rails server and refresh the page, you will be able to click the images button, upload an image, and insert it into your articles.