Skip to content

Latest commit

 

History

History
 
 

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 

README.md

W10D01 - Rails Review

To Do

  • "Rails Week" Conversation
  • MVC Review
  • Quickly build simple Rails app
  • Nested Resources
  • Returning JSON

Rails Convo

  • covers majority of application types
  • can build very quickly
  • preferred writing the SQL directly

Model View Controller Review

  • Model: Responsible for handling data logic (eg. database queries)
  • View: Responsible for the UI (User Interface)
  • Controller: Ties the model and view together, talks to both and shares data between them
  • Rails also uses a router (routes.rb) sitting between the user requests and the controllers that respond to those requests

MVC Diagram

Rails Libraries

  • Rails is a framework made up of a collection of libraries
  • Active Record
    • An Object Relational Mapper (ORM)
    • Allows you to query and modify the application data in an intuitive way
  • Action View
    • Handles template lookup and rendering
    • Provides helpers for building forms and other UI elements
  • Action Controller
    • Controller library
    • Controller's make sense of the request and decide what should be returned to the client
  • Action Dispatch
    • The Rails router
    • Handles incoming requests and forwards them to the correct controller
  • Action Cable
    • Websockets for Rails

Nested Routes

  • We define the nesting in our routes. In routes.rb:
resources :authors do
  resources :books
end
  • Will generate:
author_books      GET    /authors/:author_id/books(.:format)          books#index
                  POST   /authors/:author_id/books(.:format)          books#create
new_author_book   GET    /authors/:author_id/books/new(.:format)      books#new
edit_author_book  GET    /authors/:author_id/books/:id/edit(.:format) books#edit
author_book       GET    /authors/:author_id/books/:id(.:format)      books#show
                  PATCH  /authors/:author_id/books/:id(.:format)      books#update
                  PUT    /authors/:author_id/books/:id(.:format)      books#update
                  DELETE /authors/:author_id/books/:id(.:format) 
  • Notice that these routes will be affected by the books Controller. All the logic will go there to work with these nested routes.
  • There are some nice helpful path helpers that get generated by this nested resource stuff. If you look at rake routes, you'll see the prefixes for these under PREFIX. For example, the path /authors/5/books could be generated by calling author_books(@author).

Useful Links

Some notes borrowed from Travis' lecture and Vas' lecture