www.hackyale.com
< Intro to Rails />
Monday, June 10, 13
Building MVCr from scratch
< front end />
Monday, June 10, 13
MVC Recap
Monday, June 10, 13
Monday, June 10, 13
Monday, June 10, 13
hand-crafted,artisanal
rails code
Monday, June 10, 13
router
Monday, June 10, 13
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 )
Monday, June 10, 13
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
Monday, June 10, 13
SAMPLE ROUTE
RedditDemo::Application.routes.draw do match 'users/' => 'users#index' root to: 'users#index'end
Monday, June 10, 13
controller
Monday, June 10, 13
CONTROLLER
The controller orchestrates the request.
It communicates with models to gather and manipulate data.
It then passes that data to the view.
Monday, June 10, 13
CONTROLLER ACTIONS
Controllers have multiple actions.
The standard ‘restful’ actions are:
indexshowcreateneweditupdatedestroy
Monday, June 10, 13
SAMPLE CONTROLLER
class UsersController < ActionController::Base def index @users = User.all endend
Monday, June 10, 13
views
Monday, June 10, 13
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.
Monday, June 10, 13
VIEWS
Views use Embedded RuBy (ERB) to substitute data into the template.
<% ruby.code.no.output %>
<%= equal.sign.outputs.to.template %>
Monday, June 10, 13
SAMPLE VIEW
<% @users.each do |user| %> <%= user.name %><% end %>
Monday, June 10, 13
models
Monday, June 10, 13
MODELS
Models represent the nouns and data in your application.
They make sure stored data is valid, and perform calculations/analysis.
Monday, June 10, 13
DATABASES
•Store our model data (in rows), and define the attributes (columns).
•We manage changes in our database with migrations.
Monday, June 10, 13
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
Monday, June 10, 13
DATABASE
id first_name last_name netid
1
2
3
Adam Bray alb64
Vladimir The Bear vtb39
Charlie The Horse cth44
users
Monday, June 10, 13
DATABASE
id first_name last_name netid
1
2
3
Adam Bray alb64
Vladimir The Bear vtb39
Charlie The Horse cth44
users
Monday, June 10, 13
ADDING ANOTHER COLUMN
rails generate migration AddClassYearToUsers class_year:integer
Monday, June 10, 13
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
Monday, June 10, 13
SAMPLE MODEL
class User < ActiveRecord::Baseend
Monday, June 10, 13
group activity:add show page
for users
Monday, June 10, 13
YOU’LL NEED TO:
•create a route
•add a controller action
•add a view template
Monday, June 10, 13
TIPS
params hash:if you put symbols in your route they are available in controller
e.g. ‘users/:id’ gives you ‘params[:id]’ in controller
retrieving modelsuser.find(num) returns user with id == num
use link_to method to link from index
Monday, June 10, 13
group activity:add new user
form
Monday, June 10, 13
YOU’LL NEED TO
•add two new routes• use ‘resources’ route instead
•add two new actions, new, and create• new generates a form
• create takes form data and uses it to create user
Monday, June 10, 13
FORMS
<%= form_for(@user) do |f| %> <div class="field"> <%= f.label :first_name %><br /> <%= f.text_field :first_name %> </div> <div class="actions"> <%= f.submit %> </div><% end %>
Monday, June 10, 13