How do forms and views work?

Conceptual

  • 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
    • Request is usually POST if the form is creating something.
    • In the case of, e.g. a search form, a GET is more appropriate.
    • See HTTP Methods: GET vs POST

Rails

  • 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, form_tag and form_with: let’s look at two examples
    • Why is there an Item.new 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()
    • Explain injection attacks

More complicated case

  • Nested resources
  • Routes automatically create a set of helpers
  • resource routes create a complete set of CRUD helpers
  • Consider difference between:
 1# /items, /items/3, /items/3/comments, /items/3/comments/5
 2resources :items do
 3  resources :comments
 4end
 5
 6# /items, /items/5, /comments, /comments/4
 7resources :items
 8resources :comments
 9
  • Different URLS have varied impact
  • Is it meaningful to specify, e.g. /comments/44 ?
  • When you create a new comments, are you required to specify what book it’s in?
  • How is the POST different?
  • 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!

Summary

  • 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