Part 2 - Introducing Rails
Creating your (first) Rails app
Open the terminal to create a new Rails app
Find the project directory in the Finder/Explorer. Have a look how Rails generated a folder structure and a lot of files for you. Ask your coach about the details.
Starting the Rails server
A Rails app can deliver dynamically generated content with the help of the Ruby programming language. This is why we can’t just open a file anymore in the browser. We need a program that will combine the templates with the dynamic information to create the final HTML. This program is called the Rails server.
To start the Rails server, open you terminal (in the railsgirls-blog
folder) and type the following:
Once it’s started up, you can go visit your app under
The page indicates that everything worked fine and the server is running.
The power of Rails: creating a scaffold
One (not so secret) weapon of Rails is its ability to quickly get you started using scaffolds. Since we’re trying to create blog here, let’s put everything into place to create, update and show our blog articles.
To do that type the following commands into your terminal:
Now you can checkout your app again in the browser. If you check
You’re not going to notice any difference, since we haven’t told our app about what to do when someone hits the home page just yet. For now you still have to know where to look for the articles, which is under
http://localhost:3000/articles.
Click around and explore what your app can do already. Which is quite a lot. It doesn’t look very pretty yet but try to discover that creating, editing and deleting of articles completely works out of the box. Make sure to create at least three articles so we have some data in our app to play with going forward.
Tweaking the default to our needs
The power of Rails is that it can generate a lot of underlying functionality for you automatically. But what’s equally important is that it allows us to work with what was created and tweak it to our needs. Let’s try that with our articles index page to make it look a bit more like a blog.
In your editor open the file app/views/articles/index.html.erb
. You’ll notice that this looks nearly like the HTML you’re already used to. Apart from some small differences. We’ll get into those shortly. For now just go ahead and replace everything after the h1
tag with the following code:
Save it and go have a look at the result in your browser.
What about our about page now?
Let’s bring the two parts of this guide together. We want to integrate the about page we created in the first part into our Rails app. For that we will use our Rails’ generator again and let it create all the setup we need for that page. To do that open the terminal again and type the following line:
If you open http://localhost:3000/pages/about you will see the page that Rails has created for us. It understands the URL and shows us some placeholder content. Let’s now put our about page from earlier in that place. Open the file mentioned on the web page in your editor. Now paste the complete content from our about.html page into this file.
visit http://localhost:3000/pages/about
Something dynamic
Now that our page is part of a Rails app there are many possibilities of making the content more dynamic. You might have noticed the .erb
extension on our view file earlier. This allows us to run actual ruby inside our html. Whenever you see <%= %>
or <% %>
this means you’re in Ruby land within HTML. Here are a couple of examples to see that at work:
Open the file app/views/pages/about.html.erb
. See how we explicitly state the year in the section <footer>
? Let’s have Rails provide the current year for us dynamically. To do that change the code as follows, and reload the page to see that the year is still there.
-
(Show how this also works in IRB and maybe the controller)
-
Now go back to the file
app/controllers/pages_controller.rb
. We’ll be introducing a variable to hold the value of the year now.
- And in
app/views/pages/about.html.erb
:
Other examples could be the
- Calculate age of the participant
- Try a dynamic image using some API or something…
Iterate over lists
Imagine part of your pages looks like this:
Adding another language here means you have to repeat writing the HTML tags each and every time.
Have a look what we can do using dynamic lists (arrays) in ruby. In the file app/controllers/pages_controller.rb
add the following:
- And in
app/views/pages/about.html.erb
:
And now from the database
We’re ready for the next level of magic: Let’s use the database to store the languages, because editing your code every time you learn a new language is going to get boring fast. ;)
Check out http://localhost:3000/languages
Try out the forms and create, edit and delete a couple of entries.
Now see how we can use the languages stored in the database. For that we tell the controller to load the information from the database. Open the file app/controllers/pages_controller.rb
in your editor and add the line about languages.
In app/views/pages/about.html.erb
replace the line <%= language %>
with:
Now go check what the page looks like.
Let’s add a contact page and feel the need for a layout
We want to add another page to our blog that contains our contact information. Instead of using the generator we’ll do this manually so you get to see the steps that are invovled. No worries there is no need to learn those by heart. Just follow along and let’s see what happens.
Open a new file in your editor and save it as app/views/pages/contact.html.erb
. Copy paste the following content in it and save again:
We need to add something called a route so the server knows what to do if someone visits the URL /pages/contact
. For that open the file config/routes.rb
in your editor and add the following line directly above get 'pages/about'
.
Let’s see what we’ve got http://localhost:3000/pages/contact.
You have now created a dynamic web application with multiple interconnected pages. Feels good, right? :)
- High five your neighbours.
Now, let’s add a menu to connect the pages.
Add this code on top of your contact page and remember to include bootstrap, if you haven’t yet (ask your coach).
Find your pages in the dropdown menu. See that this only appears in one page. Why?
Introducing: layouts.
- Open
app/views/layouts/application.html.erb
- Move the navbar elements into this file
- Remove the common HTML from the erb pages
The Rails console
Open the rails console:
And now have another look at: http://localhost:3000/languages. What changed?
Rubygems
Ask the coach “what is a gem?” and be prepared for an interesting lecture about programmer happiness.
Try adding the gem youtube_addy to your Gemfile
and creating a new scafford for videos. NOTE that the keyword raw
needs to be added to make the gem work correctly (<%= raw YouTubeAddy.youtube_embed_url("your-url-here",420,315) %>
).
Ask the coach for his/her favourite gems.