Today's blog is going to chronicle my attempt to build a
simple web app to list and rate books in
Ruby on Rails.
We'll see how easy it is. Ruby's not a language I usually find
myself programming in.
Naturally I'm putting the code on
Github
so you can peruse my work as I go.
I'm also going to try hosting it on Heroku at
https://book-rails-2017.herokuapp.com/
because it's a quick free solution many people use for
prototyping. Additionally, it's easy to set up automagic
deployment from Github to Heroku.
Firstoff I discovered that I had Ruby version 2.0.0 and the
Rails docs said I needed at least 2.2.2; ergo I upgraded. I
found a
solution
for upgrading on StackOverflow, but, as is often the case,
this didn't immediately work. I had to update Homebrew first
apparently. And then there was some more verbose error
output which wasn't super useful. Eventually I settled on
trying to use rvm to install version 2.2.2 rather than the
2.2.6 it was trying to install. My guess it's because I don't
have the latest OSX version. Now I have Ruby 2.2.2, so I can
get Rails.
The install of Rails using gem went off without a hitch:
version 5.0.1. I also checked for SQLite and found version
3.13.0 was there already. Awesome. Then it was just a matter
of running rails new book-rails and Rails
created everything I needed to get started.
Next up, I needed to get the app up and running. A "Hello,
World" sort of thing. Locally this was super simple, as Rails
comes equiped with a binary to do just that (bin/rails server )
setting it up on port 3000 of my localhost. All well and good.
Then it was a matter of using the rails generate
command to create a view and a controller. Then I had to
modify config/routes.rb to include it at the
root. Fairly straightforward. Next to get it onto Heroku. I
have an existing Heroku account connected to my Github account,
but this is fairly straightforward to set up if you haven't
already. Then creating the app is ridiculously easy. I just
connected it to the Github repo I made and setup autodeployment.
I didn't even have to specify that it was a Rails app. Heroku
detected that automagically! But then I ran into a snag:
apparently Rails expects SQLite will be there and Heroku
doesn't support it. So I commented out that line in the
Gemfile and the running local server automagically
removed it from Gemfile.lock . Heroku then was
happy to declare "Build Succeeded" in the build log while
still telling me "Application Error" when I tried going to the
app's URL. Only by restarting the server locally was I able
to reproduce the error though. Will have to remember that...
So Rails, much like Wordpress, requires a database underneath.
The one Heroku supports is PostgreSQL. I think I knew that. It's
one of the reasons PostgreSQL is so popular. So, following
this article
I set up the production database to be PostgreSQL by updating
config/database.yml and Gemfile .
This got it working locally again, and then a push to Github
yielded a new error on Heroku. It's an iterative process. A
search for the error message pointed me to this StackOverflow
answer.
Which didn't help. So I tried the
Heroku docs
to no avail. So I asked my own
question.
Slight confession: I've been playing on StackOverflow most
every day this year. These guys are helpful, but often a bit
pedantic. Nevertheless, they directed me to install the
Heroku CLI so I could explore the
log files to better understand the error. Looking int the log
it seems I need a version of the untracked secrets.yml
file on production. But how to get it there?
Can it be done with the CLI?
At this point I took a step back and decided to just redo
my creation of the rails app with a PostgreSQL database instead.
So I created a new branch and let rails overwrite things.
Surprisingly few files were rewritten. The new
.gitignore kinda sucked though, so I reverted it
mostly. I installed PostgreSQL with Homebrew and then initialized
the database. After a lot of back and forth it seems like my
only option is to commit secrets.yml against my
better judgement. C'est la vive.
Next I added Boostrap for a nice familiar frontend look. This
was relatively easy following the
documentation.
Rails, nicely enough, has testing included automagically. Ergo
I next worked to integrate the app with
Travis CI,
which runs
the tests. Heroku can also be set to pay attention to the
results so that if the tests fail then the app is not deployed.
This took a couple iterations to get everything right setting
up the Ruby and PostgreSQL configurations.
Next was getting to the real meat of the project. Finally, I
know right? I mocked up a book listing as well as forms to add
and update books. Then it was onto the small matter of creating
the database table to hold the information about the books.
Rather than creating it straight up in the database, I decided
to use the fully Rails solution: rails generate model .
Then it was just a matter of figuring out CRUD in Rails
terminology.
All in all it was a fun little exercise to see if I could
teach myself the basics of writing a Rails app in a day. And,
hey, maybe one day I'll get to apply it...
|