Markdown, Customizing Active Admin
What This Tutorial Covers
Markdown Rendering
Convert markdown to HTML with Redcarpet.
Active Admin
Auto-generated admin UI for Rails models.
Custom Pages
Override default admin templates and forms.
Noble Desktop's Full-Stack Web Development Certificate teaches modern web development concepts that transfer across Ruby, Python, and JavaScript stacks.
Dive deep into a comprehensive Ruby on Rails tutorial that covers rendering Markdown, customizing product columns, and personalizing the filter sidebar; this guide provides hands-on exercises and bonus challenges for a more interactive learning experience.
Topics Covered in This Ruby on Rails Tutorial:
Rendering Markdown, Customizing the Product Columns, Customizing the Filter Sidebar
Exercise Overview
In this exercise, we’ll be looking at some more of Active Admin’s features. We are going to customize the Products admin page to make it less cluttered.
If you completed the previous exercises, you can skip the following sidebar. We recommend you finish the previous exercises (8A–8C) before starting this one. If you haven’t finished them, do the following sidebar.
If You Did Not Do the Previous Exercises (8A–8C)
- Close any files you may have open.
- Open the Finder and navigate to Class Files > yourname-Rails Class
- Open Terminal.
- Type
cdand a single space (do NOT press Return yet). - Drag the yourname-Rails Class folder from the Finder to the Terminal window and press ENTER.
- Run
rm -rf nuttyto delete your copy of the nutty site. - Run
Git clone https://bitbucket.org/Noble Desktop/nutty.Gitto copy the That Nutty Guy Git repository. - Type
cd nuttyto enter the new directory. - Type
Git checkout 8Cto bring the site up to the end of the previous exercise. - Run
bundleto install any necessary gems. - Run
yarn install—check-filesto install JavaScript dependencies.
Markdown & Bulleted Lists
We’re almost done with the setup for the product pages, but we still need to fix one more thing. To see it, make sure you have localhost:3000 open in the browser and the server running.
Click on the Finger Tentacles to view its page.
To the right of the image, click on Specs.
Take note that the specs appear as a neat, bulleted list rather than as one paragraph.
Navigate to: localhost:3000/admin and log in.
Click on the Products link at the top.
Scroll down to the Finger Tentacles entry and click the Edit link to its right.
On its Edit page, scroll down to Specs. We’d like the specs on the site to appear like the list we created here.
We can use Markdown to convert our plain text to HTML-formatted bullets. Markdown allows us to write plain text using a simple syntax, which can then be converted to HTML with a Markdown processor. To create a bulleted list, we typed an asterisk ( * ) in front of each item.
We will use a Ruby gem called Kramdown to process Markdown. In a browser, navigate to: GitHub.com/gettalong/kramdown
The installation instructions are here in case you need to reference them.
In your code editor, open nutty > Gemfile.
At the bottom, add the following code:
# Parse markdown for product descriptions & specs gem 'kramdown'Save the file, then close it.
In Terminal, switch to the ruby tab (the one with the server running.)
Hit CTRL–C to stop the server.
Install the gem by typing:
bundleStart the server by typing:
rails sIn your code editor, open nutty > app > views > products > show.html.erb
Around line 51, add the code shown in bold:
<div class="tab-pane fade" id="specs" role="tabpanel" aria-labelledby="specs-tab"> <%= raw Kramdown::Document.new(@product.specs).to_html %> </div> <!—/ #specs—>A few notes on the syntax, here. Most of the action is performed by
Kramdown::Document.new().to_html, which loads a string and converts it from Markdown into HTML. We also need therawcommand at the beginning so Rails doesn’t escape the output.Save the file.
In the browser go to localhost:3000
Click on the Finger Tentacles to view its page.
Click on Specs and notice it now shows our bulleted list. Sweet!
Leave the two tabs open and the server running in Terminal so we can continue with them in the next exercise.
Bonus: Customizing the Products Admin Page
Open a browser and navigate to: localhost:3000/admin and sign in.
Click on Products at the top. It’s a bit cluttered and could use some cleanup.
For this exercise, we’ll continue working with the nutty folder located in Desktop > Class Files > yourname-Rails Class > nutty
If you haven’t already done so, we suggest opening the nutty folder in your code editor if it allows you to (like Sublime Text does).
In your code editor, open nutty > app > admin > product.rb
Add this code to customize the index page with only the columns we want:
ActiveAdmin.register Product do permit_params :title, :description, :specs, :sku, :price, :image index do selectable_column column :image do |product| image_tag url_for(product.image.variant(resize: "100x100")) end column :title column :sku column :price column :created_at actions end show doNOTE:
selectable_columnis the checkbox to the left of each item in the Products admin page, which allows you to select them.actionsrepresents the View, Edit, and Delete links to the right of each product.Save the file, go back to the browser, and reload: localhost:3000/admin/products
It looks so much better! Look at the Filters section on the right of the page. We can edit which criteria admins can use to search for products.
Return to product.rb in your code editor and add this code above the index code:
permit_params :title, :description, :specs, :sku, :price, :image filter :title filter :sku filter :created_at filter :price index doSave the file.
Go back to the browser and reload: localhost:3000/admin/products
The Filters section should look much cleaner now.
Now that we’re done with this section, we can shut down the Rails server.
Switch to Terminal.
Switch to the ruby tab (the one running the server).
Hit CTRL–C.
Quit Terminal.
Bonus Challenge: Customizing the Dashboard
Like the Products page, you can also customize the Dashboard of Active Admin.
- In Terminal,
cdinto the nutty folder:- Type
cdand a space. - Drag the nutty folder onto the Terminal window (so it will type out the path for you). The folder is in Desktop > Class Files > yourname-Rails Level 2 Class.
- In Terminal, hit Return to change directory.
- Type
Restart the server by typing:
rails sIn a browser, navigate to: localhost:3000/admin
Sign in if prompted.
Click on the Dashboard link at the top left. You can customize this page as well.
In your code editor, open nutty > app > admin > dashboard.rb
Within this file, there is sample code (commented out) of some different ways you can build cool little widgets within the Dashboard.
If you are up to the challenge, we recommend trying to create two columns on this page, one for the newest products and the other for the most expensive products.
When you are done, make sure to stop the server and quit Terminal before continuing to the next exercise.