/
action_controller_basics.html
271 lines (190 loc) · 7.62 KB
/
action_controller_basics.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
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Ruby on Rails 101</title>
<!-- metadata -->
<meta name="generator" content="S5" />
<meta name="version" content="S5 1.1" />
<meta name="presdate" content="&date" />
<meta name="author" content="Peter Marklund" />
<meta name="organization" content="&organization;" />
<meta name="company" content="Rails Mentor" />
<!-- configuration parameters -->
<meta name="defaultView" content="slideshow" />
<meta name="controlVis" content="hidden" />
<!-- style sheet links -->
<link rel="stylesheet" href="../ui/default/slides.css" type="text/css" media="projection" id="slideProj" />
<link rel="stylesheet" href="../ui/default/outline.css" type="text/css" media="screen" id="outlineStyle" />
<link rel="stylesheet" href="../lib/stylesheets/print.css" type="text/css" media="print" id="slidePrint" />
<link rel="stylesheet" href="../ui/default/opera.css" type="text/css" media="projection" id="operaFix" />
<link rel="stylesheet" href="../lib/stylesheets/pressie.css" type="text/css" />
<!-- S5 JS -->
<script src="../ui/default/slides.js" type="text/javascript"></script>
<!-- Syntax Highlighter -->
<link rel="stylesheet" href="../dp.SyntaxHighlighter/Styles/SyntaxHighlighter.css"></link>
</head>
<body>
<div class="layout">
<div id="controls"><!-- DO NOT EDIT --></div>
<div id="currentSlide"><!-- DO NOT EDIT --></div>
<div id="header"></div>
<div id="footer">
<h2>Copyright © 2009 Peter Marklund</h2>
</div>
</div>
<div class="presentation">
<div class="slide">
<h1>Action Controller Basics</h1>
</div>
<div class="slide">
<h1>Controllers and Actions</h1>
<ul>
<li>Controllers are Ruby classes that live under app/controllers</li>
<li>Controller classes extend the ActionController::Base class</li>
<li>An action is a public method in the controller</li>
<li>An action typically reads from the database and renders a view template or writes to the database and issues a redirect</li>
<li>An action is invoked by a request to a certain <span class="caps">URL</span>. The mapping of URLs to actions is called routing.</li>
</ul>
</div>
<div class="slide">
<h1>Controller Environment</h1>
<div class="code-normal">
<pre name="code" class="ruby:nogutter:nocontrols">
cookies[:login] = { :value => "peter", :expires => 1.hour.from_now }
headers['Content-Type'] = 'application/pdf; charset=utf-8'
params
request: env, request_uri, get?, post?, xhr?, remote_ip
response
session[:user_id] = @user.id
logger.warn("Something pretty bad happened")
</pre></div>
</div>
<div class="slide">
<h1>request.env</h1>
<div class="code-normal">
<pre name="code" class="ruby:nogutter:nocontrols">
HTTP_ACCEPT_ENCODING = gzip,deflate
HTTP_USER_AGENT = Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en-US; rv:1.8.1.3)
SERVER_PROTOCOL = HTTP/1.1
HTTP_CACHE_CONTROL = no-cache
HTTP_ACCEPT_LANGUAGE = en-us,en;q=0.5
HTTP_HOST = localhost:3000
REMOTE_ADDR = 127.0.0.1
REQUEST_PATH = /
REQUEST_URI = /
SERVER_PORT = 3000
HTTP_ACCEPT = text/xml,application/xml,application/xhtml+xml,text/html
HTTP_CONNECTION = keep-alive
</pre></div>
</div>
<div class="slide">
<h1>Rendering a Response</h1>
<ul>
<li>All actions must render a response or issue a redirect</li>
<li>A response is rendered with the render command</li>
<li>An action can only render a response or redirect once</li>
<li>Rails invokes render automatically if you don’t</li>
<li>Redirects are made with the redirect_to command</li>
<li>You may need to make sure you return from an action after an invocation of render or redirect_to</li>
</ul>
</div>
<div class="slide">
<h1>Render Examples</h1>
<div class="code-normal">
<pre name="code" class="ruby:nogutter:nocontrols">
render :text => "Hello World"
render :action => "some_other_action", :layout => false
render :template => "weblog/show"
render :partial => "top_menu"
render :xml => {:name => "David"}.to_xml # Renders '<name>David</name>'
render :file => "/path/to/some/template.erb", :layout => true, :status => 404
</pre></div>
</div>
<div class="slide">
<h1>Redirect Examples</h1>
<div class="code-normal">
<pre name="code" class="ruby:nogutter:nocontrols">
redirect_to :back
redirect_to("/help/order_entry.html")
redirect_to :controller => 'blog', :action => 'list'
redirect_to @article #=> redirects to url_for(@article)
</pre></div>
</div>
<div class="slide">
<h1>Cookies</h1>
<ul>
<li>A hash stored by the browser</li>
<li>cookies[:preference] = {
:value => ‘ice cream’,
:expires => 10.days.from_now,
:path => ’/store’
}</li>
<li>Valid options: :domain, :expires, :path, :secure, :value</li>
</ul>
</div>
<div class="slide">
<h1>Sessions</h1>
<ul>
<li>A hash that can be stored in a cookie (default), a database table, in the file system etc.</li>
<li>The session is keyed by the cookie _app_name_session</li>
<li>Avoid storing complex Ruby objects, instead put id:s in the session and keep data in the database, i.e. use session[:user_id] rather than session[:user]</li>
</ul>
</div>
<div class="slide">
<h1>Session Configuration</h1>
<div class="code-normal">
<pre name="code" class="ruby:nogutter:nocontrols">
# In config/initializers/session_store.rb
ActionController::Base.session = {
:key => '_demo_session',
:secret => '62af9cd9dd2ac22e3e49a3254d2a27c75c67f46d489f7526cf5531dd46e81503a78cb16750ba90b8591c626ab817404f97c3039298f9e195b3a90c701f4d0b80'
}
# Use the database for sessions instead of the cookie-based default,
# which shouldn't be used to store highly confidential information
# (create the session table with "rake db:sessions:create")
# ActionController::Base.session_store = :active_record_store
</pre></div>
</div>
<div class="slide">
<h1>Flash Messages</h1>
<div class="code-normal">
<pre name="code" class="ruby:nogutter:nocontrols">
* The flash is a way to set a text message to the user in one request and then display it in the next (typically after a redirect)
* The flash is stored in the session
* flash[:notice], flash[:error]
* flash.now[:notice] = "Welcome" unless flash[:notice]
* flash.keep(:notice)
</pre></div>
</div>
<div class="slide">
<h1>Rescuing from Exceptions</h1>
<div class="code-normal">
<pre name="code" class="ruby:nogutter:nocontrols">
class ArticlesController < ApplicationController
rescue_from User::NotAuthorized, :with => :deny_access
private
def deny_access
...
end
end
</pre></div>
</div>
<div class="slide">
<h1>Best Practice</h1>
<ul>
<li><b>Don’t put <span class="caps">SQL</span> and too much code in your controllers/views – it’s a code smell, and maybe the most common design mistake Rails developers make. Actions should be 3-4 lines that script business objects. The goal is to have fat models and skinny controllers.</b></li>
<li><b>Always access data via the logged in user object (i.e. prefer current_user.articles.find(params[:id]) over Article.find(params[:id])).</b></div></li>
</ul>
<!-- Syntax Highlighter -->
<script language="javascript" src="../dp.SyntaxHighlighter/Scripts/shCore.js"></script>
<script language="javascript" src="../dp.SyntaxHighlighter/Scripts/shBrushCpp.js"></script>
<script language="javascript" src="../dp.SyntaxHighlighter/Scripts/shBrushRuby.js"></script>
<script language="javascript" src="../dp.SyntaxHighlighter/Scripts/shBrushXml.js"></script>
<script language="javascript">
dp.SyntaxHighlighter.ClipboardSwf = '/flash/clipboard.swf';
dp.SyntaxHighlighter.HighlightAll('code');
</script>
</div>
</body>
</html>