Using the Mac OSX Terminal and Ruby on Rails
Posted September 15, 2007on:
I’m fairly new to the programming world, but have been fascinated by Ruby on Rails, and the apparent ease in using it. However, I’ve always had problems with getting the coding just right in the Terminal. Because I’ve never had a class or anything, I’ve not really known how or why things worked the way they did in the Unix command line. So the following is just a snippet that helped me in understanding how a small part of the command line works in relation to setting up a Ruby on Rails demo application. It may be helpful to others, it may not. I’m just writing it because it was helpful for me.
This is me logged into Terminal. From here, I can open an application by simply writing:
And Textmate opens up. This option doesn’t seem to work with all applications (for instance if I type “daniel$ Safari” the Terminal will come back with a response saying “-bash: Safari: command not found”. I don’t get this part of the Terminal just yet). Other applications that seem to be based on the command line, like MySQL work just fine if you type them here (ex. “daniel$ mysql”).
Now, the “daniel$” portion of the Terminal is basically telling me that I can open up files, folders and applications that are in the Daniel User section of my hard drive. (Maybe that is why it won’t open up applications that are based in the Applications file—and again I don’t know the code to open files from the Applications folder—I guess I should look it up). In any case, if I want a particular file or folder opened up in Textmate from the Daniel root folder, I merely type:
daniel$ mate demo
And the demo folder opens up. If this folder is in another folder in the Daniel root folder then I type:
daniel$ mate Sites/demo
If the “demo” folder happened to be in the Sites folder.
Using the “cd” command moves the Terminal into the folder you desire to work in, like so:
daniel$ cd Sites
That moved our Terminal into the folder Sites. Any command we give from here on out will only work in the Sites folder unless we give the following command:
/Sites daniel$ cd ..
This will command the Terminal to shift back one folder.
If we go back into the sites folder:
daniel$ cd Sites
Here we can now create a Rails application.
/Sites daniel$ rails demo
Because I’ve already installed rails and ruby and mysql and all the other required applications, the computer automatically runs and creates exactly what I need. If you run the following command (ls -p) —that is a lower case ‘l’ (as in letter), you get a listing of all the files under Sites. To get into the demo folder, we do
/Sites daniel$ cd demo
Which gives us
Now that we’re in the demo folder, we can make the Rails application run. At this point, simply creating the files doesn’t “turn on” the engine sort to speak. To run the application, we give the following command, while in the demo folder (this command will not work in the Sites folder).
/Sites/demo daniel$ ruby script/server
This starts the server. If we go with our browser to the http://localhost:3000 URL, we’ll see the traditional Welcome Aboard file.
From this point on we will need numerous applications for designing a great websites. Textmate will edit our files. The Terminal will run our commands and mysql database. Any browser will preview our website. Any image editor will create our appropriate images.
One other note. Once you run your server, you need to open a new command shell to run other Terminal applications like mysql.
If you wish Textmate to open your demo file, make sure you’re in the Sites folder first like so:
/Sites/daniel$ mate demo
Otherwise if you do this:
daniel$ mate demo
You merely create a new file called demo and not open the demo folder you created using rails.
To create a controller called “Say” type in the command line:
/Sites/demo daniel$ ruby script/generate controller Say
Several files will now be created.
And so on. There is obviously more, and from here on out the various books out there do a great job in helping you learn Ruby on Rails. I hope this little tidbit has helped out someone else. If not, no worries. It was helpful for me to write this down.