2012-01-26_08.08.55 notes
Web requests: URL, method, format
In the routes.rb file, you can have the same URL, but different methods
If you are having trouble naming controllers, go into the Rails console, call the “pluarlize” or “singularize” methods. This will give you the Rails conventions.
In browsers, when you type something in the address bar it will automatically be an HTTP get request
Hashes
product = { :name => "iPad2", :price => 499, :quantity => 2 } the_name = product[:name] the_price = product[:price] # change the name product[:name] = "iPad3" # you could have a hash inside a hash params = {:shipping => :standard, :customer => {:name => "Patrick", :city => "Chicago" } } # this will print out "standard" since we never put anything in that symbol puts "Shipping: #{params[:standard]}" customer_data = params[:customer] puts "Name: #{customer_data[:name]}" puts "City: #{cutomer_data[:city]}" # or get a hash within a has puts "City: #{params[:customer][:city]}"
Rails forms will have hashes and sub-hashes
3 parts to HTTP request: URL, method and format
The R is resources
Stop thinking pages, start thinking resources
1. web request
params hash contains the data
2. Rails App
3. Web response
Forms in Rails
use the form_for method.
<%= form_for @station do |f| %> <%= f.text_field :name %>
So you should see params[:station] with :station as a hash in the hash, and containing a key called :name
In the train station app, we could also do
Station.create params[:station]
How to change things in the database?
We use the HTTP method is PUT
We have to name our URLs in the routes.rb file
rake routes
The form knows to label button “Update Record”
In the rails console, you can call x.new_record?
Rails can support HTTP put method with a “_method” param with a value of “put”
To list methods well:
y Station.method.sort
y puts it in yaml
Cheating:
rails generate scaffold blog
TO get rid of it:
rails destroy scaffold blog
New is paired up with create, edit paired up with update conceptually, so pair them in your routes.rb file
In the update method of the controller, we could call @station.update_attributes(params[:station])
It is best to use named routes
If there is a URL with multiple HTTP methods, you only need to name them once
In the controllers, you use
redirect_to $ROUTE_NAME_url
If the path is stations:
redirect_to stations_url
But what if you need the ID?
redirect_to station_url(@station.id)
You could also do station_path
that will do the relative path
path gives you less data to the client
Only good if you will be in the same domain
Problem if you have a redirect
If you send an ActiveRecord object to a route in a link_to call, you could just send it @the_station instead of @the_station.id
The ID is the only one you could leave off. Everything else must be specified.
We will have a lot of controllers in a web app.
You will be creating, deleting, updating a lot of stuff
You could just do
resources :stations
There will be a cheat sheet for the routes
A lot of people never learn the long way like we did, they just learn the resources :model
Even Hartl does not go over that, which kind of surprises me.
Scaffold:
singular for model, plural for controllers, singular for scaffold
It will make a lot of stuff for you
rails generate scaffold station name:string address:string info:string
It creates something for a new station, as well as some edit, destroy, etc
rails generate scaffold Station name:string address:string info:string
and
rake db:migrate
afterwards
Scaffolding is not intended to be the final application
We will see that scaffolding will not do too much, especially when we have multiple models
Model validations: you will have rules about your models, aka business rules, or domain logic
don’t let a user sign up multiple times with same address, is credit card valid, etc, etc
These rules go in the model
class Movie < ActiveRecord::Base # this will make sure we have a title validates :title, :presence => true end
If you change model while in the rails console, you either need to exit or call reload!
Many of the variables will no longer be valid
You could call m.valid? and m.errors (which gives a hash)
or call: m.errors.full_messages
That returns an array
The scaffolding will create error handling as well
Image from Wikimedia, assumed allowed under Fair Use. Image from the Vatican Virgil, a 5th century manuscript of poems by Virgil.