Routes and Views


Premium Content - Free Preview

You created two pages, so now it's time to fix their URLs and content.

Adjusting the Routes

The URLs don’t look so nice, so let’s change them. Open up routes.rb and look at What Rails created:

config/routes.rb

get 'store/home'
get 'store/about'

The first line above maps visits from the URL '/store/home' to StoreController's home action, which returns the home web page.

Let's change the about route to the following:

get 'about', to: 'store#about'

This will map the shorter URL '/about' to StoreController's about action. It follows this general format of a route:

get 'URL', to: 'controller#action'

A route first states what URL it matches with, and then what controller and action it maps to. Later, we'll learn about other ways to specify routes.

Now let's set up a home page for our site to replace the default one. Instead of putting in a a URL, we'll just use the word root to mark the root URL itself:

root to: 'store#home'

Now routes.rb should look like this:

Rails.application.routes.draw do

  get 'about', to: 'store#about'
  root to: 'store#home'

  # lots of comments...
end

We can now navigate to the home page / of our site:


End of Free Content Preview. Please Sign in or Sign up to buy premium content.

Contact Us
Sign in or email us at [email protected]