When starting a project Rails automatically creates a large tree structure of folders and files, so what are they all for? Find out in this screencast.
Creating a Project.
To create a project in Rails you need to be in your Terminal. Find an appropriate place to store your applications, so if you're on a Mac the "sites" folder is probably a good idea.
cd sites You can then generate the project by simply using the "rails" command followed by the name of your project.
rails store Once all these files have been created. You will want open them in your editor, I recommend TextMate if you're a Mac user, but anything that has a tree structure for files and ruby syntax highlighting will do.
cd store mate . Now all these folders might be a little daunting to begin with but I'll explain them one by one. At the top we have our "app" folder, coincidently this is the folder which most of the logic behind your application will be put.
M.V.C
Rails follows the MVC (or model view controller) pattern. The way this works is it separates your code into a logical structure. Imagine a user navigates to a page on your website. Their request is first sent to the relevent controller, which picks out an action, like viewing the index, or updating an item.
The controller then loads the objects, or models it needs and maybe performs some updates on them. Models are linked to the database via ActiveRecord which means you don't really need to write any database code (such as SQL) until you're getting into advanced applications, so that is one worry you can forget about.
When the controller is finished detailing with the model it will pass on any information to the view, which will turn it into HTML, XML, RSS or whatever file format you want, before passing it back to the browser.
You might notice there is also a "helper" folder within the app directory, helpers are basically small functions you can write to save you time and tidy your code up in the views.
The Rest.
Next we have our "config" directory, this contains all the files that are run during booting your application. The most notable are your database.yml file, which stores your database access details. The routes file which basically decides what your URLs look like and maps them to the right controllers. And finally the environments folder which stores the different settings your application can run in, the defaults are development, production and test, however some people make a fourth for staging to test performance.
Your "db" directory will store the migration files you create, however they will be discussed in a separate video so don't worry about them.
The "doc" directory doesn't contain anything of significance at the moment.
You will use the "lib" directory to store custom classes for blocks functionality you've factored out of models and controllers.
One of the most important features of Rails is the log, the files store all the information you need to know while developing such as what queries are running, the performance of each page etc.
Your "public" directory is mapped to the root URL, so in plain english that means that rather than going to example.com/public/images people just visit example.com/images.
In the terminal you will run commands like script/server to boot up the server, these are all stored in the "script" directory, so you can leave that one alone for the time being.
You may have come across the acroynms TDD and BDD, which translate to test driven development and behaviour driven development. What these basically mean is that rather than waiting until the end of your project to test it, you test as you go along, so you will instantly pick up on any issues that might occur when you update your code. These tests are obviously stored in the test directory, unless you are doing BDD with Rspec - but more on that in another video.
The "tmp" directory stores your temporary files such as cache files, sessions and the like, no need to worry about that one.
Finally you have the "vendor" folder, this stores any plugins you have installed, the edge version of Rails if you're using that and any gems you have bundled into your application.
discuss this topic to forum
