Ruby on Rails: Creating a (Very) Simple Blog!
Usually when I'm doing the first application of a new language, I would do a "Hello World" application (or something similar), but as usual - rails is different!
Hello-world applications in rails require you to make at the very least a controller - so we may as well go the whole way to make a simple blog!
Since you probably don't have a clue what I just talked about (with controllers and the lark), here are a few definitions for you:
Models - Models basically represent the data of the application, and give rules on how to manipulate the data.
Views - Views basically contain the layout and how you want to display your information. These are usually ".html.erb" files.
Controllers - Controllers are basically a bridge from models to views, they ask the models for data, and pass that data on to the views to present.
Actions - Actions are basically what they sound like - they are different pieces of ruby code in the controllers to react to certain situations.
Ok, so with that in mind lets get started with making our blog!
First of all to create our blog you need to locate the folder in which you want the application created, these is usually done by using the "cd" command to change directory in the terminal; for example I use something like this to go into my rails_apps folder:
cd rails_apps
Now that we're in the directory where we want to create our application - we need to run the command to actually create the application. This is done using the "rails new" command followed by what you want your rails application to be called - for example:
rails new Blog
I've called my application "Blog".
On running the command, the output will show you all of the many files that rails has generated for us. These include default controllers, views, stylesheets and a bunch of other stuff (all of which we can customise if we so choose!).
Now to make our blog actually work - we're going to need some way of creating, listing, viewing, editing and deleting posts.
The easiest way to do this is to create what is called a scaffold. When we create (or generate) as scaffold, rails creates a model (for the way that our posts interact with other things), a controller (to handle different actions such as index, show, create, destroy), some views (for each of the different actions), a migration and some other stuff that we don't need to worry about right now.
You should understand why it's generated the things I've listed, apart from a migration (as you probably don't know what this is!). Migrations are essentially just documents which show the changes we want to make to our database (which of course will store all of our post information).
In our case - the migration will tell the database to create a new table for our posts, and various columns for whatever values we've decided that our post scaffold needs.
So let's actually generate the scaffold. We need to specify the command "rails generate" - this is used whenever we want to generate something (for example we will learn about generating migrations in later tutorials). We then specify what we want to generate, "scaffold" in our case.
After this we tell it what we want our scaffold to be called - this is very important as we will refer to it's name all over the place, in our case we'll want something like "post".
Finally we specify what columns we want our scaffold to have - in this example we'll go with a name (which is of the string datatype) and some content (which is of the text datatype).
In my example, I'm going to generate the scaffold using the following command:
rails generate scaffold post name:string content:text
OK, so rails has now created pretty much our whole blog application because as I've mentioned before - rails functions on the "standard application". We can index (list), show (view), create, destroy (delete) and edit all of our posts as default functionality! Note that we can do this because these actions have been generated in our posts controller.
There is one more thing we have to do though before we can test out our application - I mentioned that rails generates a migration file. We can see this if we want in the db/migrate folder (we'll learn how to edit and create these in a later lesson) - but for now we're really just interested in "making it go" so that our database creates the tables and columns as specified in the migration file.
To get rails to make the changes to the database, we simply use a "rake" command that makes the database migrate:
rake db:migrate
OK, so now that our database is all setup to work with the application - we should actually be able to test our simple blog out!.
To see it in action we need to simply start the server, this can be done using the "rails server" command:
rails server
You should get some output saying that everything has gone well and that it's started up, and it should tell you the port that it's started up on with something like this:
http://0.0.0.0:3000/
In my case, 3000 is the port number and it says 0.0.0.0 to show that its running on your local computer.
To see the blog in action we can go in a web browser and simply access our local computer at the port 3000 (where our application is being served).
We can use either "localhost" or "127.0.0.1" to do this (and use a colon to specify the port):
127.0.0.1:3000or
localhost:3000
Going to that address will get you to the default rails homepage (since we haven't deleted this file from our public directory yet) - so to actually access our simple blog we'll either need to setup a route to tell our application what to do if someone goes to the homepage, or we can simply go to the default route that rails setup when we generated the scaffold.
This is simply /YourScaffoldName, so in our example we simply go to "/posts". The full URL for testing would be:
127.0.0.1:3000/postsor
localhost:3000/posts
In testing you should be able to see that we can successfully use all of the functionality that rails generated for us!
Back to Ruby on Rails

