Intro to rails
-
Upload
adam-bray -
Category
Technology
-
view
151 -
download
0
Transcript of Intro to rails
![Page 1: Intro to rails](https://reader033.fdocuments.in/reader033/viewer/2022052823/5554789cb4c90548358b47d8/html5/thumbnails/1.jpg)
rails basics
Tuesday, May 28, 13
![Page 2: Intro to rails](https://reader033.fdocuments.in/reader033/viewer/2022052823/5554789cb4c90548358b47d8/html5/thumbnails/2.jpg)
how do browsers work?
Tuesday, May 28, 13
![Page 3: Intro to rails](https://reader033.fdocuments.in/reader033/viewer/2022052823/5554789cb4c90548358b47d8/html5/thumbnails/3.jpg)
what does rails do?
Tuesday, May 28, 13
![Page 4: Intro to rails](https://reader033.fdocuments.in/reader033/viewer/2022052823/5554789cb4c90548358b47d8/html5/thumbnails/4.jpg)
MODELS, Views, Controllers
rMVC
(and routes)
Tuesday, May 28, 13
![Page 5: Intro to rails](https://reader033.fdocuments.in/reader033/viewer/2022052823/5554789cb4c90548358b47d8/html5/thumbnails/5.jpg)
router
Tuesday, May 28, 13
![Page 6: Intro to rails](https://reader033.fdocuments.in/reader033/viewer/2022052823/5554789cb4c90548358b47d8/html5/thumbnails/6.jpg)
Tuesday, May 28, 13
![Page 7: Intro to rails](https://reader033.fdocuments.in/reader033/viewer/2022052823/5554789cb4c90548358b47d8/html5/thumbnails/7.jpg)
Tuesday, May 28, 13
![Page 8: Intro to rails](https://reader033.fdocuments.in/reader033/viewer/2022052823/5554789cb4c90548358b47d8/html5/thumbnails/8.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022052823/5554789cb4c90548358b47d8/html5/thumbnails/9.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022052823/5554789cb4c90548358b47d8/html5/thumbnails/10.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022052823/5554789cb4c90548358b47d8/html5/thumbnails/11.jpg)
controller
Tuesday, May 28, 13
![Page 12: Intro to rails](https://reader033.fdocuments.in/reader033/viewer/2022052823/5554789cb4c90548358b47d8/html5/thumbnails/12.jpg)
Tuesday, May 28, 13
![Page 13: Intro to rails](https://reader033.fdocuments.in/reader033/viewer/2022052823/5554789cb4c90548358b47d8/html5/thumbnails/13.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022052823/5554789cb4c90548358b47d8/html5/thumbnails/14.jpg)
CONTROLLER ACTIONS
Controllers have multiple actions.
The standard ‘restful’ actions are:
indexshowcreateneweditupdatedestroy
Tuesday, May 28, 13
![Page 15: Intro to rails](https://reader033.fdocuments.in/reader033/viewer/2022052823/5554789cb4c90548358b47d8/html5/thumbnails/15.jpg)
SAMPLE CONTROLLER
class UsersController < ActionController::Base def index @users = User.all endend
Tuesday, May 28, 13
![Page 16: Intro to rails](https://reader033.fdocuments.in/reader033/viewer/2022052823/5554789cb4c90548358b47d8/html5/thumbnails/16.jpg)
models
Tuesday, May 28, 13
![Page 17: Intro to rails](https://reader033.fdocuments.in/reader033/viewer/2022052823/5554789cb4c90548358b47d8/html5/thumbnails/17.jpg)
Tuesday, May 28, 13
![Page 18: Intro to rails](https://reader033.fdocuments.in/reader033/viewer/2022052823/5554789cb4c90548358b47d8/html5/thumbnails/18.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022052823/5554789cb4c90548358b47d8/html5/thumbnails/19.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022052823/5554789cb4c90548358b47d8/html5/thumbnails/20.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022052823/5554789cb4c90548358b47d8/html5/thumbnails/21.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022052823/5554789cb4c90548358b47d8/html5/thumbnails/22.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022052823/5554789cb4c90548358b47d8/html5/thumbnails/23.jpg)
ADDING ANOTHER COLUMN
rails generate migration AddClassYearToUsers class_year:integer
Tuesday, May 28, 13
![Page 24: Intro to rails](https://reader033.fdocuments.in/reader033/viewer/2022052823/5554789cb4c90548358b47d8/html5/thumbnails/24.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022052823/5554789cb4c90548358b47d8/html5/thumbnails/25.jpg)
SAMPLE MODEL
class User < ActiveRecord::Baseend
Tuesday, May 28, 13
![Page 26: Intro to rails](https://reader033.fdocuments.in/reader033/viewer/2022052823/5554789cb4c90548358b47d8/html5/thumbnails/26.jpg)
views
Tuesday, May 28, 13
![Page 27: Intro to rails](https://reader033.fdocuments.in/reader033/viewer/2022052823/5554789cb4c90548358b47d8/html5/thumbnails/27.jpg)
Tuesday, May 28, 13
![Page 28: Intro to rails](https://reader033.fdocuments.in/reader033/viewer/2022052823/5554789cb4c90548358b47d8/html5/thumbnails/28.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022052823/5554789cb4c90548358b47d8/html5/thumbnails/29.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022052823/5554789cb4c90548358b47d8/html5/thumbnails/30.jpg)
SAMPLE VIEW
<% @users.each do |user| %> <%= user.name %><% end %>
Tuesday, May 28, 13