Setting The Default Page
Goals
The main page of our app should be the list of topics.
Currently when you go to your server main page, you see the Rails Logo and Version. It would be easier to use our app if the main page went directly to the topics list. In this step we'll make that happen and learn a bit about routes in Rails.
Steps
Step 1
open
http://localhost:3000/
- it's still the default pageStep 2
Open the file
config/routes.rb
in the editor.Look for the line
Rails.application.routes.draw
at the beginning of the file, and add the lineroot 'topics#index'
after it. When you are done the start of the file should look like this:Rails.application.routes.draw do root 'topics#index'Step 3
Check
http://localhost:3000/
again. You should see the topics list now.Step 4
let's look at all the routes created by the scaffold - in the terminal
Type this in the shell:rails routes -c topics
Explanation
root 'topics#index'
is a Rails route that says the default address for your site the one given by the topics controller with the index action.- Rails routes control how URLs (web addresses) get matched with code on the server.
- The file
config/routes.rb
is like an address book listing the possible addresses and which code goes with each oneroutes.rb
uses some shortcuts so it doesn't always show all the possible URLs. To explore the URLs in more detail we can use the terminal.- When reading the output of
rails routes
:id
means the primary key of a record.:format
is the format of the view, e.g.html
orjson
.Prefix Verb URI Pattern Controller#Action root GET / topics#index topics GET /topics(.:format) topics#index POST /topics(.:format) topics#create new_topic GET /topics/new(.:format) topics#new edit_topic GET /topics/:id/edit(.:format) topics#edit topic GET /topics/:id(.:format) topics#show PATCH /topics/:id(.:format) topics#update PUT /topics/:id(.:format) topics#update DELETE /topics/:id(.:format) topics#destroy
Now would be a good time to commit to git
find a good commit message and commit your changes
Next Step:
Go on to Voting On Topics