Forms and Views (Fri Oct 27, lecture 23)

Homework due for today

Legend: : Participation | : Early | : PDF | : Portfolio | : Zipped

  1. Teams: Continue working on your projects

Text Based formats / languages

  • There are many
    • XML - granddaddy of them all
    • HTML (HAML, Markdown)
    • JSON (YAML)
    • ERB
  • They are all like little programming languages
    • Syntax checking and syntax errors
    • Versions (HTML5, )
    • Given to ‘interpreter’ or ‘compiler’ to do something

Lightning Reviews


  • You need a form whenever the browser needs to send data to the server
  • <form> tag begins the group of fields. It declares the URL that payload is sent to
    • There are zero or more fields
    • There’s always a “submit” link or button that triggers the sending
    • </form> terminates the group
  • Sending always is done via some kind of HTTP request
    • Payload is always name=value pairs, value is always text over the wire


  • There are always TWO controller actions:
    • one to display the form
    • one to accept the data from the form
    • Where can I find them for items?
    • Two controller actions means TWO urls!
  • Display the form
    • Let’s look at what the HTML should look like
    • Difference between form_for and form_tag: let’s look at two examples
    • Why is there an in the form display action (new)?
  • Accept data from the form
    • What URL is requested to send the data? What http verb? Why?
    • How does the data come back from the form?
    • What can the controller do with that data?
  • Security issues
    • What if the form payload is not coming from your form?
    • What if your form has been modified inside the browser?
    • Explain params.require()
More complicated case
  • Nested resources
    • A comment always belongs to an item
    • What should the URL look like to display the form to create a comment (THINK!) What is the verb?
    • Does the form display to add a comment work differently?
      • Using form_for vs. form_tag in this situation
    • What would the URL look like to accept the data of the new comment?
      • Let’s read the action and understand what it does
    • How do you create a nested resource, i.e. a comment for item 3?
Making Views DRY
  • When you see yourself writing the same html over and over again
    • write a helper: let’s do an example!
    • write a partial: And let’s do another example!


  • Routes
    • Understand the urls and verbs and how they map to controller actions
    • Use rake routes to verify your understanding
    • Make changes to routes.rb to get the urls and verbs you intend
  • Forms
    • Two actions, one to display the form, one to send the payload to the server
    • form_for and form_tag are equally good. In real cases you end up with form_tag often
    • Understand params and params.require
    • Forms for nested resources
  • Other tools
    • helpers to generate repetitive html
    • parials to build repetitive html
    • rake db:migrate:reset and rake db:seeds

Let me demonstrate…

Look at next class