Rails App - A Little Design
Phase Three
In the last module we built up fields to handle the transcription fields we wanted using the scaffolding that Rails provides. This gets us down the road quite a ways in creating a new application, but it doesn’t necessarily look very nice. Since we have to look at this, now is a good time to get a bit further into implementing an updated view from the View layer using Ruby’s built-in ERB templating language.
Don’t tell the instructors in your local web design course, but we’re going to use a shortcut in our design and layout with the Twitter Bootstrap CSS framework not only to make things look a lot better, but also to give us a version of the application that works as well on mobile devices as it does in the browser on a computer.
Include the CSS
We need to tell the main application layout to include the Twitter
Bootstrap styles from their CDN.
Open app/views/layouts/application.html.erb
and add the following under
the stylesheet_link_tag
line:
Now find the main yield
section and surround it with a
div.container
.
If you’re like me, you’re impatient and want to see what changed. Start
up your server (bin/rails server
) and see if you see anything different at
http://localhost:3000/transcriptions/.
JavaScript
Twitter Bootstrap makes use of JavaScript to help with a few
interactions. Let’s add that library in. Again, in the
app/views/layouts/application.html.erb
file, just before the closing tag for the<body>
element, add these new footer
and script
elements.
If you start your web server up again, you should see the new footer on
the page, and it should also be loading the bootstrap
library of
effects.
Question: What does
Time.now.year
do? Why might this be better than manually typing out “2014”?
Navigation
We want to add a cool navigation header on the page. We want to include
a menu option for mobile users that displays all the pages, as well as
the name of the app and a link to the transcriptions. Add the following below the body
element and before the div.container
.
Fire up your browser now and look at http://localhost:3000/transcriptions. Does it look correct?
Let’s fix that now. We need to update the stylesheets for Rails. We
won’t get too much in to SASS/SCSS in this course, but it’s a
superset of CSS that has some really cool features added. However, for
our purposes, we’ll keep it “simple” and just write to the SCSS file
with CSS. Open app/assets/stylesheets/transcriptions.scss
and add
the following rules:
If you refresh your the page of transcriptions http://localhost:3000/transcriptions, you should see something that looks much better.
There are still some problems though. Notice how username came out looking a bit wonky? Can you figure out how to fix that? What box might you need to modify in the CSS/SCSS?
Git
Now that the main components are working and we have the content
working, it’s a good time to add and commit the changes to git
.
Scaffold Styles
So your app doesn’t use browser defaults, Rails adds a few styles when you run
the scaffold
generator. However, we have some better styles for much of what
we want to do from Twitter Bootstrap. Let’s just go ahead and remove the
generated file. We’ll use the git
command to do this.
If you start the server up again, and look at the [transcriptions][t], does this look any different?