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
-
Read the Rails Routing documentation. It's a pretty easy read!
-
Create a new rails project called
routes-practice
. -
Add a route to your
config/routes.rb
file that maps aGET
request to/students
with thestudents_controller
'sindex
method. -
Add the corresponding controller and method within that controller that renders the text: "You've hit the students index page."
-
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. -
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.
-
Modify your routes file to add all eight routes for a
Teacher
resource. -
Can you add code to the end of the
resources :teachers
line in order to only create theshow
andindex
routes instead of all eight routes generated byresources
? -
Add a controller for
teachers
and ensure that theshow
andindex
actions work. -
Add a root path so that we can navigate to
http://localhost:3000/
and not get an error. Have this route go to theindex
action in a welcome controller. Create the corresponding controller and action, and test out the root by starting your server and navigating to'/'
. -
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 accessparams[: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.
- 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