Other Pages

Expand All

setting_the_default_page.step

goals {
  message <<-MARKDOWN
  Now that the structure is complete, let's make the flow work smoothly.
  Currently when you go to <http://localhost:3000> you see the "Welcome aboard" message.
  It would be easier to use our app if <http://localhost:3000> went directly to the topics list.
  In this step we'll make that happen and learn a bit about routes in Rails.
  MARKDOWN
}
steps {
  step "Add a root route" do
    message "Open the file `config/routes.rb` in an editor."
    message "Search the file for **root**, it should be near the top if you are using Rails 4."
    message "Uncomment the line that contains the example command by removing the `#` sign in front of it, and change it to read `root 'topics#index'`. When you are done the line should look like this:"
    source_code :ruby, <<-RUBY
root 'topics#index'
    RUBY
    em do
      message "(Rails 3.x users should add `root to: 'topics#index'` and will need to remove their `public/index.html` file)."
    end
  end
  step "Confirm your changes" do
    message "Go back to <http://localhost:3000/>. You should be taken to the topics list automatically."
  end
}
explanation {
 message <<-MARKDOWN
  * `root 'topics#index'` is a rails route that says the default
    address for your site is `topics#index`. `topics#index` is the topics
    list page (the topics controller with the index action).
  * Rails routes control how URLs (web addresses) get matched with
    code on the server. Similar to how addresses match with houses and
    apartments.
  * The file `config/routes.rb` is like an address directory listing the
    possible addresses and which code goes with each one
  * `routes.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.
  At the terminal type `rake routes`. You should get something that
  looks like this:
  ````
    $ rake routes
        Prefix Verb   URI Pattern                Controller#Action
        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
          root GET    /                          topics#index
  ````
  This shows all the URLs your application responds to. The code that starts with colons are variables so :id means the id number of the record. The code in parenthesis is optional.
  In Rails 4, you can also get this information on your site in development. Go to <a href="http://localhost:3000/rails/info">http://localhost:3000/rails/info</a> and you'll see something like this:
  <img src='img/rails4_rails_info_routing.png'>
  You'll also see that table in Rails 4 whenever you try to access an invalid route (try <a href="http://localhost:3000/sandwich">http://localhost:3000/sandwich</a>)
### Exploring Routes (optional)
  Now you can have a look at the paths that are available in your app.
  Let's try looking at one of the topics routes we just generated.
  Open up your rails console and play:
    $ rails console
    >> app.topics_path
    => "/topics"
    >> app.topics_url
    => "http://www.example.com/topics"
  `app` is a special object that represents your entire application.
  You can ask it about its routes (as we just did), play with its
  database connections, or make pseudo-web requests against it with
  `get` or `post` (and lots more).
  Don't forget to exit the console before proceeding:
    >> exit
  <h2>Excellent! You've learnt about routes and you're about to send your app out to the Internetz!</h2>
  Stand up. High five the person next to you. Excellent.
  MARKDOWN
}
tip {
  message <<-MARKDOWN
  ## Short recap
  Find a partner and pair to describe the steps and what you've just done. Use sticky notes and paper.
  Write down things that you're unsure about too, then get a teacher to help you understand.
  MARKDOWN
}
insert 'consider_deploying'
next_step "voting_on_topics"

If you find something that could be improved, please make a pull request or drop us a note via GitHub Issues (no technical knowledge required).

Source: https://github.com/RailsBridge-CapeTown