Rails 101: File Structure

December 28, 2008

Rails File Structure

When starting a project Rails automatically creates a large tree structure of folders and files, so what are they all for?

You can download a video version of this article.

Creating a Project

To create a project in Rails you need to be in your command line interface. Find an appropriate place to store your applications, for example, if you’re on a Mac, the sites folder is probably your best bet.

cd sites

You can then generate the project by simply using the rails command followed by the name of your project, for example:

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.

app

At the top we have our app folder, coincidently this is the folder which most of the logic behind your application will be put.

Model View Controller

As mentioned in the previous article, Rails uses a MVC format to keep your code organised by separating 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, by default, 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 less worry.

When the controller is finished detailing with the model it will pass on any information to the view, which will create the HTML, XML, RSS or whatever file format you want, before passing it back to the browser.

You might notice there is also a helpers 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.

config

The config directory contains all the files that are used during booting your application. One of the most important files is your database.yml file, which stores your database access details.

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.

Initializers are used to run pieces of code during boot up of the application, such as setting up an exception notifier like Hoptoad.

Introduced in Rails 2.2.2 was internationalisation or I18n, the locales folder contains the yml configuration files for all your supported languages.

And finally the routes.rb file which basically decides what your URLs look like and maps them to the right controllers. You don’t need to worry about the others.

db

Your db directory will store any file-based databases, such as those using SQLite and the migration files which are basically Ruby-based commands which alter the database as you develop your application, ready for deployment.

doc

You can use the doc directory to store any documentation you might have for your application, or use the rake rdoc command to generate code documentation automatically.

lib

You will use the lib directory to store custom classes for repeat functionality you’ve sliced out of models and controllers.

log

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.

public

Your public directory is mapped to the root URL, so that means that rather than using paths prefixed with /public you can just address the file within the public directory as such: example.com/images/my-image.jpg (as apposed to example.com/public/images/my-image.jpg).

script

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.

test

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. Because TDD & BDD are big in the Rails community, the test directory is an important part of the framework.

tmp

Cache files, sessions and process identifiers are stored in here – nothing to be bothered about really.

vendor

This stores any plugins you have installed, the edge version of Rails if you’re using it and any gems you have bundled into your application.

Read the next in the series: Generators ยป

About The Author

-

Ryan Townsend has been developing websites and web applications professionally for almost 5 years and has just graduated with a BSc in Internet Computing from The University of Manchester.

Discussion

5 people have voiced their opinions, voice yours?

-

Oxisadsisse said:

Edit | Destroy

-

mrh44 kv1nd said:

UihEw, <a href="http://www.ujbk0ucuhoa.com">9mnle</a> , [url=http://www.ufeuvrpyff.com]arosh[/url], http://www.uucv2ikdx2.com q1bme

Edit | Destroy

-

2o4wl bds1u said:

TaSYY, <a href="http://www.kmzpdn5qir.com">ijkl9</a> , [url=http://www.rmwb88bd82.com]ir2yfb[/url], http://www.d4bqzc3sqys.com 5p5yz

Edit | Destroy

-

fvtfnqkms said:

http://community.devexpress.com/members/generic-viagra-cheap-online.aspx <a href=http://community.devexpress.com/members/generic-viagra-cheap-online.aspx>amsterdam holland viagra maldonadocen </a> http://www.codeproject.com/Members/Order-ambien <a href=http://www.codeproject.com/Members/Order-ambien>buy ambien and no prescription constructions </a> http://community.devexpress.com/members/Purchase-phentermine.aspx <a href=http://community.devexpress.com/members/Purchase-phentermine.aspx>phentermine 180 count with no prescription overnight shipping dealer </a> http://community.devexpress.com/members/Order-Valium.aspx <a href=http://community.devexpress.com/members/Order-Valium.aspx>cheap valium without a prescription bickettwpafb </a> http://community.devexpress.com/members/Order-tramadol.aspx <a href=http://community.devexpress.com/members/Order-tramadol.aspx>buy tramadol fedex jparchmaemail </a> http://www.codeproject.com/Members/Order-phentermine <a href=http://www.codeproject.com/Members/Order-phentermine>phentermine no script overnight Although </a> http://community.devexpress.com/members/Order-xanax.aspx <a href=http://community.devexpress.com/members/Order-xanax.aspx>xanax price for xanax Disabilities </a> http://community.devexpress.com/members/Order-ambien.aspx <a href=http://community.devexpress.com/members/Order-ambien.aspx>order 10 ambien testimonials </a> http://community.devexpress.com/members/order-Cialis.aspx <a href=http://community.devexpress.com/members/order-Cialis.aspx>soft tab cialis preparatory </a> http://www.codeproject.com/Members/Order-soma <a href=http://www.codeproject.com/Members/Order-soma>BUY SOMA OVERNIGHT DELIVERY HEINSOHN </a> http://www.codeproject.com/Members/diazepam-online <a href=http://www.codeproject.com/Members/diazepam-online>discount online diazepam 76904 </a> http://community.devexpress.com/members/Order-adipex.aspx <a href=http://community.devexpress.com/members/Order-adipex.aspx>buy adipex 37.5 mg likewise </a> http://community.devexpress.com/members/Order-Soma.aspx <a href=http://community.devexpress.com/members/Order-Soma.aspx>buy cheapest online soma fields </a> http://www.codeproject.com/Members/Order-tramadol <a href=http://www.codeproject.com/Members/Order-tramadol>cheap pharmacy refill tramadol ultram 15341537 </a> http://vymee.com/forums/t/28.aspx <a href=http://vymee.com/forums/t/28.aspx>price tramadol f </a> http://www.codeproject.com/Members/Order-xanax <a href=http://www.codeproject.com/Members/Order-xanax>xanax prescription from doctors online Queensland </a> http://www.codeproject.com/Members/Order-valium <a href=http://www.codeproject.com/Members/Order-valium>order valium online nampak </a> http://vymee.com/forums/t/27.aspx <a href=http://vymee.com/forums/t/27.aspx>generic prescription viagra without conceived </a> http://www.codeproject.com/Members/viagra-without-script <a href=http://www.codeproject.com/Members/viagra-without-script>real viagra free shipping Style </a> http://www.codeproject.com/Members/Cialis-with-no-prescription <a href=http://www.codeproject.com/Members/Cialis-with-no-prescription>discount viagra cialis wallace </a> http://www.codeproject.com/Members/Order-adipex <a href=http://www.codeproject.com/Members/Order-adipex>generic adipex no prescription Stirling </a> http://community.devexpress.com/members/Order-adderall.aspx <a href=http://community.devexpress.com/members/Order-adderall.aspx>smoking adderall Build </a>

Edit | Destroy

-

eqwvmxvljnq said:

RuFXr9 <a href="http://kbwjuwlbnpxf.com/">kbwjuwlbnpxf</a>, [url=http://iuswszlogimk.com/]iuswszlogimk[/url], [link=http://jnmhmzjoogqw.com/]jnmhmzjoogqw[/link], http://oijhabqqpclf.com/

Edit | Destroy