your first rails app - 2
TRANSCRIPT
YOUR FIRST RAILS APP Environments, MVC, Scaffold
Sunday, November 13, 11
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
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
RUNNING YOUR APP
cd rosterbundle
rails server
Sunday, November 13, 11
RAILS ENVIRONMENTS
By default, Rails is configured with 3 environments:• development• test• production
Sunday, November 13, 11
RAILS ENVIRONMENTS
Sunday, November 13, 11
RAILS ENVIRONMENTS
Sunday, November 13, 11
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
CONFIG/DATABASE.YML
Sunday, November 13, 11
ADDITIONAL CONFIGURATION
config/environments/• development.rb• production.rb• test.rb
Sunday, November 13, 11
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
MVC
Sunday, November 13, 11
LEARNING WITH SCAFFOLD
rails generate scaffold person first_name:string last_name:string
Sunday, November 13, 11
NAMING CONVENTIONS
Sunday, November 13, 11
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
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
VIEWS
<% @people.each do |person| %><tr> <td><%= person.first_name %></td> <td><%= person.last_name %></td></tr><% end %>
Sunday, November 13, 11
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