Skip to content

Latest commit

 

History

History
44 lines (26 loc) · 2.53 KB

routes_controllers_rails.markdown

File metadata and controls

44 lines (26 loc) · 2.53 KB

Routes and Controllers in Rails

Today we talked about

  • the routes.rb file in Rails
  • the purpose of a controller in Rails
  • how to use resources :things to create eight RESTful routes

Your Tasks

  1. Read the Rails Routing documentation. It's a pretty easy read!

  2. Create a new rails project called routes-practice.

  3. Add a route to your config/routes.rb file that maps a GET request to /students with the students_controller's index method.

  4. Add the corresponding controller and method within that controller that renders the text: "You've hit the students index page."

  5. Modify your routes file to add all eight routes for a Course resource. Remember - you don't need to do this manualy, Rails has a shortcut for you, but for now we'd like for you to write it manually and also explore the shortcut.

  6. Add a controller and all methods that the controller will need in order to use the routes generated by this. Don't worry about rendering text. Just make sure you have all the (empty) methods defined.

  7. Modify your routes file to add all eight routes for a Teacher resource.

  8. Can you add code to the end of the resources :teachers line in order to only create the show and index routes instead of all eight routes generated by resources?

  9. Add a controller for teachers and ensure that the show and index actions work.

  10. Add a root path so that we can navigate to http://localhost:3000/ and not get an error. Have this route go to the index action in a welcome controller. Create the corresponding controller and action, and test out the root by starting your server and navigating to '/'.

  11. With resources :teachers you typically access a single teacher with a numeric ID like /teachers/6. Can you make it so their last name is used instead of the numeric ID (like /teachers/warbelow) and I can access params[:last_name]? Use this blog post and/or this Stack Overflow answer. These blog posts go into depth on overwriting a method (to_param) in your model, but since we don't have a model, you don't need to worry about that part.

Extension:

  1. What does the following piece of code do? How do you have to modify your controllers in order to access these routes?
Rails.application.routes.draw do
  namespace :school do
    resources :teachers
  end
end