As I mentioned before, I’m going to work on publishing a ROR User System Tutorial (RoRUST for short) that will let you create a fully managed user system. People will be able to login, edit profiles, create admins, and much more. I’m very excited to share some wisdom back to the community. So, here we go!
Today’s goal: set up the basics so we can start playing with the actual system! (feel free to fast forward through what you know, but we’ll take it slow for those following along.)
Note: This is all done on a windows machine. Some Mac things may vary…but I’m sure you’ll figure out what needs to happen…
Setting up the Directory
Throughout this process you’ll need a command prompt open. Keep it open — as we’ll come back to it often. I recommend having a folder in C:\ called rails to host most of your files. in the C:\rails directory, start a new rails program
rails membership
This will create all the directory files for a new rails project.
Next, create the user model. (The model will handle the passing of information between the program and the database.) First, go into your rails project at the command prompt (cd membership) and then At the command prompt:
ruby script/generate model user
Model created! Rails is smart and knows that because we named this model user, it’s going to interact with our users table. Rails can add the plurals to your models and is smart. Just remember that — rails is smart.
You can now use your favorite editor to mess with the files (I use RadRails) we just created.
First thing we’re going to do is set up our database. The key to a great ruby program is being intentional and well-planned with your database. Your database will drive your program and make life so much easier. You should always plan what you want to do.
You need to set-up a database to do your damage. You’ll need to do this one own, but call it: membership_development. You should then go into your editor and go to: config\database.yml and edit the code to fit your new database name, username, and password.
Now, to start setting it all up.
In your editor, go to: db\migrate\ and you should see a file labeled 001_something_or_other. These are migrations set to make your database manipulation much easier. You’ll be able to add more migrations later and continue to add to your database through rails.
Edit the 001 file to be the following:
def self.up
create_table "users" do |t|
t.column "username", :string, :limit => 24, :default => "", :null => false
t.column "hashed_password", :string
t.column "first_name", :string
t.column "last_name", :string
t.column "email", :string
t.column "url", :string
end
end
def self.down
drop_table :users
end
We're gonna add the following fields to our user database -- fields we'll want to use throughout our project (username, firstname, etc.). Notice we didn't have to put an ID field into our database -- rails will do that for us.
Now, to make this changes to the database, we need to go back to our command prompt:
rake migration
rake migrate
This will force everything in our migration files to update the database to the latest*(see note below regarding newer rails versions). Rails allows you to do a great things:
- Allow you to go forward and backward in your database iterations. Make a mistake? easy - you can go backwards. You can also push forward.
The self.up are the files that are created when you move forward. If you go backward, the self.down kicks in. More about this later.
Now we have a database with fields and we're ready to start playing with our model, but one more thing we're going to set up is our controller.
A controller builds the relationship between the user and the model. A controller can interact with many models, for this program, we're going to call ours "portal" -- something that can be our gateway to information.
Back to the command prompt we go:
ruby script/generate controller portal
Now we have a controller. Notice when you refresh your file directory in your editor that a few more files have been created. We have controllers, helpers, and more views now. We'll play with these all in good time.
Alright, that's a good starting point. Lots of jumping around and nothing to show for it, yet. Trust me, this set up is well worth the time it will take in the end. Programmers are saying that a usual 10 month project in Java can be completed in 3 weeks in Ruby. You'll love what Ruby has to offer.Hopefully you're starting to learn a little ruby. This will be a great tutorial to jump you ahead of the crowd.What's coming next time?Next time we're going to work on getting our controller and model playing together. By the end of next time we'll have our first steps of a user system, including starting to register users.Let me know if you have any questions -- I'll be back in about a week with Part 2.Update: Fixed migration error noted above -- should be "rake migrate"*Note: Those using new versions of rails may need to use: "rake db:migrate" instead of "rake migrate" -- which I've learned is being depreciated.

[...] Today we continue our adventure in Ruby creating a Last time we set-up the general background support for the system. Today, we’re going to make some magic appear on the screen, and get some data on our pages. [...]
You really should be using Camtasia Studio to do these!
They would be much easier for me to follow…
yah — i should make it a more video based thing. downside is you can’t copy/paste the code. maybe the next installment
I was going through this tutorial today, and when I go to do a “rake migration” I get a message that says, “rake aborted! Don’t know how to build task ‘migration’ (See full trace by running task with –trace). When I do that I get a few lines of C:\…text but that’s about it…I’m not sure if I’m running a different version of rails then you?
Thanks AJ. I noted the changes above…just a typo.
Also added a note about newer versions of ruby and db:migrate.