Top Banner
rails basics Tuesday, May 28, 13
30
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Intro to rails

rails basics

Tuesday, May 28, 13

Page 2: Intro to rails

how do browsers work?

Tuesday, May 28, 13

Page 3: Intro to rails

what does rails do?

Tuesday, May 28, 13

Page 4: Intro to rails

MODELS, Views, Controllers

rMVC

(and routes)

Tuesday, May 28, 13

Page 5: Intro to rails

router

Tuesday, May 28, 13

Page 6: Intro to rails

Tuesday, May 28, 13

Page 7: Intro to rails

Tuesday, May 28, 13

Page 8: Intro to rails

ROUTER

the router looks at the incoming request and sends it to the right controller.

Example:

myapp.com/users/1/edit

goes to the users controller, edit action(and sends a ‘parameter’ for user_id=1 as )

Tuesday, May 28, 13

Page 9: Intro to rails

ROUTER

/users users#index list all users

/users/1 users#show show user with id 1

/users/new users#new make a new user

/users/1/edit users#edit edit user with id 1

Tuesday, May 28, 13

Page 10: Intro to rails

SAMPLE ROUTE

RedditDemo::Application.routes.draw  do    match  'users/'  =>  'users#index'    root  to:  'users#index'end

Tuesday, May 28, 13

Page 11: Intro to rails

controller

Tuesday, May 28, 13

Page 12: Intro to rails

Tuesday, May 28, 13

Page 13: Intro to rails

CONTROLLER

The controller orchestrates the request.

It communicates with models to gather and manipulate data.

It then passes that data to the view.

Tuesday, May 28, 13

Page 14: Intro to rails

CONTROLLER ACTIONS

Controllers have multiple actions.

The standard ‘restful’ actions are:

indexshowcreateneweditupdatedestroy

Tuesday, May 28, 13

Page 15: Intro to rails

SAMPLE CONTROLLER

class  UsersController  <  ActionController::Base    def  index        @users  =  User.all    endend

Tuesday, May 28, 13

Page 16: Intro to rails

models

Tuesday, May 28, 13

Page 17: Intro to rails

Tuesday, May 28, 13

Page 18: Intro to rails

MODELS

Models represent the nouns and data in your application.

They make sure stored data is valid, and perform calculations/analysis.

Tuesday, May 28, 13

Page 19: Intro to rails

DATABASES

•Store our model data (in rows), and define the attributes (columns).

•We manage changes in our database with migrations.

Tuesday, May 28, 13

Page 20: Intro to rails

MIGRATION

>> rails generate migration CreateUsers

class CreateUsers < ActiveRecord::Migration

def change

create_table :users do |t|

t.string :first_name

t.string :last_name

t.string :netid

t.timestamps

end

end

end

Tuesday, May 28, 13

Page 21: Intro to rails

DATABASE

id first_name last_name netid

1

2

3

Adam Bray alb64

Vladimir The Bear vtb39

Charlie The Horse cth44

users

Tuesday, May 28, 13

Page 22: Intro to rails

DATABASE

id first_name last_name netid

1

2

3

Adam Bray alb64

Vladimir The Bear vtb39

Charlie The Horse cth44

users

Tuesday, May 28, 13

Page 23: Intro to rails

ADDING ANOTHER COLUMN

rails generate migration AddClassYearToUsers class_year:integer

Tuesday, May 28, 13

Page 24: Intro to rails

DATABASE

id first_name last_name netid class_year

1

2

3

Adam Bray alb64 1995

Vladimir The Bear vtb39 2015

Charlie The Horse cth44 2016

users

Tuesday, May 28, 13

Page 25: Intro to rails

SAMPLE MODEL

class  User  <  ActiveRecord::Baseend

Tuesday, May 28, 13

Page 26: Intro to rails

views

Tuesday, May 28, 13

Page 27: Intro to rails

Tuesday, May 28, 13

Page 28: Intro to rails

VIEWS

Views are templates for how your web pages look.

Variables created in the respective controller are used to insert relevant information into the template.

Tuesday, May 28, 13

Page 29: Intro to rails

VIEWS

Views use Embedded RuBy (ERB) to substitute data into the template.

<% #ruby here is run, but not output %>

<%= #ruby here is run and output %>

Tuesday, May 28, 13

Page 30: Intro to rails

SAMPLE VIEW

<%  @users.each  do  |user|  %>    <%=  user.name  %><%  end  %>

Tuesday, May 28, 13