Friday, September 12, 2014

The 'app' directory in Ruby on Rails

Stemming from my last post, I just wanted to give a quick overview about what exactly Rails generated when you executed the Rails 'new' command

Start clicking around in all the files and see what they all contain. Make sure you familiarize yourself with all these files because every time you work with a Rails project, this is exactly how its going to look like.

Let's first bring our attention to the app folder and all the sub-directories that it contains.

  • app
    • assets
    • controllers
    • helpers
    • mailers
    • models
    • views

In my head( and probably everyone else) this is the meat of the entire Rails project because it controls all the logic that your rails apps will do. It will contain all the models, all the controllers, and all the views that will be shown to the user. Naturally, you will spend most of your time in this folder. Now lets take a closer look at what the app folder contains.

If you have already clicked on the 'assets' folder you will find that it has even more folders. As you can see, there is space for images, javascript files, and some stylesheets!

Here is an example of an almost finished Rails application that contains stylesheets and javascript files for each controller/view that you will eventually generate.

Now we come to the 'controller' folder that has the logic to 'talk' between the models and the views. Let's say that you have an article 'model' that contains all the characteristics of an article (:title, :body, :author). A 'controller' will have an action to 'Read' all the articles from the model(actually from the database since every model contains a table in the database), store it in a variable and give it to the 'view' to display them on the screen(It's a bit more involved, but this is just the start!)

Here is an example of an almost finished Rails application that contains the controllers. Not every model has to have a controller. Also, remember that controllers are usually very lean( don't have too much logic)!

Next we move onto the helpers, which I've only used sparingly, but it is there to put in some logic for the 'view'. In case you ever have very large chunks of ruby code in the view, just know that you can encase that code in a function and place it in here

Now comes mailers! There are a lot of times when a website needs to send emails automatically. Through the mailers we can set exactly that type of functionality. I'll show you how to do it at some later time.

Now here comes the models, which are a little chunky with the logic, but it's okay, everyone still loves models because they are the basis for what we work with in our application. Now the model is pretty cool because you can think about anything in real life and create a model from it. An Article has (:title, :body, :author), a User has(:email, :name, :password, :Profile), a Profile has (:birthday, :fave_color,...). All models have a table in the database and they can have relations to each other!

Here is an example from an almost finished application

Finally, the last folder in the list is the views. It is precisely what you might think it is, which is the folder that contains all the files of html with embedded ruby code that come together to show a user your site!

Here is an example of something you might eventually see in the one of the view sub-folders

That concludes most of the app folder. I will eventually write about the rest of the files that Rails generated just to clear things up. Also, if you find anything confusing or just downright wrong, please notify me in the comments.

Picking the first Ruby on Rails

I have been away for a while, but I just wanted to share some of the things that I learned in the past month and in what I direction life is taking me at the moment.

Last month my friend needed an app made for a club and she asked me for some help. Immediately, I thought this could be the experience that I needed to get myself out there, so I decided I could help her. Earlier in the summer I was learning some Ruby on Rails and I thought I could make a web app to meet her needs. This will be some quick knowledge for those you that are interested in Ruby on Rails!

Since I am developing on a macbook pro I guess my small expertise on the subject would be on the Mac OS X 10.9 (Mavericks). However, the setup should be different on each system, but in theory the actual Ruby on Rails should be the same! (for my later tutorials)

After writing an entire tutorial on installing Rails, I realized there were tons of tutorials that looked far better crafted for installing rails and I didn't want to repeat what the internet had to offer. Although they do help you install rails here, they want a subscription for teaching you Rails.

Well hopefully you have everything installed... Let's start by looking at what gems Rails offers you!

Fire up terminal and let's get into the Desktop:

cd ~/Desktop

Let's create your first Rails app (make something simple like a blog because that's how I'm learning!):

rails new Blog -d mysql

Make sure you add the mysql database, so that Rails can set that up for you without having to worry about it. You can use whatever database that you want!. (Note: You might have to download mysql with homebrew. If you need help on that just comment!)

You should get a lot of output from Rails that looks a bit like this:

My next post will explain a little bit about what Rails generated and see what you can do with it. If you have any questions about anything up to this point feel free to comment below. I'm doing this to document my progress and help others along the way!