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:
- Important configuration files
- How data is stored
- How web pages are created
quick review
- Rails is all about "convention over configuration"
- Model-View-Controller
- 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)
- A file is generated under db/migrate that creates a table in your database (think Excel) for you when you run "rake db:migrate"
- 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
end
end
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
- dorms_path
- new_dorm_path
- post_path(dorm)
- edit_dorm_path(dorm)
Database, Route config
Magic: This Rails developer is using different databases for
thanks for coming
rails magic
By Don Yu
rails magic
- 1,075