Intro to Ruby on Rails

37
Ruby on Rails June 17, 2011 Elpizo Choi

description

Brief introduction to basics of Ruby on Rails.

Transcript of Intro to Ruby on Rails

Page 1: Intro to Ruby on Rails

Ruby on RailsJune 17, 2011

Elpizo Choi

Page 2: Intro to Ruby on Rails

What is Ruby on Rails?

• MVC Framework for building web apps in Ruby• Uses Ruby classes to abstract out the various

components of an app• Convention over configuration

Page 3: Intro to Ruby on Rails

Main Components

• ActiveRecord (ORM – think hibernate)

• ActionController (business logic, routing)

• ActionView (template parser)

• Migrations, rake tasks

Page 4: Intro to Ruby on Rails

Creating a Rails app

Page 5: Intro to Ruby on Rails

Basic folder structure

apps foldercontrollers, models, views – where you’ll spend most your time

database.ymlConfigures database

GemfileConfigures external libraries used in app

Page 6: Intro to Ruby on Rails

… a little configuration

Page 7: Intro to Ruby on Rails

…a little configuration

Page 8: Intro to Ruby on Rails

Let’s make a model

Page 9: Intro to Ruby on Rails

ActiveRecord::Base

• When inherited, maps to table that’s plural of class name– Team -> teams

• Basic methods– Team.create()– Team.find()– Team.where().limit().sort().skip().first– Team.where().limit().sort().skip().all

Page 10: Intro to Ruby on Rails

…first we need to create table

Page 11: Intro to Ruby on Rails

…first we need to create table

Page 12: Intro to Ruby on Rails

…first we need to create table

Rake

• Ruby build tool• rake –T to see all available tasks• rake db:migrate will run all database migrations that haven’t been run• Stores migrations that have already been run

Page 13: Intro to Ruby on Rails

Create a new Team

Page 14: Intro to Ruby on Rails

Querying

More in-depth tutorial:

http://guides.rubyonrails.org/active_record_querying.html

Page 15: Intro to Ruby on Rails

The “web” part of web-app

Page 16: Intro to Ruby on Rails

Let’s get this running

Page 17: Intro to Ruby on Rails

What happened?

Page 18: Intro to Ruby on Rails

What happened?

Page 19: Intro to Ruby on Rails

What happened?

• config/routes.rb defines how urls are mapped to controllers– get “teams/index” is shorthand for:– match “teams/index” => “teams#index”, :via => :get

• More details: http://guides.rubyonrails.org/routing.html

• Read comments in config/routes.rb

Page 20: Intro to Ruby on Rails

Let’s make some changes

Page 21: Intro to Ruby on Rails

Let’s make some changes

Page 22: Intro to Ruby on Rails

Let’s make some changes

Page 23: Intro to Ruby on Rails

Controller

• Logic to be run every time the mapped route is hit

• All code in the corresponding method will happen as side effects

• Instanced variables set here are available to the template

Page 24: Intro to Ruby on Rails

View

• .erb is E-Ruby

• Add as extension to allow the default Rails template engine to parse Ruby code inside HTML

Page 25: Intro to Ruby on Rails

View• Basic HTML

• <% %> tag: Ruby code to be run, but not shown

• <%= %> tag: Ruby code to be run and shown

• ^^ important: because everything in Ruby returns something (in this case it’ll be @team object from the block)

Page 26: Intro to Ruby on Rails

Creating relationships

Page 27: Intro to Ruby on Rails

Creating relationships

Create the player

Add foreign key

Page 28: Intro to Ruby on Rails

Creating relationships

Page 29: Intro to Ruby on Rails

Creating relationships

Gotta reload console after changing models

Get players

Page 30: Intro to Ruby on Rails

But wait! There’s a shortcut!

has_many basically does the following:• looks in a table with same name as its argument (players) • for records whose foreign key column corresponds to the

current class (team_id)• and where the value is the current instance id (1)

Page 31: Intro to Ruby on Rails

Likewise…You can either do this…. …or this:

It’ll accomplish the same thing:

Page 32: Intro to Ruby on Rails

We can now change our view

Page 33: Intro to Ruby on Rails

We can now change our view

Page 34: Intro to Ruby on Rails

More associations

• ActiveRecord also has shortcuts for:– has_and_belongs_to (join tables)– Polymorphic associations (belongs_to different

objects)

• http://guides.rubyonrails.org/association_basics.html

Page 35: Intro to Ruby on Rails

Congratulations!You’ve made your first Rails app

Page 36: Intro to Ruby on Rails

Additional ResourcesSource code for example_app:https://github.com/fuJiin/example_app

See it live:http://cg-example-app.heroku.com/teams/ index

Documentation and light tutorials:http://guides.rubyonrails.org/

More in depth:http://ruby.railstutorial.org/ruby-on-rails-tutorial- book

Page 37: Intro to Ruby on Rails

Questions?