Rails controllers (Fri Oct 6, lecture 14)

Homework due for today

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

  1. RORT: Read Ruby on Rails Tutorial ([RORT]) Chapter 5, “Filling in the layout. Here we see how to create views, and start seeing a lot of HTML and getting into routing at a greater level of detail. As usual I really encourage and ask you to go through the code samples and actually type them in. You will see that it really helps your understanding and will assure your success in this course! Use your resources: the TAs, the class mailing list, stack overflow, google and push through to success! When you are done, go to Latte and answer these warmup questions: * Give me your explanation on what link_to is and does. As specific as you can. * When and why would you use a partial? * What does he mean (in conclusion) by a custom routing rule? * Are there one or two things you are still confused by? If not, what one or two things were the most memorable about your reading? Deliverable: Do the warmup in Latte and submit by 8am on the day of class.
  2. Read: The Rails Doctrine and consider whether it’s a good set of principles, how much you agree with them. We will discuss this in class.
  3. Projects: You should be working or completed steps GIT, DOCUMENTATION, PLANSKEL, SHEMASKEL, BUILDSKEL, STORYSDV, PAPERSDV, SCHEMASDV.
  4. Rails PA 2: Continue working on PA Rails 2

Homework Discussion

Fundamental importance of the URI/URL

  • URI = “Uniform Resource Identifier”
    • Used to be called “Universal Resource Locator” (URL)
    • Because the resources were files on the disk of the server
    • And the URL’s path was really a path!
  • Focus now on the ‘path’ part of the URI
  • e.g. some ugly examples
    • http://roomscheduler.com/reserve/room/3
    • http://bookstore/gonewiththewind/order/3
    • http://nyt.com/latest-updates?year=2014
  • e.g. some good examples
    • http://roomscheduler.com/room/3/request
    • http://bookstore.com/book/412/price
    • http://tourmanager.com/city/boston/tour/tuesday/new
  • There are many ways to structure a url

RESTful style of URI design

  • “Representational State Transfer” (See Original REST Paper)
  • Each URI denotes a specific ‘resource’
    • ‘room 3’ or ‘book 412’ or ‘tuesday tours IN Boston city’
    • It is usually not a file but something generated ‘on the fly’
    • Still the same URI should yield the same resource every time (handwave)
  • Each URI is used in combination with an HTTP “method”
    • Get: retrieve the indicated “resource”
    • Put: update value of the indicated resource
    • Delete: delete the indicated resource
    • Post: Create a new instance of indicated resource
    • (some other, less important ones)

One of the earliest design questions

  • What do the URLs for my app look like?
  • Conceptually, they are “directory structure-like” URLs
  • There is a coherent “syntax” about them, e.g.
    • http://www.myservice.org/discussion/topics/{topic}
    • http://www.myservice.org/discussion/2008/12/10/{topic}
    • http://www.myservice.org/discussion/{year}/{day}/{month}/{topic}
  • Touchpoints, coming soon:
    • rails generate resource
    • rails routes
    • default controllers
    • In other words, you don’t have to follow this form of URL but…
    • You really should follow it because a lot of things fall into place.


REST, Resources and Controllers

  • We’ve had some discussions about designing URLs
  • We talked about using a “directory structure-like” syntax for your URLs.
  • Reference: Rails Guide about Routing

Job of the controller

  • Recall, the URL is parsed by the router, which locates and calls the right controller
  • Breaking it down:
    1. Router parses URL
    2. Determines the controller (error: Can’t find Controller’s ruby file in app/controllers/)
    3. Determines the action insider the controller (error: Can’t find a method for the action)
    4. Calls the action method, with the parameters corresponding to the ‘rest’ of the url
    5. When action completes, a view of the same name is located (error: Can’t find Views html.erb file in the views/<controllername>/ directory)
    6. View’s ‘erb’ is expanded and returned, through controller, router, web server, back to the user.

Resources in Rails

  • Automatic wiring of plumbing between urls, routing, models, and controllers
  • Takes advantage of standardized patterns
    • Nesting model in the URLs
    • CRUD operations as actions
    • Path naming (path_to_user instead of “”)
    • Controller naming
  • Huge time saver and bug saver!

Look at next class