Skip to content

Commit ca5b55c

Browse files
committedOct 21, 2013
WIP - Adding a scaffold-free version of Suggestotron
* First few pages are drafted * The rest of the curriculum has placeholder pages * Not using any generators other than rails generate migration
1 parent adf1143 commit ca5b55c

28 files changed

+1073
-0
lines changed
 

‎lib/step.rb

+5
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@ def todo todo_text
195195
IRB_CAPTION = "Type this in irb:"
196196
RESULT_CAPTION = "Expected result:"
197197
FUZZY_RESULT_CAPTION = "Approximate expected result:"
198+
RAILS_CONSOLE_CAPTION = "Type this in the Rails console:"
198199

199200
def console(commands)
200201
console_with_message(TERMINAL_CAPTION, commands)
@@ -211,6 +212,10 @@ def console_without_message(commands)
211212
console_with_message("", commands)
212213
end
213214

215+
def rails_console(commands)
216+
console_with_message(RAILS_CONSOLE_CAPTION, commands)
217+
end
218+
214219
def irb msg
215220
div :class => "console" do
216221
span IRB_CAPTION
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
div :class => "deploying" do
2+
h1 "Deploying"
3+
blockquote do
4+
message "Before the next step, you could try deploying your app to Heroku!"
5+
link 'deploying_to_heroku'
6+
end
7+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
message "`cd` stands for change directory."
2+
3+
option "Windows" do
4+
console "cd c:\\Sites"
5+
message "`cd c:\\Sites` sets our Sites directory to our current directory."
6+
end
7+
option "Mac or Linux" do
8+
console "cd ~"
9+
message "`cd ~` sets our home directory to our current directory."
10+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
message <<-MARKDOWN
2+
3+
This page will walk through adding a form to create topics. FORM HELPERS!
4+
5+
This is probably a good time to talk about HTTP verbs and RESTful routing.
6+
7+
MARKDOWN
8+
9+
next_step "add_an_edit_topics_form"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
section "Overview" do
2+
message <<-MARKDOWN
3+
Now let's add a view!
4+
5+
Things to cover:
6+
7+
* Getting resources from the DB via the controller
8+
* Sharing those with the view
9+
* ERB
10+
MARKDOWN
11+
end
12+
13+
steps do
14+
step do
15+
message <<-MARKDOWN
16+
First, let's add the correct view. Under views, add a folder called `topics`. Within that folder, make a file
17+
called `index.html.erb`
18+
MARKDOWN
19+
source_code "<h1>This is a topics index!!!</h1>"
20+
message <<-MARKDOWN
21+
View file structure description & how they're named the same as the controller actions
22+
MARKDOWN
23+
end
24+
25+
step do
26+
message <<-MARKDOWN
27+
Go to the browser and look at your beautiful page
28+
MARKDOWN
29+
end
30+
31+
step do
32+
message <<-MARKDOWN
33+
Now we'll actually show the topics on the page. First, we need to get all the topics out of the database.
34+
35+
Update your controller!
36+
MARKDOWN
37+
source_code <<-RUBY
38+
def index
39+
@topics = Topic.all
40+
end
41+
RUBY
42+
message <<-MARKDOWN
43+
Now we're getting the stuff from the controller
44+
MARKDOWN
45+
end
46+
47+
step do
48+
message <<-MARKDOWN
49+
Lets show it on the page. Add this to your view, `app/views/topics/index.html.erb`.
50+
MARKDOWN
51+
source_code <<-RUBY
52+
<%= @topics %>
53+
RUBY
54+
message <<-MARKDOWN
55+
Wow, that's ugly! I guess we're not done. ERB!!
56+
MARKDOWN
57+
end
58+
59+
step do
60+
message <<-MARKDOWN
61+
Let's show all the topics! Remove that ugly line, and replace it with this:
62+
MARKDOWN
63+
source_code <<-RUBY
64+
<% @topics.each do |topic| %>
65+
<h2><%= topic.title %></h2>
66+
<p><%= topic.description %></p>
67+
<% end %>
68+
RUBY
69+
message <<-MARKDOWN
70+
MARKDOWN
71+
end
72+
end
73+
74+
section "Recap" do
75+
message "This is a recap"
76+
end
77+
78+
next_step "add_a_new_topics_form"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
section "Overview" do
2+
message <<-MARKDOWN
3+
Now that we have a place to save our topics in the database, we need to create a Ruby class to represent
4+
our topics &mdash; a model.
5+
MARKDOWN
6+
end
7+
8+
steps do
9+
step do
10+
message <<-MARKDOWN
11+
Add a new file inside the directory `app/models`. In Sublime, you can right click (ctrl + click on Mac)
12+
and select "New File". Save it as `topic.rb`.
13+
MARKDOWN
14+
end
15+
16+
step do
17+
message "Add the following code to that file:"
18+
source_code <<-RUBY
19+
class Topic < ActiveRecord::Base
20+
end
21+
RUBY
22+
message <<-MARKDOWN
23+
This creates a class called Topic that inherits from ActiveRecord::Base. (Don't forget to save!)
24+
MARKDOWN
25+
end
26+
27+
step do
28+
message "Now we're going to make sure we hooked up our model correctly, using the Rails console!"
29+
console "rails console"
30+
message "You're now in an interactive terminal, just like IRB, but with all of your Rails app loaded up!"
31+
end
32+
33+
step do
34+
rails_console "Topic"
35+
message "This will tell you what Rails knows about the model Topic, something like: "
36+
source_code "=> Topic(id: integer, title: string, description: text)"
37+
end
38+
39+
step do
40+
rails_console 'cats = Topic.new(title: "Cats are so great", description: "Yay cats!")'
41+
message "This creates a new topic in the database, but it's not saved yet. See how it doesn't have an id?"
42+
source_code '=> #<Topic id: nil, title: "Cats are so great", description: "Yay cats!">'
43+
rails_console 'cats.save'
44+
rails_console 'cats'
45+
message "Now we have a saved topic &mdash; you can see it now has an id:"
46+
source_code '=> #<Topic id: 1, title: "Cats are so great", description: "Yay cats!">'
47+
end
48+
49+
step do
50+
message "Make two or three more topics so that you have something to look at once you make a web interface!"
51+
end
52+
53+
step do
54+
rails_console "Topic.all"
55+
message "Look at all your topics! Neat!"
56+
end
57+
end
58+
59+
next_step "add_a_topics_routes_and_controller"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
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

Comments
 (0)
Please sign in to comment.