From the course: Ruby on Rails 6 Essential Training

Examine the file structure of a Ruby on Rails project - Ruby on Rails Tutorial

From the course: Ruby on Rails 6 Essential Training

Start my 1-month free trial

Examine the file structure of a Ruby on Rails project

- [Instructor] In this movie, we'll learn about the basic file structure of a Ruby on Rails project by looking at the project that we just created in the previous movie. There are a number of different ways that we can open up this project so that we can examine it. I'm going to open in the text editor that we're going to be using. We could do that a few ways as well, we could go to the graphical user interface and drag it on top of the text editor, or something like that. But if we're in the command line and we're already navigated to the root of our Rails project and if we're using the Atom text editor, then we can just atom on the command line, with a space, followed by a period. The period is shorthand for the current directory. So it says Atom text editor should open up the current directory. When I do that, then the Atom text editor opens up and we have two columns. On the left are all the files and folders we're going to be working with and on the right it where we will actually do our coding. Before we look at these more closely, I want to make a couple of useful changes to the Atom text editor. I'm going to go to Atom, Preferences, and under Packages I can search for tree, and it'll pop up for the tree-view package. That's going to determine how those things look over here in that far left column. I'll choose settings and I'm going to change a couple of settings. The first is Sort Folders Before Files. I don't want to have the folders first, I want to have them all in alphabetical order. So I'm going to take that away. I'm also going to tell it to hide any files that my version control system has said to hide or anything that's specified as an ignored name. That'll just take away a little more of the clutter, things that I don't care about seeing. Okay, so now that we've done that, we have a smaller list and it's easier to look at the things we want to see. The main directory we want to notice is this app directory. Let's open that up. Inside the app directory is where most of your Ruby on Rails project code will live. You're going to spend 95% of your time in this directory and its subdirectories. There are three main subdirectories I want to call your attention to. Controllers, models and views. Remember we said Rails uses an MVC framework? Well, those are the three parts of that, the models, the views and the controllers. So we're going to put our code related to each of those parts into those different folders. The other one that will get used quite a bit is helpers. Helpers is a place for writing code to help you with your views. Even though it's just called helpers, you should think of them as being view helpers. Next, notice that we have a folder here called assets that inside has a place for images and stylesheets, and one down here for JavaScript. Now, JavaScript used to be included in the assets directory prior to Rails version six. This is one of the biggest changes in Rails 6.0, is that JavaScript is handled a little differently. So it's been pulled out of the assets directory and been given its own directory inside app. Now some of the other folders, like channels, jobs, and mailers, those are all going to be more advanced features that we're not going to be using yet. But they're already places so that our code can stay well organized. Besides the app directory, we should also take notice of the config directory. This is where we're going to do all of the configuration for our application, including telling it how it should connect to our database. Below the config folder, you'll see we have something called db, that's where Rails will store its database related files. The main thing that's going to go here is something called database migrations, they control the changes that we want to make to the database. If we want to add a table or add columns to the database, that code would go here. And then there's this public directory down here. Public is a little bit of a special folder. Notice that it contains some of the basic kinds of files that we would expect to have any website to have, like a favicon and a robots.txt file as well as some 404 and 500 pages. Anything that goes in the public folder, you should assume the public has access to. Ruby on Rails is designed that way. Anything goes in public folder is going to be public. Now, obviously, a lot more can be public besides what's in the folder because we're going to be displaying code and results from our app. But if you were to just, let's say, drop an image file into the public folder, the public would be able to see it. We skipped over the log folder, that's where you're going to find activity and error logs. The Gemfile up here is also an important one because it's going to specify which RubyGems should be loaded into our project. You can click on that now and get an idea of the different RubyGems that our project is already using. Then there's a number of other folders like bin, lib and vendor, and these are places where you can put other code libraries and command-line programs. For the most part you won't need to use them and I think you'll find that most code libraries are now packaged up as RubyGems and used that way. But we do have places where we can put it if we need to. A lot of the other files and folders that we see, like Rakefile and storage and tmp, those are things that are used by Ruby on Rails to do its job and not something we're going to need to worry about. For the most part, we're going to be focused on the app directory, and every now and then you'll need to come to the config directory, to the Gemfile, or the db directory to make changes to support what you're doing in your code.

Contents