Class 6 Rails dev notes…
The latest in my really, really rambling notes. I’ll be posting more coherent blog posts moving forward on Built In Chicago. Here is all about notes and learning for the time being. Having fun doing it this way too!
—NOTES—
Square brackets can mean that you want to go grab something out of the hash…
When you have a collection of things, go grab a specific thing
hash is key = {:value} based
product = { :name => “iPad 2″, :price => 499, :quantity => 2}
puts product[:name]
to change the value within the hash simply redefine the value
put S strips the colon off of the symbol value prior to printing the string
to target deep into nested hashes, simply use the [:target] approach to drill down to the level and value that you are searching for.
#targeting information within nested hashes
params = { :shipping => :standard, :customer => { :name => “Patrick”, :city => “Chicago”} }
#technique 1
customer_data = params[:customer]
puts customer_data[:city]
#technique2
puts params[:customer][:city]
use string interperlation to put numerous values from the same hash together in an output
What is in a http request
URL + Method + Format
Method has four main possibilities [ get, post, put, delete ] (up to 7 methods actually exist)
The URL does not tell the app what to do… (uniform resource locator… it knows where things are but it does little)
Your web application is no more than the value you provide to your users. But we should shift our thinking away from what pages do I need to what resources do I need to provide.
The method changes… not the URL.
—
Web request > Rails app > Web Response
is actually
Web request ( params [...] ) > Rails app > Web Response
—
Forms in Rails
<%= form_for @station do |f| %>
<%= f.label :name %>
<%= f.text_field :name %>
<%= f.submit %>
<% end %>
the params of this form are @station and :name
rails uses .pluralize and .singularize to try to help the creation of classes, methods and routes
—
use the network tab in chrome to view the method and request of the page that is being served
—
Break No. 1
—
replacing the .new method with .create
Station.create (params [:station])
redirect_to “/stations”
but your more likely to see something like the following
Built using a local variable
station = Station.new(params[:station])
station.save
Editing an existing value
> in controller.rb
def edit
@station = Station.find_by_id(params[:id])
end
you need a put method within routes to subimit the updates back into the database
—
never allow for a link to exist on your server for creation and deletion of data without a rel=”nofollow”… without this, search engines like Google could systematically delete your entire database of information
—
Route naming conventions
get “/stations” => “stations#index”, :as => :stations
method “/URL” => action, :as => name I would like to use
—
Renaming routes in routes.rb
redirect_to “/stations/#{@station.id}”
becomes
redirect_to station_url(@station.id)
but screw this noise…
> replace all of it with…
resources :stations
the long way isn’t wrong… but it’s important to know that the short way isn’t magic
what does resources :products do
URL > verb > action > named route
….
create new app
1: rails new transit_chicago
2: cd transit_chicago
3: rails generate scaffold Station name:string address:string info:string
4: rake db:migrate
5: mate .
6:
… scaffolding is not intended to be the finished product.
Just like when you are building a house with scaffolding, you should not leave up the scaffolding when the house is finished.
….
don’t wait until you know everything to start building your app…
plan on refactoring and rebuilding your app over and over as the course continues.
get comfortable with throwing code away and starting over.
….
Model Validations
Business Rules: things that must happen to keep our model organized and useful (someone can’t sign up with the same email 15 times, etc) — sometimes called Domain Logic
example:
class Movie < ActiveRecord::Base
validates :title, :presence => true
end
rails g model title:string year:interger
within models create a validation statement to keep bad or incomplete data from making it into the database
class ModelName < ActiveRecord::Base
validates
validates
end
—
if you change your code in textmate, you will either need to restart rails console entirely or reload!
—
model objects have a way of telling you why a model is invalid
m.valid?
output=true/false
m.errors
this is a collection of error feedback, like…
m.errors.full_messages
output= [“Title can’t be blank”, “Year can’t be blank”]