1. Computing

Step 1: Scaffolding Posts

By

Scaffolding Makes Starting Fast
Step 1: Scaffolding Posts

Every Ruby on Rails developer should have a blog. Not only does it show you're at least competent enough to write the simplest of Rails applications (and it is very simple, as you'll see), but it provides a platform for the open exchange of ideas. And besides, it's a fun project. No one (except you, of course) is relying on this project. You can try things out and if it all goes horribly wrong, that's nothing but a learning experience for you.

So let's dig in. In this first part of the series we'll get things started with a scaffold. By the end of this article, we're going to have a (technically) fully functional blog. Quite a tall order for a single article, but trust me when I say that this is really very simple. Rails does most of the work with the scaffold generator, and we'll be integrating a Wordpress theme instead of developing our own, so even there, not much work to be done.

The emphasis of this series will be on getting things done. There will be no tests this time (those can be a bit hard to swallow and just seem absolutely unnecessary if you're just starting out) and to implement things we're taking the path of least resistance. So let's dig in.

I also won't be discussing any of the specifics of how to go about editing files and get around in a Rails project other than to say I'm using the excellent RubyMine IDE. I used to be a straight up Vim person, just give me a solid editor and let me at it, but it's hard to deny just how great RubyMine is when it comes to Ruby and specifically Ruby on Rails. However, I will be showing how to do things from the command line as well.

The finished code for this tutorial can be found on GitHub and the complete code (in whatever state that might be) in the main repository. In order to clone this specific version of the project, first clone the project, then switch to the tag called v0.1.


$ git clone https://github.com/mmorin/blogtutorial.git
…
$ cd blogtutorial
$ git checkout v0.1

New Project

Go ahead and generate a new Rails project, I called mine blogtutorial. Initialize a new Git repository, add the files and commit.


$ rails new blogtutorial
…
$ cd blogtutorial
$ git init
$ git add .
$ git commit -am 'Empty Rails project'

And for every task, I like to create a new Git branch. It helps me keep things organized and if I fail completely and want to start over again I can just nuke that branch and create a new one. For this feature alone you should be using Git.


$ git checkout -b scaffold_posts

And make sure the database is created and set up by running the db:create rake task and all the gems needed are installed by running bundle install.


$ bundle install
…
$ rake db:create
…

Scaffold Posts

The scaffold generator is a good way to get up and running fast. The scaffold generator spits out a complete RESTful resource, ready to go live. The interface is a bit generic, it's nothing you'll want to keep in the long run, but it's meant to save a lot of grunt work in setting things up. So let's generate a Post model. A blog post has a title, which is a string, and a body, which is text (strings are for shorter columns, text doesn't have any real size limitations). We'll add more columns later, such as the column that says who made the post, but let's not think too far ahead. So let's run the generator and see what we get.


$ rails generate scaffold post title:string body:text
     invoke  active_record
     create    db/migrate/20130212150827_create_posts.rb
     create    app/models/post.rb
     invoke  resource_route
      route    resources :posts
…

This all looks good. We'll go ahead and add a validation to the model, a no-brainer here since we don't want title-less posts or posts with no body. Validations are called before an ActiveRecord object is saved or updated, they're there to (among other things) make sure your data is sane and complete.


class Post < ActiveRecord::Base
 attr_accessible :body, :title

 validates_presence_of [:title,:body]
end

Run the migration, fire up the server and create a few test posts using the generated scaffold interface by going to http://localhost:3000/posts.


$ rake db:migrate
…
$ rails server

The scaffold interface gives you a lot of what a blog is. You can create posts, list posts, see specific posts, etc. At this point I usually make a Lorem Ipsum post, which is useful for testing.

OK, so the "blog" works (kind of), but it doesn't look like a blog yet, but we'll get to that. Instead of writing any CSS, we're going to integrate an existing Wordpress template. There are countless Wordpress templates available for free, with liberal licenses, so even if you're not a web designer you can have an attractive blog easily.

  1. About.com
  2. Computing
  3. Ruby
  4. Ruby on Rails
  5. Ruby on Rails 3 Tutorials
  6. A Full Featured Blog
  7. Step 1: Scaffolding Posts

©2014 About.com. All rights reserved.