Rails – What Goes Where
Homes for Your Logic, CSS, HTML, and More
Rails is a framework. (If you didn’t know that before starting this article, you might want to hit up some other beginner articles first.) Framework makers love separating files into different folders—they’re the obsessively tidy folks in the world of code. Simple logic would follow that Rails involves separating files into different folders. This would be true.
But what, exactly, goes where?
A Place for Everything…
So there you are, sitting at your computer, nigh wiggling with excitement. You’re tickled pink about doing your first real Ruby on Rails app. You hit up the command line, and you type
Ack!, you may exclaim. What’s this scrolling by? A list of new directories. OK, that makes sense—Rails is a framework, which translates to “many-many directories” in the ancient language of Devese.
It can be a bit overwhelming, or at least boggling, at first. With so many directories to choose from, how do you know where everything goes? Some things are somewhat obvious if you have any background understanding of MVC, but others are more Rails-specific and require some thought. Since there hasn’t been a simple document yet—that I’ve seen, anyway—to lay all this out for once and for all, I decided to undertake the mission. It’s a dirty job, yeah, but somebody has to do it.
Besides, I kinda like playing in the dirt.
If you haven’t yet, you should definitely read my article on MVC, the design pattern that inspired Rails. Shoo! Go on! I’ll wait.
We’re going to check out the
app/ directory first. Yours should look something like this:
app/ controllers/ helpers/ models/ views/
APIs are something new to Rails 0.10, which introduced ActionWebService. If you’re writing a Rails app that relies on an API to interface with another service somewhere, or if your Rails app can be used via an API, then the files with the relevant code go in
Controllers are what makes up the meat of your program. Without your controller, you have nothing, because the controller is what makes it all happen. And controllers are all located in
Models are code representations of database tables—you give it a name, and tell Rails how it relates to other database tables. Model files go in
Once you get past the nitty gritty code that actually does stuff, then you’ll want to put a pretty face on it. You’ll notice I left out a couple directories from the last section—that’s because they go here.
Layouts are the Rails equivalent of including a header and footer file for various pages. You have a base layout file for every controller you create using
And it will apply for all of the pages displayed using the Books controller; it will call the appropriate individual view for each method that ends up needing a display. If you create a controller called Books, then you’ll find
Individual view files are used for each method in the controller which requires display. You’ll probably have a method called edit, which requires a view because it generates the form to do the necessary editing. If you generated a scaffold for your Books controller instead of generating the controllers and models by hand, you’ll find
app/views/books/. They appear within the layout described in the last paragraph.
Helpers are little methods you can create to save time and repetition when doing HTML in view files. For example, there are several helpers built in to Rails to generate form elements with the correct name attributes and so on. You might want to create a helper to create the login/logout links across your site, for example. You’d put that code in
app/helpers/application_helper.rb if you want it to be available to all controllers, or
app/helpers/books_helper.rb if you want it to be accessible just to the Books controller I keep yammering on about.
apps/ and enter
Hooray for CSS. Your stylesheets go in
public/stylesheets/. Linking to them is as easy as (no
I guess you’re seeing the pattern at this point—
public/images/. ‘Nuff said.
404 and 500 error pages are in the root of
public/. Customize ‘em. You know you want to.