ruby on rails

behind the magic

objective

Learning and building Ruby on Rails apps will be easier if you understand the magic of how everything fits and works:
  1. Important configuration files
  2. How data is stored
  3. How web pages are created


quick review

  1. Rails is all about "convention over configuration"
  2. Model-View-Controller
  3. Creating a new rails application with "rails new <name>"

gemfile

Magic: where is rails getting all the extra functionality like sending emails, connecting to facebook, or using a database
Behind the scenes: Rails apps install all the Ruby libraries that you need through a Gemfile. List a library in the file and then type "bundle install" from the Terminal  and all the libraries will be downloaded and installed for you

rails resources

These refer to collections of data that represent the same thing i.e. a Users resource, a Tweets resource,  a Songs resource

Magic: If I run "rails generate resource dorm name:string location:string" I supposedly have a complete resource.

 $ rails generate resource dorms name:string location:string

rails resources (behind the scenes)

  1. A file is generated under db/migrate that creates a table in your database (think Excel) for you when you run "rake db:migrate"
  2. A controller is generated for you under app/controllers and a model is generated for you under app/models

Let's add functionality to the Controller and the Model

rails controllers

Magic: How does the controller magically know where to get the data and which to pass it to?

Behind the Scenes: You implement specific methods within the controller and within those methods, you get the data you need and pass it into a view (this is automatic)

 class DormsController < ApplicationController   respond_to :html     def index     @dorms = Dorm.all     respond_with @dorms   endend

rails views

Generating the views isn't magic, it's convention

If user requests www.dormbnb.com/dorms.html, then the corresponding view will be index.html.erb within the app/views/dorms/ directory

This is all seen within the routes.rb file

rails route magic

Every view can make links to these other views
  1. dorms_path
  2. new_dorm_path
  3. post_path(dorm)
  4. edit_dorm_path(dorm)

Database, Route config

Magic: This Rails developer is using different databases for 

thanks for coming

Made with Slides.com