The Revelation needs YOU
Recently, I started a new programming project at home, in Ruby on Rails, and it brought to mind a blog post I wrote waay back in 2007.
Starting a Java program from scratch is so filled with overhead that experienced programmers I trust have just told me to use tutorial source code to start my project.
That’s good, solid, pragmatic advice, but it kinda feels like cheating.
The barrier to entry for Ruby/Rails is way less. Sure, you have to download Ruby and install the gems and Rails, but all of that is fairly easy.
From there, you navigate to the folder you want to contain your project folder (I like c:\projects\ or /home/liam/projects/) and type the following:
rails [projectName]
if you want to associate it with mysql (sqlite is the default) run this instead of the above:
rails -d mysql [projectName]
This creates the directory structure and the skeleton files you need. From there, it is a matter of another command line to add an appropriate business object.
(from inside the c:\projects\projectName folder) ruby script/generate scaffold [object_name]
This line does a lot of heavy lifting:
It creates skeletons for the model, the controller, the view, database migration file, routing information, helper files, and unit tests.
View files include edit, new, and list pages, as well as a layout page to customize that business object’s pages.
Controllers contain basic functionality for add, edit, and remove, as well as redirection logic for new, edit, and list.
Database migration is a wonderful thing. All of the database work is done in Ruby. Ruby and Rails provides you three schemas to work with: Testing, Development, and Production. This allows you to fully integration-test without touching other data. Also, updates to the database are immediately reflected in the schema file so you dont’ have to update your models or work in SQL to update your schema. Win-win from my perspective.
Once the scaffold generation is complete, yes, you actually have to do some work.
Populate the migration script with the data you want on the model/table, and code up the html.erb pages.
Before running the application, you’ll have to create your schema in mysql:
(in projects/projectName) rake db:create:all
and migrate to the newest version of the database:
rake db:migrate
There might be a little more work than that, if your development database has different authentication, you’ll probably have to find the database.yaml file and update it, but it should be obvious how to change the settings in that file.
Granted, the next step is one I’m not entirely sure of. I’ve always coded these applications inside an IDE, and the servers have always come with the IDE. I choose which one I want (usually mongrel, just because) and run it inside the IDE. If you are not such a person, never fear. I will do some last-second research and learn how to run it.
…
…
This just in: run the following (from projects\projectName)
ruby script/server
and that should start your server up on the default port of 3000.
Routing is a little different from what I was used to, back in the day, so I’ll go into it a little bit.
All of this is assuming a base url of http://localhost:3000/
So, if you want to look at the list page of Object:
objects
If you want to look at the new page of object:
objects/new
View:
objects/[id]
Edit:
objects/edit/[id]
If anyone is using these instructions and something I’ve said is wrong (which is possible) or a lie (which is likely), leave me a comment, and I’ll be sure to get back to you as quickly as possible.
Happy coding!