your first rails app - 2

18
YOUR FIRST RAILS APP Environments, MVC, Scaffold Sunday, November 13, 11

Upload: blazing-cloud

Post on 20-Aug-2015

769 views

Category:

Technology


1 download

TRANSCRIPT

Page 1: Your first rails app - 2

YOUR FIRST RAILS APP Environments, MVC, Scaffold

Sunday, November 13, 11

Page 2: Your first rails app - 2

CREATING YOUR FIRST RAILS APP

rails new roster

This command generates code fora new Rails web application 

in a sub-directory called “roster”  

Sunday, November 13, 11

Page 3: Your first rails app - 2

THE APPLICATION DIRECTORY

•The whole rails app is in this one directory• No hidden configuration files in system directories• You will modify many of these files in the course of your development• We're using sqlite so even the database is in this directory, but usually the

database is the only part of your application that lives somewhere else

•You can simple copy this directory to server to deploy the app•You can delete the directory and its contentsif you want to throw it away and start over

Sunday, November 13, 11

Page 4: Your first rails app - 2

RUNNING YOUR APP

cd rosterbundle

rails server 

Sunday, November 13, 11

Page 5: Your first rails app - 2

RAILS ENVIRONMENTS

By default, Rails is configured with 3 environments:• development• test• production

Sunday, November 13, 11

Page 6: Your first rails app - 2

RAILS ENVIRONMENTS

Sunday, November 13, 11

Page 7: Your first rails app - 2

RAILS ENVIRONMENTS

Sunday, November 13, 11

Page 8: Your first rails app - 2

RAILS ENVIRONMENTSThe environment can be specified with RAILS_ENV as an environment variable "development" by default.

In your code, you refer to Rails.env (or RAILS_ENV in Rails 2)

Sunday, November 13, 11

Page 9: Your first rails app - 2

CONFIG/DATABASE.YML

Sunday, November 13, 11

Page 10: Your first rails app - 2

ADDITIONAL CONFIGURATION

config/environments/• development.rb• production.rb• test.rb

Sunday, November 13, 11

Page 11: Your first rails app - 2

MODIFYING THE HOME PAGE

public/index.html

All files in the public directoryare static content.

Rails checks this directory before executing any dynamic code.

Sunday, November 13, 11

Page 12: Your first rails app - 2

MVC

Sunday, November 13, 11

Page 13: Your first rails app - 2

LEARNING WITH SCAFFOLD

rails generate scaffold person first_name:string last_name:string

Sunday, November 13, 11

Page 14: Your first rails app - 2

NAMING CONVENTIONS

Sunday, November 13, 11

Page 15: Your first rails app - 2

SCAFFOLDModel

app/models/person.rbdb/migrate/20090611073227_create_people.rb

5 viewsapp/views/people/index.html.erbapp/views/people/show.html.erbapp/views/people/new.html.erbapp/views/people/edit.html.erbapp/views/people/_form.html.erb

Controllerapp/controllers/people_controller.rbroute.rb: resources :people

Sunday, November 13, 11

Page 16: Your first rails app - 2

MVCModel: ActiveRecord•Represents what is in the database

View: ActionView, erb•Model rendered as HTML

Controller: ActionController•Receives HTTP actions (GET, POST, PUT, DELETE)•Decides what to do, typically rendering a view

Sunday, November 13, 11

Page 17: Your first rails app - 2

VIEWS

<% @people.each do |person| %><tr> <td><%= person.first_name %></td> <td><%= person.last_name %></td></tr><% end %>

Sunday, November 13, 11

Page 18: Your first rails app - 2

VIEW EXERCISE

On the main people page  a. Change “Listing people” to “My Class List”  b. List people with first initial and last name in one visual

column (e.g. W. Flintstone) 

Sunday, November 13, 11