|
| 1 | +section "Overview" do |
| 2 | + message <<-MARKDOWN |
| 3 | +We're going to an index, so we can see all of our beautiful topics! |
| 4 | + |
| 5 | +### Reviewing MVC |
| 6 | +This is a review of MVC |
| 7 | + MARKDOWN |
| 8 | +end |
| 9 | + |
| 10 | +steps do |
| 11 | + step do |
| 12 | + message <<-MARKDOWN |
| 13 | +First, we're going to update the `routes.rb` file, inside the `config` directory. This file comes |
| 14 | +with lots of helpful comments that can help you create your routes correctly. We're going to add our |
| 15 | +routes using the resource method on line 2: |
| 16 | + MARKDOWN |
| 17 | + source_code <<-RUBY |
| 18 | +resources :topics |
| 19 | +root 'topics#index' |
| 20 | + RUBY |
| 21 | + message <<-MARKDOWN |
| 22 | +This is helping information about routes. Mention something about the root route. |
| 23 | + MARKDOWN |
| 24 | + end |
| 25 | + |
| 26 | + step do |
| 27 | + message <<-MARKDOWN |
| 28 | +Now let's look at what routes that generated by visiting http://localhost:3000/rails/info/routes. |
| 29 | + |
| 30 | +Here's some info about reading that page |
| 31 | + MARKDOWN |
| 32 | + end |
| 33 | + |
| 34 | + step do |
| 35 | + message <<-MARKDOWN |
| 36 | +Okay, let's go look at our app again and see what's changed. Visit http://localhost:3000. |
| 37 | + |
| 38 | +Now, instead of the default Rails page, we see the error "uninitialized constant TopicController" ... because |
| 39 | +there's no controller for topics! |
| 40 | + MARKDOWN |
| 41 | + end |
| 42 | + |
| 43 | + |
| 44 | + step do |
| 45 | + message <<-MARKDOWN |
| 46 | +Time for a topics controller |
| 47 | + MARKDOWN |
| 48 | + source_code <<-RUBY |
| 49 | +class TopicsController < ApplicationController |
| 50 | + def index |
| 51 | + end |
| 52 | +end |
| 53 | + RUBY |
| 54 | + message <<-MARKDOWN |
| 55 | +What does this do? How does it relate to the route? |
| 56 | + MARKDOWN |
| 57 | + end |
| 58 | +end |
| 59 | + |
| 60 | +section "Recap" do |
| 61 | + message <<-MARKDOWN |
| 62 | +This is a recap |
| 63 | + MARKDOWN |
| 64 | +end |
| 65 | + |
| 66 | +next_step "add_a_topics_index" |
| 67 | + |
| 68 | + |
| 69 | +message "# here's the original 'set root route' page content" |
| 70 | + |
| 71 | +goals { |
| 72 | + |
| 73 | + goal "Now that the structure is complete, let's make the flow work smoothly." |
| 74 | + |
| 75 | + message "Currently when you go to <http://localhost:3000> you see the \"Welcome |
| 76 | + aboard\" message." |
| 77 | + |
| 78 | + message "It would be easier to use our app if <http://localhost:3000> went directly to the topics list." |
| 79 | + |
| 80 | + message "In this step we'll make that happen and learn a bit about routes in Rails." |
| 81 | +} |
| 82 | + |
| 83 | +steps { |
| 84 | + |
| 85 | + step "Add a root route" do |
| 86 | + message "Open `config/routes.rb`. Search the file for 'root' (near the top) uncomment that line and change it to read `root 'topics#index'`. When you are done the line should look like this:" |
| 87 | + |
| 88 | + message "(Rails 3.x users should add `root to: 'topics#index'` and will need to remove their `public/index.html` file)." |
| 89 | + |
| 90 | + end |
| 91 | + |
| 92 | + source_code :ruby, <<-RUBY |
| 93 | +root 'topics#index' |
| 94 | + RUBY |
| 95 | + |
| 96 | + |
| 97 | + step "Confirm your changes" do |
| 98 | + message "Go back to <http://localhost:3000/>. You should be taken to the topics list automatically." |
| 99 | + end |
| 100 | +} |
| 101 | + |
| 102 | +explanation { |
| 103 | + |
| 104 | + message <<-MARKDOWN |
| 105 | + * `root 'topics#index'` is a rails route that says the default |
| 106 | + address for your site is `topics#index`. `topics#index` is the topics |
| 107 | + list page (the topics controller with the index action). |
| 108 | + * Rails routes control how URLs (web addresses) get matched with |
| 109 | + code on the server. Similar to how addresses match with houses and |
| 110 | + apartments. |
| 111 | + * The file `config/routes.rb` is like an address directory listing the |
| 112 | + possible addresses and which code goes with each one |
| 113 | + * `routes.rb` uses some shortcuts so it doesn't always show all the |
| 114 | + possible URLs. To explore the URLs in more detail we can use the |
| 115 | + terminal. |
| 116 | + |
| 117 | + At the terminal type `rake routes`. You should get something that |
| 118 | + looks like this: |
| 119 | + |
| 120 | + ```` |
| 121 | + $ rake routes |
| 122 | + |
| 123 | + Prefix Verb URI Pattern Controller#Action |
| 124 | + topics GET /topics(.:format) topics#index |
| 125 | + POST /topics(.:format) topics#create |
| 126 | + new_topic GET /topics/new(.:format) topics#new |
| 127 | + edit_topic GET /topics/:id/edit(.:format) topics#edit |
| 128 | + topic GET /topics/:id(.:format) topics#show |
| 129 | + PATCH /topics/:id(.:format) topics#update |
| 130 | + PUT /topics/:id(.:format) topics#update |
| 131 | + DELETE /topics/:id(.:format) topics#destroy |
| 132 | + root GET / topics#index |
| 133 | + ```` |
| 134 | + 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. |
| 135 | + |
| 136 | + 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: |
| 137 | + |
| 138 | + <img src='img/rails4_rails_info_routing.png'> |
| 139 | + |
| 140 | + 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>) |
| 141 | + |
| 142 | +### Exploring Routes (optional) |
| 143 | + |
| 144 | + Now you can have a look at the paths that are available in your app. |
| 145 | + Let's try looking at one of the topics routes we just generated. |
| 146 | + Open up your rails console and play: |
| 147 | + |
| 148 | + $ rails console |
| 149 | + >> app.topics_path |
| 150 | + => "/topics" |
| 151 | + >> app.topics_url |
| 152 | + => "http://www.example.com/topics" |
| 153 | + |
| 154 | + `app` is a special object that represents your entire application. |
| 155 | + You can ask it about its routes (as we just did), play with its |
| 156 | + database connections, or make pseudo-web requests against it with |
| 157 | + `get` or `post` (and lots more). |
| 158 | + |
| 159 | + MARKDOWN |
| 160 | +} |
| 161 | + |
0 commit comments