Last weekend I had the opportunity to attend a Rails Girls day and I rally enjoyed it. I can't say that I really understand Ruby or Rails properly at the moment but I can hack myself through enough to get a website running. And, the online guides are amazing. It was also obvious from watching what the coaches were able to do on the day that, once you get proficient with Rails, you can very rapidly produce a lot of stuff.
But, and its a big but, there are so many new concept and things to learn.......!
So, here are some of my notes and useful commands to that helped me get going.
Installing Rails on Mavericks
Mavericks comes with Ruby preinstalled but most posts I read recommended setting up a separate environment for rails so that you don't end up braking the standard install.
I followed the very detailed instructions written by Daniel Kehoe at Install Ruby on Rails · Mac OS X Mavericks to get myself going.
Because I had been using my Mac to write PHP and C, I had the security set pretty tight which initially caused me a few problems but, if you pick a directory to install rails and give yourself permission for it and its subdirectories you should be fine.
My first rails app
The Rails Girls website is awesome for getting started and I followed the Rails Girls App tutorial as a first step. The Rails guides are also a really good reference. Of course, as with learning any language, one of the hardest first steps is learning what you actually need to ask for when looking for answers. For Rails, concepts such as "convention over configuration" take some getting use to if you come from a C style language background, but I'll come back to that again later.
If you're familiar with web development, its not too much of a leap, apart from the specific Ruby and Rails stuff, but it largely makes sense.
To further explain a few things from the Rails Girls app tutorial for myself:
Rails is an MVC framework:
- The Model is the link between the application and the data store or database and where the "back-office" or business logic processing gets done.
- The Controller is is essentially the link between the Model and the browser. The Controller manages user requests, sends off instructions to various Model methods which get data, perform calculations and return results to the controller.
The Better Explained website has a more detailed explanation of MVC if you need more information.
There are a few useful terminal commands to understand:
- rails server (shorten to rails s) starts a web server from the current directory
- ctrl c to end the server session
- rails console (shorten to rails c) starts a ruby console session (irb>)
- exit to stop the console session
- ActiveRecord::Base.connection.tables will return a list of all the tables in the current project's database
- If you have a table called "customers" for example, entering Customer will give you a list of the columns and data types for that table. Note the use of a capital first letter and singular instead of plural. This is important.
- Customer.count will give you the number of records in the table
- Customer.all will return all the records from the table..
- Customer.first will return the first row from the table.
While still a total Rails novice, I have, since last weekend, been able to add several tables, pull in data from a CSV file, pull in data from a URL, push data to a user page and chart it using D3 amongst other things. The online help is amazing and, once you get your head around a few core concepts, its not that daunting.
I'll post more here as I slowly increase my understanding of Rails.