1. GET
need to get route
do erb
write the form
Would go in views file: – save in komodo, file, save as other, views, save as form.erb
form.erb
This is a form
once you save it as a form, the app.rb will get it from get/ from erb :form
Create the action in the form
<html>
<head>
<title>This is a form </title>
<form action=”/form”> – what you coudl usually do, but on the ITP server you ahve to put an actual url there which would e http://….~acb551 blah blah
in the form, we need to name it something, and also a method, i.e. method=”POST”
<form action=”http://http://itp.nyu.edu/~acb551/ITP/html_form” method = “POST”>
<input type = “text” name=”firstname” />
/html,head.title
2. POST
take steps before with get request and do a post route instead
- route
-erb
change get to post and /save (for example), and change the erb
must call variables with the @ symbol – using the @ symbol creates a global variable
then we have to call the @variable in other file
<%= @whatevervariablelikefirstname %> – call it like this in other form
THIS IS THE MOST IMPORTANT
THING YOU WILL
LEARN
WHAT DO I NEED TO CREATE
I NEED TO DO A GET ROUTE
I NEED TO DO A ERB ROUTE
THE USER PUTS STUFF IN HERE
NOTHING OF THIS IS OBVIOUS
SPREAD OUT THE INTENSE
WORK YOU ARE DOING
TO FIFTEEN MINUTES
A DAY KEEP GOING BACK
TO IT
////////////////////////////////////
INTRODUCING DATABASES
#save this firstname with all the other firstnames
#get all the firstnames in a global variable
erb :save
DataMapper::setup(:default, {:adapter => ‘yaml’, :path => ‘db’})
class is kind of a wrapper for multiple properties/variables
datamapper works in the same way as classes, but it allows us to create a new class, all of the object/variables and save it in a file. instead of saving simple strings we are saving a collecton of things.
ex.
Class person ()
- firstname
- lastname
- age
Doing a class in Ruby is simpler than in Processing. Write class, name of class
class Person
end
important that it’s behind DataMapper.finalize
then copy/paste two lines
include DataMapper ::Resource
property :id, Serial
property :firstname, String
property id is something related to the way databases work. every time you save it will get a specific id, id1, id2, etc. imp if two people are named the same thing e.g.
now you can only save a first name, but as our application grows you can save more things
post “/save” do
#a way to create a new class
@p = Person.new
@p.firstname = params[:firstname]
@p.save#get all the people in a global variable
@people = Person.all – creates a new variable called people that will put all the people form the database into an array//will take whatever you put into p and save into the database
but there’s the ruby file (app.rb) and the html files (.erb) files
% starts ruby code
in save.erb
<% for p in @people %>
<%= p.firstname %>
<% end %>
</body>
HOMEWORK
start with an empty sinatra sketch, take the steps needed to recreate what we did in class
all of the example will be on github but don’t copy it