Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@lengarvey
Created April 8, 2014 20:20
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lengarvey/10185222 to your computer and use it in GitHub Desktop.
Save lengarvey/10185222 to your computer and use it in GitHub Desktop.
Intro to Web Applications and Rails

So you want to build a web site?

Here's how the web works:

  1. A user types a web address (or clicks a link) into their browser.
  2. The browser asks a server for the page corresponding to that web address.
  3. The server responds with data, generally text encoded as HTML.
  4. The browser displays this HTML to the user.

To build a web application we don't need to worry about the browser (someone else has built that) so we just need to concern ourselves with instructing the server what to respond with. All of Ruby on Rails is concerned with step 3.

Let's explore it further.

Building a web application

The role of a web application is to listen for requests from browsers, and then to respond to those requests. Additionally, it's common for a web application to store information and retrieve it later. This means that the web application needs to be able to store things in a database.

This means a web application might have three simple responsibilities:

  1. Listening for incoming requests.
  2. Storing or retrieving information from a database depending on the contents of the request.
  3. Responding to request with HTML that the browser can display to the end-user.

There are other things a web-application might want to do (such as sending emails) but we'll focus on these three simple responsibilities.

Using Rails to build a web application

In software development, like in life, it's a good practice to keep your code as clean and as organised as you can. Because of this we use different sections of our code to handle the 3 different responsibilities outlined above.

I'm going to assume that you've already created a Rails application from here on. If you haven't you can check out my tutorial at: http://railsinstallfest.org/guides/installfest/getting_started/

Listening with the Router

A request is broken down into two parts, the address (which is referred to as the URL) and the data. In Rails we have the Router which is the block of code that takes the address and and sends it to the proper Controller.

The Router is located in config/routes.rb.

The second part of the request, the data, is handled by the Controller. The Router determines which controller is used, and which section of that controller is invoked. This section is the method, or action.

Your controllers are located in app/controllers.

Managing the Request with the Controller

Because the entire purpose of Rails is to respond to a web request, we're going to use something which manages the request itself. The Controller is like an overseer, or foreman in a construction site. It's the Controller that ensures the request is responded to, the Controller makes sure that data is gathered from the database, and that the HTML is created.

Storing and Retrieving data with Models

The next component in our Rails application is our data. In our code we represent this data with a Model. If you have a statistics or mathematics background, you might say that we model the data with our Models.

Your models are located in app/models.

Your models provide three pieces of functionality to our web application:

  1. Retrieving data from the database. We do this by interacting with the name (or class) of the Model. So if you have a Post model, you can retrieve all the posts with the code Post.all.
  2. Storing data in the database. We do this also by interacting with the name (or class) of the appropriate model. If you have an Idea model you can create a new idea and store it in the database by running the code Idea.create(idea_params). In this case the idea_params might be some data gathered from the request by your IdeasController.
  3. Interacting with a single idea.

Point 3 is worth a little further explanation. Imagine that your database is made up of rows and columns, just like an Excel spreadsheet:

Posts Table:

IdTitleBody
1Building a Rails AppLorem....
2What I did todayLorem....
3Sunshine in Calgary!Lorem....

Your Post model (or class) represents the entire table, but an individual post "instance" represents a single row of that table. This instance can be interacted with so, if we retrieve the post that has an Id of 2:

post = Post.find(2)

we can interact with that post and retrieve the body or the title from it:

post.body
post.title

Remember above how we could retrieve ALL the posts from the database with Post.all? This gives us a list of our post objects:

posts = Posts.all
first_post = posts.first
first_post.body

Would let us retrieve the first_post and it's body value. There are many more things we can do with models, but this is enough to get us started.

The last part of our simple web application is actually responding to the request with HTML.

Creating HTML with Views

To be completed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment