-
Notifications
You must be signed in to change notification settings - Fork 2
/
README.html
392 lines (303 loc) · 11.2 KB
/
README.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html><head>
<!-- this document lives in my 465/lecture directory: http://www.ecst.csuchico.edu/~tyson/465/lectures.html -->
<meta content="text/html; charset=ISO-8859-1" http-equiv="content-type"><title>form_tag rails example (whois server)</title><meta http-equiv="content-type" content="text/html; charset=utf-8"></head><body>
<h1>Whois Server</h1>
<hr><br>
This
example rails application allows the user to enter a URL and then it
displays the result returned by the whois database. The intent is
to keep the application as simple as possible.<br>
<br>
It demonstrates:<br>
<br>
<div style="margin-left: 40px;">using the form_tag function (form_tag is similar to much more commonly used form_for but is not associated with an object)<br>
<br>
creating a "home page" that is not associated with any model<br>
<br>
how a controller can use the params hash (the params hash is a major piece of the Rails puzell)<br>
</div>
<br>The interesting parts of this demonstration are:<br>
<br>
<div style="margin-left: 40px;">The form_tag in app/views/home/index.html.erb<br>
<br>
The routes in config/routes.rb<br>
<br>
The show controller in app/controllers/home_controller.rb <br>
<br>
The views in app/views/home/index.html.erb and app/views/home/show.html.erb<br>
</div>
<br>
Details that are easy to miss:<br>
<br>
<div style="margin-left: 40px;">The form_tag arguments includes the
path to display the query (query_path) and the tag name for the text
the user enters ":query"<br>
<br>
Because the route in config/routes.rb is named "query", we must use
query_path in the form tag (we could have named it anything we wanted).<br>
<br>
query_path is a function "created" by declaring the query route in
config/routes.rb (this is probably implemented using the ruby
missing_method function; is was not really "created")<br>
<br>
Because "home#show" is in the config/routes.rb file, the function in
app/controllers/home_controller.rb must be "def show". <br>
<br>
Because ":query" is used, in the form_tag in home's index view, the
text the user typed will be available in the home#show controller as
params[:query]. <br>
</div>
<br>
Download the working code from github: $ git clone ssh://[email protected]/tysonhenry/whois.git<br>
<br>
<hr style="width: 100%; height: 2px;">
<h3>Step 0: Install the whois command</h3>
<p>
The computer you run this application on (either your AWS server or
your computer) must have the whois command installed. On most Linux
distributions you can install it like this:
</p>
<p>
</p>
<blockquote>
$ sudo apt-get whois
</blockquote>
We will execute the system whois command so you don't need a gem.<br>
<br>
<hr>
<h3>Step 1: Create "empty" Rails application</h3>
<p>
Create directory for your rails applications.
</p>
<blockquote>
$ cd ~/rails
</blockquote>
<p>
Use the rails "new" command to create a new application called "whois"
</p>
<blockquote>
$ rails new whois<br>
<br>
Notice the call to "bundle install" which makes sure that all the necessary gems are installed<br>
<br>
$ cd whois<br>
</blockquote>
<br>
<hr>
<h3>Step 2: Create a Rails "home" view</h3>
<p>
Make a directory for the "home" (default) view
</p>
<blockquote>
$ cd app/views # the assumption is that current directory is rails/whois<br>
$ mkdir home<br>
$ cd home<br>
</blockquote>
<p>
your current directory should be whois/app/views/home
</p>
<p>
add the following to the new file index.html.erb
</p>
<blockquote>
$ vim index.html.erb
</blockquote>
<div style="margin-left: 40px;"><h1>Whois Utility</h1><br>
<br><br>
<br><br>
<br><br>
<%= form_tag(query_path, method: "get") do %><br>
<%= label_tag(:query, "URL: ") %><br>
<%= text_field_tag(:query) %><br>
<%= submit_tag("Lookup") %><br>
<% end %><br>
<br><br>
</div>
<br>
<p>
The index controller (<span style="font-weight: bold;">def index</span>
in whois/app/controllers/home_controller.rb) is
the default (or root) controller. After this controller is
called, this page (whois/app/views/home/index.html.erb) is
rendered. Rails
always renders the view with the same name as the controller function
(in this example, the home/index.html.erb is rendered after the
function index in the home controller is executed). <br>
<br>
The text_field_tag creates a box for the user to enter a URL. The
generated form will associate ":query" with that string when it sends an http
message back to rails.<br>
<br>
The submit_tag will create a button for submitting the entered text. The button will be labeled "Lookup"<br>
<br>
When the submit button is pressed, the request will be sent to the
server with verb = GET, and the entered text.<br>
<br>
Below, a command is entered into the routes file (config/routes.rb) to
route "/query" to the show function in the home controller.</p>
<p>When rendered, the home.html.erb page displays the form that allows the user to enter a URL. It will look like this:<br>
<br>
</p>
<div style="margin-left: 40px;">
<table style="text-align: left; width: 319px; height: 173px;" border="1" cellpadding="2" cellspacing="2">
<tbody>
<tr>
<td style="vertical-align: top;"><h1 style="color: rgb(0, 0, 0); font-family: 'Times New Roman'; font-style: normal; font-variant: normal; letter-spacing: normal; line-height: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;">Whois Utility</h1><br>
<form accept-charset="UTF-8" action="http://sorry_this_button_does_not_work_here.com/query" method="get" style="color: rgb(0, 0, 0); font-family: 'Times New Roman'; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;"><label for="query">URL:<span class="Apple-converted-space"> </span></label><input id="query" name="query" type="text"><span class="Apple-converted-space"> </span><input name="commit" value="Lookup" type="submit"></form><br class="Apple-interchange-newline"></td>
</tr>
</tbody>
</table>
<br>
</div>
<br class="Apple-interchange-newline">
<br>
<hr>
<h3>Step 3: Create view to display the query results<br>
</h3>
Create the empty file and add the following text<br>
<div style="margin-left: 40px;"><br>
$ cd whois/app/views/home<br>
$ vim show.html.erb<br>
</div>
<p style="margin-left: 40px;"><%= link_to "Search Page", root_path %><br>
<br>
<p><%= "$ whois #{@query_text} returned" %></p><br>
<br><br>
<% @result.each do |line| %><br>
<%= line %><br>
<br><br>
<% end %><br>
<br>
<%= link_to "Search Page", root_path %></p>
<p><br>
</p>
<hr>
<h3>Step 4: Create two routes<br>
</h3>
<p>
Now we need to tell rails that when the user navigates to this
application it should show the home index page and when the user navigates to the query page it should show the query page<br>
</p>
<p>
add the following to whois/config/routes.rb (add them inside the .do loop)
</p>
<blockquote>
$ vim whois/app/config/routes.rb
</blockquote>
<p style="margin-left: 40px;"> root "home#index"<br>
<br>
get 'query', to: 'home#show'
</p>
<p>
we are telling rails that when <span style="font-style: italic;">root</span> url is requested, the index function in the home controller should be executed</p>
<p>and when the <span style="font-style: italic;">query</span> url arrives, the show function in the home controller should be executed<br>
</p>
<p>
You can see all the routes for a rail application using the "rake" utility
</p>
<blockquote>
$ rake routes
</blockquote>
<p>You can also examine the route helper functions (for this program root_path() and query_path()) using the rails console</p>
<div style="margin-left: 40px;">$ rails c<br>
Loading development environment (Rails 4.1.6)<br>
2.1.2 :001 > app.root_path<br>
=> "/" <br>
2.1.2 :002 > app.query_path<br>
=> "/query" <br>
2.1.2 :003 > <br>
<br>
</div>
<p>The rails console loads all the objects (and subsequently the
database when you have one) and allows you to access everything.
You can even modify the database.<br>
</p>
<p><br>
</p>
<p>
</p>
<hr>
<h3> Step 5: Create a home controller</h3>
add the following text to the new file home_controller.rb
<blockquote>
$ vim whois/app/controllers/home_controller.rb
</blockquote>
<div style="margin-left: 40px;">class HomeController < ApplicationController<br>
<br>
def index<br>
# don't need to do anything here<br>
end<br>
<br> def show<br>
@query_text = params[:query]<br>
if @query_text =~ /^[a-zA-Z0-9\.\-]*$/<br>
@result = `whois #{@query_text}`.split("\n")<br>
else<br>
@result = ["Illegal URL"];<br>
end<br>
end<br>
<br>
end<br>
</div>
<br>
<br>
<hr style="width: 100%; height: 2px;"><big style="font-weight: bold;">Step 6: Update the production secret_key_base</big> (only need this in production mode)<br>
<br>
<div style="margin-left: 40px;">By default rails assumes you will have
an environment variable named SECRET_KEY_BASE that will be used to
generate the keys for validating data from a client.<br>
<br>
You can create an environment variable that rails will see in
production by adding it to your ~/.profile. Below is an
example. If this was a highly secure site you would want to make
a nunique key (this should all be one one line with no spaces).<br>
<br>export
SECRET_KEY_BASE='59a948f27ea091f631f724c3742990b270dd5985adf99b2c98c55aa5b2ca887327d3f13d5dc5c433d753c93f9498b015d3772e46dee5c959f82c8de1078cffbe'<br>
<br>
</div>
<br>
<hr style="width: 100%; height: 2px;"><big><span style="font-weight: bold;">Step 7: Run the application</span></big><br>
<br>
<div style="margin-left: 40px;">In development mode:<br>
<br>
$ rails s<br>
<br>
Things to notice: <br>
<div style="margin-left: 40px;">Look at the URL generated by looking up
a URL (recall that http GET messages do not have a body, or at least a
body that is used)<br>
<br>
Look at the arguments "passed" to the home controller's show function (they are visible in the window runnng "$ rails s"<br>
<br>
<div style="margin-left: 40px;"><span style="font-weight: bold; color: red;">the "params" hash is a crutial part of how rails works***</span><br>
</div>
<br>
***The "params" has is not really a hash, it is a function that returns
a hash. But since this function returns a hash, we can use it as
if it were a hash. As with a lot of objects in ruby, they are not
what they appear.<br>
<br>
Look at the log/development.log file<br>
</div>
<br>
<br>
In production mode:<br>
<br>
added this project and its directory to /etc/apache2/sites-available/000-default.conf <br>
<br>
$ sudo apache2ctl restart # make an alias for this, you will probably do it a lot<br>
<br>
<br>
</div>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
</body></html>