Fork me on GitHub

Project Notes

Ruby on Rails 6

Running Rails 6, learning about changes and new features, and testing out my favourite gems including: HAML

:arrow_forward: return to the Catalog


Rails 6 was release August 16, 2019.

Major features in Rails 6:

Upgrading to Rails 6.0

Recommended approach for upgrading an existing application:

Checking Pre-requisites and Installation

$ node -v
$ npm -v
$ ruby -v
ruby 2.6.0p0 (2018-12-25 revision 66547) [x86_64-darwin17]
$ sqlite3 --version
3.19.3 2017-06-27 16:48:08 2b0954060fe10d6de6d479287dd88890f1bef6cc1beca11bc6cdb79f72e2377b
$ gem install rails -v 6.0.0
$ rails --version
Rails 6.0.0

Fixing missing dependencies

Node.js not installed. Please download and install Node.js

brew install node

Yarn not installed. Please download and install Yarn from

brew install yarn

Could not get it to work with node v12.8.0; had to downgrade to 11.15.0

n 11.15.0

Creating a Simple App

$ rails new minime6
$ cd minime6
$ rails db:migrate
$ rails server
=> Booting Puma
=> Rails 6.0.0 application starting in development
=> Run `rails server --help` for more startup options
Puma starting in single mode...
* Version 3.12.1 (ruby 2.6.0-p0), codename: Llamas in Pajamas
* Min threads: 5, max threads: 5
* Environment: development
* Listening on tcp://localhost:3000
Use Ctrl-C to stop

Hit http://localhost:3000..


Adding a Welcome Page

Using controller generator:

bin/rails generate controller Welcome index

Adjust routes to use this as the main page.



$ rake test
Run options: --seed 13985

# Running:


Finished in 4.183906s, 0.2390 runs/s, 0.2390 assertions/s.
1 runs, 1 assertions, 0 failures, 0 errors, 0 skips

Adding The Blog App

The getting started guide has always outlined a simple blog application. Adding the basic features to minime6:

rails generate controller Articles
rails generate model Article title:string text:text
rails db:migrate

Using HAML

I prefer haml for templating in Rails. Does it still work for Rails 6? Yes!

While one can just add the haml gem, I’m using haml-rails which adds a rake task for converting Erb files. I ran thisk over all files in the app:

$ rails haml:erb2haml
Generating HAML for app/views/articles/index.html.erb...
Generating HAML for app/views/articles/edit.html.erb...
Generating HAML for app/views/articles/show.html.erb...
Generating HAML for app/views/articles/_form.html.erb...
Generating HAML for app/views/articles/new.html.erb...
Generating HAML for app/views/welcome/index.html.erb...
Generating HAML for app/views/layouts/application.html.erb...
Generating HAML for app/views/layouts/mailer.html.erb...
Generating HAML for app/views/layouts/mailer.text.erb...
HAML generated for the following files:
Would you like to delete the original .erb files? (This is not recommended unless you are under version control.) (y/n)
Deleting original .erb files.
Task complete!
No .erb files found. Task will now exit.

Rendering Multiple Formats

Using respond_to

The default Rails mechanism, demonstrated with the Time::NowController.

  def show
    respond_to do |format|
      format.json { render json: time_data }
      format.xml { render xml: time_data.to_xml(root: 'time') }
      format.text { render plain: @time }

Generating JSON:

$ curl -H "Accept: application/json" http://localhost:3000/time/now
$ curl http://localhost:3000/time/now.json

Generating XML:

$ curl -H "Accept: application/xml" http://localhost:3000/time/now
<?xml version="1.0" encoding="UTF-8"?>
$ curl http://localhost:3000/time/now.xml
<?xml version="1.0" encoding="UTF-8"?>

Generating plain text:

$ curl -H "Accept: text/plain" http://localhost:3000/time/now
$ curl http://localhost:3000/time/now.txt

Rails Controller Testing

rails-controller-testing adds back the assigns and assert_template methods for controller testing (these were removed from rails core in 5.0)

Working in Rails 6 - I’ve added an assert_template 'welcome/index' test.

Credits and References

About LCK#82 rubyrailshaml
Project Source on GitHub Return to the Project Catalog

This page is a web-friendly rendering of my project notes shared in the LittleCodingKata GitHub repository.

LittleCodingKata is my collection of programming exercises, research and code toys broadly spanning things that relate to programming and software development (languages, frameworks and tools).

These range from the trivial to the complex and serious. Many are inspired by existing work and I'll note credits and references where applicable. The focus is quite scattered, as I variously work on things new and important in the moment, or go back to revisit things from the past.

This is primarily a personal collection for my own edification and learning, but anyone who stumbles by is welcome to borrow, steal or reference the work here. And if you spot errors or issues I'd really appreciate some feedback - create an issue, send me an email or even send a pull-request.