kitabu 0.4.7 → 1.0.0.rc1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (544) hide show
  1. data/.gitignore +3 -0
  2. data/.gitmodules +3 -0
  3. data/.rspec +1 -0
  4. data/Gemfile +2 -0
  5. data/Gemfile.lock +62 -0
  6. data/README.rdoc +187 -0
  7. data/Rakefile +4 -36
  8. data/bin/kitabu +2 -5
  9. data/examples/RailsGuides/config/helper.rb +29 -0
  10. data/examples/RailsGuides/config/kitabu.yml +48 -0
  11. data/examples/RailsGuides/images/challenge.png +0 -0
  12. data/examples/RailsGuides/images/posts_index.png +0 -0
  13. data/examples/RailsGuides/images/rails_welcome.png +0 -0
  14. data/examples/RailsGuides/output/RailsGuides.epub +0 -0
  15. data/examples/RailsGuides/output/RailsGuides.html +1556 -0
  16. data/examples/RailsGuides/output/RailsGuides.pdf +4930 -7
  17. data/examples/RailsGuides/templates/cover.erb +16 -0
  18. data/examples/RailsGuides/templates/layout.css +352 -0
  19. data/examples/RailsGuides/templates/layout.erb +43 -0
  20. data/{lib/kitabu/vendor/ruby1.8/uv/render/xhtml/files/css/idle.css → examples/RailsGuides/templates/syntax.css} +0 -0
  21. data/examples/RailsGuides/templates/user.css +19 -0
  22. data/examples/RailsGuides/text/01_Guide_Assumptions.mkdn +13 -0
  23. data/examples/RailsGuides/text/02_What_is_Rails.mkdn +106 -0
  24. data/examples/RailsGuides/text/03_Creating_a_new_Rails_project.mkdn +200 -0
  25. data/examples/RailsGuides/text/04_Hello_Rails.mkdn +62 -0
  26. data/examples/RailsGuides/text/05_Getting_Up_and_Running_Quickly_with_Scaffolding.mkdn +4 -0
  27. data/examples/RailsGuides/text/06_Creating_a_resource.mkdn +503 -0
  28. data/examples/RailsGuides/text/07_Adding_a_second_model.mkdn +232 -0
  29. data/examples/RailsGuides/text/08_Refactoring.mkdn +123 -0
  30. data/examples/RailsGuides/text/09_Deleting_comments.mkdn +57 -0
  31. data/examples/RailsGuides/text/09_Security.mkdn +56 -0
  32. data/examples/RailsGuides/text/10_Building_a_multi_model_form.mkdn +130 -0
  33. data/examples/RailsGuides/text/11_View_helpers.mkdn +50 -0
  34. data/examples/RailsGuides/text/12_Whats_next.mkdn +14 -0
  35. data/examples/RailsGuides/text/13_Configuration_gotchas.mkdn +10 -0
  36. data/kitabu.gemspec +28 -491
  37. data/lib/kitabu.rb +54 -46
  38. data/lib/kitabu/cli.rb +88 -0
  39. data/lib/kitabu/errors.rb +3 -0
  40. data/lib/kitabu/exporter.rb +81 -0
  41. data/lib/kitabu/extensions/redcloth.rb +65 -0
  42. data/lib/kitabu/extensions/string.rb +11 -0
  43. data/lib/kitabu/generator.rb +74 -0
  44. data/lib/kitabu/parser.rb +34 -0
  45. data/lib/kitabu/parser/epub.rb +131 -0
  46. data/lib/kitabu/parser/html.rb +174 -0
  47. data/lib/kitabu/parser/mobi.rb +17 -0
  48. data/lib/kitabu/parser/pdf.rb +41 -0
  49. data/lib/kitabu/parser/txt.rb +22 -0
  50. data/lib/kitabu/stream.rb +27 -0
  51. data/lib/kitabu/syntax.rb +109 -107
  52. data/lib/kitabu/toc.rb +62 -33
  53. data/lib/kitabu/version.rb +8 -0
  54. data/spec/kitabu/cli/export_spec.rb +17 -0
  55. data/spec/kitabu/cli/new_spec.rb +21 -0
  56. data/spec/kitabu/cli/permalinks_spec.rb +12 -0
  57. data/spec/kitabu/cli/version_spec.rb +12 -0
  58. data/spec/kitabu/extensions/redcloth_spec.rb +52 -0
  59. data/spec/kitabu/extensions/string_spec.rb +21 -0
  60. data/spec/kitabu/generator_spec.rb +10 -0
  61. data/spec/kitabu/parser/epub_spec.rb +10 -0
  62. data/spec/kitabu/parser/html_spec.rb +75 -0
  63. data/spec/kitabu/parser/pdf_spec.rb +14 -0
  64. data/spec/kitabu/syntax_spec.rb +80 -76
  65. data/spec/kitabu/toc_spec.rb +42 -68
  66. data/spec/kitabu_spec.rb +4 -0
  67. data/spec/spec_helper.rb +20 -24
  68. data/spec/support/exit_with_code.rb +26 -0
  69. data/spec/support/have_tag.rb +115 -0
  70. data/spec/support/helper.rb +18 -0
  71. data/spec/support/mybook/.gitignore +1 -0
  72. data/spec/support/mybook/code/.gitkeep +0 -0
  73. data/spec/support/mybook/code/code.rb +13 -0
  74. data/spec/support/mybook/config/helper.rb +29 -0
  75. data/spec/support/mybook/config/kitabu.yml +59 -0
  76. data/spec/support/mybook/images/.gitkeep +0 -0
  77. data/spec/support/mybook/images/logo.gif +0 -0
  78. data/spec/support/mybook/output/.gitkeep +0 -0
  79. data/spec/support/mybook/templates/cover.erb +16 -0
  80. data/spec/support/mybook/templates/epub.css +1 -0
  81. data/spec/support/mybook/templates/epub.erb +15 -0
  82. data/spec/support/mybook/templates/layout.css +137 -0
  83. data/{templates/layouts/boom/layout.html → spec/support/mybook/templates/layout.erb} +26 -17
  84. data/{templates/themes/mac_classic.css → spec/support/mybook/templates/syntax.css} +63 -0
  85. data/spec/support/mybook/templates/user.css +1 -0
  86. data/spec/support/mybook/text/.gitkeep +0 -0
  87. data/spec/support/mybook/text/01_Markdown_Chapter.markdown +8 -0
  88. data/spec/support/mybook/text/02_Textile_Chapter.textile +3 -0
  89. data/spec/support/mybook/text/03_HTML_Chapter.html +3 -0
  90. data/spec/support/mybook/text/04_With_Directory/Some_Chapter.mkdn +3 -0
  91. data/spec/support/mybook/text/CHANGELOG.textile +6 -0
  92. data/spec/support/mybook/text/TOC.textile +0 -0
  93. data/spec/support/mybook/text/_00_Introduction.markdown +3 -0
  94. data/spec/support/shared.rb +16 -0
  95. data/templates/config.erb +48 -0
  96. data/templates/cover.erb +16 -0
  97. data/templates/ebook.png +0 -0
  98. data/templates/epub.css +0 -0
  99. data/templates/epub.erb +15 -0
  100. data/templates/helper.rb +29 -0
  101. data/templates/layout.css +353 -0
  102. data/templates/layout.erb +44 -0
  103. data/{lib/kitabu/vendor/ruby1.8/uv/render/xhtml/files/css → templates/styles}/active4d.css +0 -0
  104. data/{lib/kitabu/vendor/ruby1.8/uv/render/xhtml/files/css → templates/styles}/all_hallows_eve.css +0 -0
  105. data/{lib/kitabu/vendor/ruby1.8/uv/render/xhtml/files/css → templates/styles}/amy.css +0 -0
  106. data/{lib/kitabu/vendor/ruby1.8/uv/render/xhtml/files/css → templates/styles}/blackboard.css +0 -0
  107. data/{lib/kitabu/vendor/ruby1.8/uv/render/xhtml/files/css → templates/styles}/brilliance_black.css +0 -0
  108. data/{lib/kitabu/vendor/ruby1.8/uv/render/xhtml/files/css → templates/styles}/brilliance_dull.css +0 -0
  109. data/{lib/kitabu/vendor/ruby1.8/uv/render/xhtml/files/css → templates/styles}/cobalt.css +0 -0
  110. data/{lib/kitabu/vendor/ruby1.9/uv/render/xhtml/files/css → templates/styles}/dawn.css +0 -0
  111. data/{lib/kitabu/vendor/ruby1.8/uv/render/xhtml/files/css → templates/styles}/eiffel.css +0 -0
  112. data/{lib/kitabu/vendor/ruby1.8/uv/render/xhtml/files/css → templates/styles}/espresso_libre.css +0 -0
  113. data/{lib/kitabu/vendor/ruby1.9/uv/render/xhtml/files/css → templates/styles}/idle.css +0 -0
  114. data/{lib/kitabu/vendor/ruby1.8/uv/render/xhtml/files/css → templates/styles}/iplastic.css +0 -0
  115. data/{lib/kitabu/vendor/ruby1.8/uv/render/xhtml/files/css → templates/styles}/lazy.css +0 -0
  116. data/{lib/kitabu/vendor/ruby1.8/uv/render/xhtml/files/css → templates/styles}/mac_classic.css +0 -0
  117. data/{lib/kitabu/vendor/ruby1.8/uv/render/xhtml/files/css → templates/styles}/magicwb_amiga.css +0 -0
  118. data/{lib/kitabu/vendor/ruby1.8/uv/render/xhtml/files/css → templates/styles}/pastels_on_dark.css +0 -0
  119. data/{lib/kitabu/vendor/ruby1.8/uv/render/xhtml/files/css → templates/styles}/slush_poppies.css +0 -0
  120. data/{lib/kitabu/vendor/ruby1.8/uv/render/xhtml/files/css → templates/styles}/spacecadet.css +0 -0
  121. data/{lib/kitabu/vendor/ruby1.8/uv/render/xhtml/files/css → templates/styles}/sunburst.css +0 -0
  122. data/{lib/kitabu/vendor/ruby1.8/uv/render/xhtml/files/css → templates/styles}/twilight.css +0 -0
  123. data/{lib/kitabu/vendor/ruby1.8/uv/render/xhtml/files/css → templates/styles}/zenburnesque.css +0 -0
  124. data/templates/user.css +1 -1
  125. metadata +395 -501
  126. data/README.markdown +0 -60
  127. data/VERSION +0 -1
  128. data/lib/kitabu/base.rb +0 -271
  129. data/lib/kitabu/blackcloth.rb +0 -184
  130. data/lib/kitabu/command.rb +0 -94
  131. data/lib/kitabu/redcloth.rb +0 -964
  132. data/lib/kitabu/tasks.rb +0 -113
  133. data/lib/kitabu/templates.rb +0 -54
  134. data/lib/kitabu/vendor/colorize/colorize.rb +0 -81
  135. data/lib/kitabu/vendor/ruby1.8/plist/plist.rb +0 -22
  136. data/lib/kitabu/vendor/ruby1.8/plist/plist/generator.rb +0 -224
  137. data/lib/kitabu/vendor/ruby1.8/plist/plist/parser.rb +0 -224
  138. data/lib/kitabu/vendor/ruby1.8/textpow/textpow.rb +0 -10
  139. data/lib/kitabu/vendor/ruby1.8/textpow/textpow/debug_processor.rb +0 -36
  140. data/lib/kitabu/vendor/ruby1.8/textpow/textpow/score_manager.rb +0 -65
  141. data/lib/kitabu/vendor/ruby1.8/textpow/textpow/syntax.rb +0 -284
  142. data/lib/kitabu/vendor/ruby1.8/uv/render/xhtml/active4d.render +0 -140
  143. data/lib/kitabu/vendor/ruby1.8/uv/render/xhtml/all_hallows_eve.render +0 -104
  144. data/lib/kitabu/vendor/ruby1.8/uv/render/xhtml/amy.render +0 -179
  145. data/lib/kitabu/vendor/ruby1.8/uv/render/xhtml/blackboard.render +0 -119
  146. data/lib/kitabu/vendor/ruby1.8/uv/render/xhtml/brilliance_black.render +0 -560
  147. data/lib/kitabu/vendor/ruby1.8/uv/render/xhtml/brilliance_dull.render +0 -569
  148. data/lib/kitabu/vendor/ruby1.8/uv/render/xhtml/cobalt.render +0 -170
  149. data/lib/kitabu/vendor/ruby1.8/uv/render/xhtml/dawn.render +0 -134
  150. data/lib/kitabu/vendor/ruby1.8/uv/render/xhtml/eiffel.render +0 -140
  151. data/lib/kitabu/vendor/ruby1.8/uv/render/xhtml/espresso_libre.render +0 -131
  152. data/lib/kitabu/vendor/ruby1.8/uv/render/xhtml/files/css/dawn.css +0 -121
  153. data/lib/kitabu/vendor/ruby1.8/uv/render/xhtml/idle.render +0 -101
  154. data/lib/kitabu/vendor/ruby1.8/uv/render/xhtml/iplastic.render +0 -107
  155. data/lib/kitabu/vendor/ruby1.8/uv/render/xhtml/lazy.render +0 -104
  156. data/lib/kitabu/vendor/ruby1.8/uv/render/xhtml/mac_classic.render +0 -143
  157. data/lib/kitabu/vendor/ruby1.8/uv/render/xhtml/magicwb_amiga.render +0 -125
  158. data/lib/kitabu/vendor/ruby1.8/uv/render/xhtml/pastels_on_dark.render +0 -212
  159. data/lib/kitabu/vendor/ruby1.8/uv/render/xhtml/slush_poppies.render +0 -131
  160. data/lib/kitabu/vendor/ruby1.8/uv/render/xhtml/spacecadet.render +0 -89
  161. data/lib/kitabu/vendor/ruby1.8/uv/render/xhtml/sunburst.render +0 -194
  162. data/lib/kitabu/vendor/ruby1.8/uv/render/xhtml/twilight.render +0 -161
  163. data/lib/kitabu/vendor/ruby1.8/uv/render/xhtml/zenburnesque.render +0 -134
  164. data/lib/kitabu/vendor/ruby1.8/uv/syntax/actionscript.syntax +0 -97
  165. data/lib/kitabu/vendor/ruby1.8/uv/syntax/active4d.syntax +0 -276
  166. data/lib/kitabu/vendor/ruby1.8/uv/syntax/active4d_html.syntax +0 -311
  167. data/lib/kitabu/vendor/ruby1.8/uv/syntax/active4d_ini.syntax +0 -50
  168. data/lib/kitabu/vendor/ruby1.8/uv/syntax/active4d_library.syntax +0 -21
  169. data/lib/kitabu/vendor/ruby1.8/uv/syntax/ada.syntax +0 -33
  170. data/lib/kitabu/vendor/ruby1.8/uv/syntax/antlr.syntax +0 -151
  171. data/lib/kitabu/vendor/ruby1.8/uv/syntax/apache.syntax +0 -191
  172. data/lib/kitabu/vendor/ruby1.8/uv/syntax/applescript.syntax +0 -384
  173. data/lib/kitabu/vendor/ruby1.8/uv/syntax/asp.syntax +0 -70
  174. data/lib/kitabu/vendor/ruby1.8/uv/syntax/asp_vb.net.syntax +0 -129
  175. data/lib/kitabu/vendor/ruby1.8/uv/syntax/bibtex.syntax +0 -151
  176. data/lib/kitabu/vendor/ruby1.8/uv/syntax/blog_html.syntax +0 -41
  177. data/lib/kitabu/vendor/ruby1.8/uv/syntax/blog_markdown.syntax +0 -42
  178. data/lib/kitabu/vendor/ruby1.8/uv/syntax/blog_text.syntax +0 -27
  179. data/lib/kitabu/vendor/ruby1.8/uv/syntax/blog_textile.syntax +0 -27
  180. data/lib/kitabu/vendor/ruby1.8/uv/syntax/buggy/nemerle.syntax +0 -74
  181. data/lib/kitabu/vendor/ruby1.8/uv/syntax/build.syntax +0 -53
  182. data/lib/kitabu/vendor/ruby1.8/uv/syntax/bulletin_board.syntax +0 -287
  183. data/lib/kitabu/vendor/ruby1.8/uv/syntax/c++.syntax +0 -109
  184. data/lib/kitabu/vendor/ruby1.8/uv/syntax/c.syntax +0 -326
  185. data/lib/kitabu/vendor/ruby1.8/uv/syntax/cake.syntax +0 -55
  186. data/lib/kitabu/vendor/ruby1.8/uv/syntax/camlp4.syntax +0 -36
  187. data/lib/kitabu/vendor/ruby1.8/uv/syntax/cm.syntax +0 -32
  188. data/lib/kitabu/vendor/ruby1.8/uv/syntax/coldfusion.syntax +0 -119
  189. data/lib/kitabu/vendor/ruby1.8/uv/syntax/context_free.syntax +0 -176
  190. data/lib/kitabu/vendor/ruby1.8/uv/syntax/cs.syntax +0 -59
  191. data/lib/kitabu/vendor/ruby1.8/uv/syntax/css.syntax +0 -195
  192. data/lib/kitabu/vendor/ruby1.8/uv/syntax/css_experimental.syntax +0 -1925
  193. data/lib/kitabu/vendor/ruby1.8/uv/syntax/csv.syntax +0 -68
  194. data/lib/kitabu/vendor/ruby1.8/uv/syntax/d.syntax +0 -142
  195. data/lib/kitabu/vendor/ruby1.8/uv/syntax/diff.syntax +0 -81
  196. data/lib/kitabu/vendor/ruby1.8/uv/syntax/dokuwiki.syntax +0 -204
  197. data/lib/kitabu/vendor/ruby1.8/uv/syntax/dot.syntax +0 -47
  198. data/lib/kitabu/vendor/ruby1.8/uv/syntax/doxygen.syntax +0 -43
  199. data/lib/kitabu/vendor/ruby1.8/uv/syntax/dylan.syntax +0 -62
  200. data/lib/kitabu/vendor/ruby1.8/uv/syntax/eiffel.syntax +0 -78
  201. data/lib/kitabu/vendor/ruby1.8/uv/syntax/erlang.syntax +0 -922
  202. data/lib/kitabu/vendor/ruby1.8/uv/syntax/f-script.syntax +0 -80
  203. data/lib/kitabu/vendor/ruby1.8/uv/syntax/fortran.syntax +0 -141
  204. data/lib/kitabu/vendor/ruby1.8/uv/syntax/fxscript.syntax +0 -142
  205. data/lib/kitabu/vendor/ruby1.8/uv/syntax/greasemonkey.syntax +0 -34
  206. data/lib/kitabu/vendor/ruby1.8/uv/syntax/gri.syntax +0 -83
  207. data/lib/kitabu/vendor/ruby1.8/uv/syntax/groovy.syntax +0 -191
  208. data/lib/kitabu/vendor/ruby1.8/uv/syntax/gtd.syntax +0 -22
  209. data/lib/kitabu/vendor/ruby1.8/uv/syntax/gtdalt.syntax +0 -143
  210. data/lib/kitabu/vendor/ruby1.8/uv/syntax/haml.syntax +0 -88
  211. data/lib/kitabu/vendor/ruby1.8/uv/syntax/haskell.syntax +0 -88
  212. data/lib/kitabu/vendor/ruby1.8/uv/syntax/html-asp.syntax +0 -27
  213. data/lib/kitabu/vendor/ruby1.8/uv/syntax/html.syntax +0 -362
  214. data/lib/kitabu/vendor/ruby1.8/uv/syntax/html_django.syntax +0 -36
  215. data/lib/kitabu/vendor/ruby1.8/uv/syntax/html_for_asp.net.syntax +0 -424
  216. data/lib/kitabu/vendor/ruby1.8/uv/syntax/html_mason.syntax +0 -119
  217. data/lib/kitabu/vendor/ruby1.8/uv/syntax/html_rails.syntax +0 -40
  218. data/lib/kitabu/vendor/ruby1.8/uv/syntax/html_tcl.syntax +0 -26
  219. data/lib/kitabu/vendor/ruby1.8/uv/syntax/icalendar.syntax +0 -32
  220. data/lib/kitabu/vendor/ruby1.8/uv/syntax/inform.syntax +0 -48
  221. data/lib/kitabu/vendor/ruby1.8/uv/syntax/ini.syntax +0 -55
  222. data/lib/kitabu/vendor/ruby1.8/uv/syntax/installer_distribution_script.syntax +0 -77
  223. data/lib/kitabu/vendor/ruby1.8/uv/syntax/io.syntax +0 -81
  224. data/lib/kitabu/vendor/ruby1.8/uv/syntax/java.syntax +0 -211
  225. data/lib/kitabu/vendor/ruby1.8/uv/syntax/javaproperties.syntax +0 -20
  226. data/lib/kitabu/vendor/ruby1.8/uv/syntax/javascript.syntax +0 -256
  227. data/lib/kitabu/vendor/ruby1.8/uv/syntax/javascript_+_prototype.syntax +0 -72
  228. data/lib/kitabu/vendor/ruby1.8/uv/syntax/javascript_+_prototype_bracketed.syntax +0 -140
  229. data/lib/kitabu/vendor/ruby1.8/uv/syntax/jquery_javascript.syntax +0 -114
  230. data/lib/kitabu/vendor/ruby1.8/uv/syntax/json.syntax +0 -136
  231. data/lib/kitabu/vendor/ruby1.8/uv/syntax/languagedefinition.syntax +0 -708
  232. data/lib/kitabu/vendor/ruby1.8/uv/syntax/latex.syntax +0 -566
  233. data/lib/kitabu/vendor/ruby1.8/uv/syntax/latex_beamer.syntax +0 -41
  234. data/lib/kitabu/vendor/ruby1.8/uv/syntax/latex_log.syntax +0 -50
  235. data/lib/kitabu/vendor/ruby1.8/uv/syntax/latex_memoir.syntax +0 -64
  236. data/lib/kitabu/vendor/ruby1.8/uv/syntax/lexflex.syntax +0 -219
  237. data/lib/kitabu/vendor/ruby1.8/uv/syntax/lighttpd.syntax +0 -54
  238. data/lib/kitabu/vendor/ruby1.8/uv/syntax/lilypond.syntax +0 -492
  239. data/lib/kitabu/vendor/ruby1.8/uv/syntax/lisp.syntax +0 -61
  240. data/lib/kitabu/vendor/ruby1.8/uv/syntax/literate_haskell.syntax +0 -24
  241. data/lib/kitabu/vendor/ruby1.8/uv/syntax/logo.syntax +0 -29
  242. data/lib/kitabu/vendor/ruby1.8/uv/syntax/logtalk.syntax +0 -152
  243. data/lib/kitabu/vendor/ruby1.8/uv/syntax/lua.syntax +0 -86
  244. data/lib/kitabu/vendor/ruby1.8/uv/syntax/m.syntax +0 -142
  245. data/lib/kitabu/vendor/ruby1.8/uv/syntax/macports_portfile.syntax +0 -163
  246. data/lib/kitabu/vendor/ruby1.8/uv/syntax/mail.syntax +0 -118
  247. data/lib/kitabu/vendor/ruby1.8/uv/syntax/makefile.syntax +0 -36
  248. data/lib/kitabu/vendor/ruby1.8/uv/syntax/man.syntax +0 -17
  249. data/lib/kitabu/vendor/ruby1.8/uv/syntax/markdown.syntax +0 -543
  250. data/lib/kitabu/vendor/ruby1.8/uv/syntax/mediawiki.syntax +0 -567
  251. data/lib/kitabu/vendor/ruby1.8/uv/syntax/mel.syntax +0 -92
  252. data/lib/kitabu/vendor/ruby1.8/uv/syntax/mips.syntax +0 -66
  253. data/lib/kitabu/vendor/ruby1.8/uv/syntax/mod_perl.syntax +0 -50
  254. data/lib/kitabu/vendor/ruby1.8/uv/syntax/modula-3.syntax +0 -47
  255. data/lib/kitabu/vendor/ruby1.8/uv/syntax/moinmoin.syntax +0 -189
  256. data/lib/kitabu/vendor/ruby1.8/uv/syntax/mootools.syntax +0 -572
  257. data/lib/kitabu/vendor/ruby1.8/uv/syntax/movable_type.syntax +0 -162
  258. data/lib/kitabu/vendor/ruby1.8/uv/syntax/multimarkdown.syntax +0 -39
  259. data/lib/kitabu/vendor/ruby1.8/uv/syntax/objective-c++.syntax +0 -18
  260. data/lib/kitabu/vendor/ruby1.8/uv/syntax/objective-c.syntax +0 -233
  261. data/lib/kitabu/vendor/ruby1.8/uv/syntax/ocaml.syntax +0 -764
  262. data/lib/kitabu/vendor/ruby1.8/uv/syntax/ocamllex.syntax +0 -167
  263. data/lib/kitabu/vendor/ruby1.8/uv/syntax/ocamlyacc.syntax +0 -184
  264. data/lib/kitabu/vendor/ruby1.8/uv/syntax/old/YAML.yaml +0 -160
  265. data/lib/kitabu/vendor/ruby1.8/uv/syntax/old/txt2tags.yaml +0 -139
  266. data/lib/kitabu/vendor/ruby1.8/uv/syntax/opengl.syntax +0 -14
  267. data/lib/kitabu/vendor/ruby1.8/uv/syntax/pascal.syntax +0 -77
  268. data/lib/kitabu/vendor/ruby1.8/uv/syntax/perl.syntax +0 -1115
  269. data/lib/kitabu/vendor/ruby1.8/uv/syntax/php.syntax +0 -1253
  270. data/lib/kitabu/vendor/ruby1.8/uv/syntax/plain_text.syntax +0 -32
  271. data/lib/kitabu/vendor/ruby1.8/uv/syntax/pmwiki.syntax +0 -113
  272. data/lib/kitabu/vendor/ruby1.8/uv/syntax/postscript.syntax +0 -114
  273. data/lib/kitabu/vendor/ruby1.8/uv/syntax/processing.syntax +0 -106
  274. data/lib/kitabu/vendor/ruby1.8/uv/syntax/prolog.syntax +0 -40
  275. data/lib/kitabu/vendor/ruby1.8/uv/syntax/property_list.syntax +0 -635
  276. data/lib/kitabu/vendor/ruby1.8/uv/syntax/python.syntax +0 -868
  277. data/lib/kitabu/vendor/ruby1.8/uv/syntax/python_django.syntax +0 -21
  278. data/lib/kitabu/vendor/ruby1.8/uv/syntax/qmake_project.syntax +0 -114
  279. data/lib/kitabu/vendor/ruby1.8/uv/syntax/qt_c++.syntax +0 -26
  280. data/lib/kitabu/vendor/ruby1.8/uv/syntax/quake3_config.syntax +0 -32
  281. data/lib/kitabu/vendor/ruby1.8/uv/syntax/r.syntax +0 -81
  282. data/lib/kitabu/vendor/ruby1.8/uv/syntax/r_console.syntax +0 -16
  283. data/lib/kitabu/vendor/ruby1.8/uv/syntax/ragel.syntax +0 -201
  284. data/lib/kitabu/vendor/ruby1.8/uv/syntax/rd_r_documentation.syntax +0 -91
  285. data/lib/kitabu/vendor/ruby1.8/uv/syntax/regexp.syntax +0 -50
  286. data/lib/kitabu/vendor/ruby1.8/uv/syntax/regular_expressions_oniguruma.syntax +0 -107
  287. data/lib/kitabu/vendor/ruby1.8/uv/syntax/regular_expressions_python.syntax +0 -109
  288. data/lib/kitabu/vendor/ruby1.8/uv/syntax/release_notes.syntax +0 -46
  289. data/lib/kitabu/vendor/ruby1.8/uv/syntax/remind.syntax +0 -253
  290. data/lib/kitabu/vendor/ruby1.8/uv/syntax/restructuredtext.syntax +0 -250
  291. data/lib/kitabu/vendor/ruby1.8/uv/syntax/rez.syntax +0 -80
  292. data/lib/kitabu/vendor/ruby1.8/uv/syntax/ruby.syntax +0 -1035
  293. data/lib/kitabu/vendor/ruby1.8/uv/syntax/ruby_experimental.syntax +0 -145
  294. data/lib/kitabu/vendor/ruby1.8/uv/syntax/ruby_on_rails.syntax +0 -88
  295. data/lib/kitabu/vendor/ruby1.8/uv/syntax/s5.syntax +0 -69
  296. data/lib/kitabu/vendor/ruby1.8/uv/syntax/scheme.syntax +0 -347
  297. data/lib/kitabu/vendor/ruby1.8/uv/syntax/scilab.syntax +0 -41
  298. data/lib/kitabu/vendor/ruby1.8/uv/syntax/setext.syntax +0 -147
  299. data/lib/kitabu/vendor/ruby1.8/uv/syntax/shell-unix-generic.syntax +0 -384
  300. data/lib/kitabu/vendor/ruby1.8/uv/syntax/slate.syntax +0 -149
  301. data/lib/kitabu/vendor/ruby1.8/uv/syntax/smarty.syntax +0 -63
  302. data/lib/kitabu/vendor/ruby1.8/uv/syntax/sql.syntax +0 -237
  303. data/lib/kitabu/vendor/ruby1.8/uv/syntax/sql_rails.syntax +0 -18
  304. data/lib/kitabu/vendor/ruby1.8/uv/syntax/ssh-config.syntax +0 -33
  305. data/lib/kitabu/vendor/ruby1.8/uv/syntax/standard_ml.syntax +0 -121
  306. data/lib/kitabu/vendor/ruby1.8/uv/syntax/strings_file.syntax +0 -39
  307. data/lib/kitabu/vendor/ruby1.8/uv/syntax/subversion_commit_message.syntax +0 -36
  308. data/lib/kitabu/vendor/ruby1.8/uv/syntax/sweave.syntax +0 -84
  309. data/lib/kitabu/vendor/ruby1.8/uv/syntax/swig.syntax +0 -57
  310. data/lib/kitabu/vendor/ruby1.8/uv/syntax/tcl.syntax +0 -152
  311. data/lib/kitabu/vendor/ruby1.8/uv/syntax/template_toolkit.syntax +0 -121
  312. data/lib/kitabu/vendor/ruby1.8/uv/syntax/tex.syntax +0 -86
  313. data/lib/kitabu/vendor/ruby1.8/uv/syntax/tex_math.syntax +0 -49
  314. data/lib/kitabu/vendor/ruby1.8/uv/syntax/textile.syntax +0 -215
  315. data/lib/kitabu/vendor/ruby1.8/uv/syntax/tsv.syntax +0 -50
  316. data/lib/kitabu/vendor/ruby1.8/uv/syntax/twiki.syntax +0 -241
  317. data/lib/kitabu/vendor/ruby1.8/uv/syntax/txt2tags.syntax +0 -79
  318. data/lib/kitabu/vendor/ruby1.8/uv/syntax/vectorscript.syntax +0 -57
  319. data/lib/kitabu/vendor/ruby1.8/uv/syntax/xhtml_1.0.syntax +0 -4027
  320. data/lib/kitabu/vendor/ruby1.8/uv/syntax/xml.syntax +0 -180
  321. data/lib/kitabu/vendor/ruby1.8/uv/syntax/xml_strict.syntax +0 -92
  322. data/lib/kitabu/vendor/ruby1.8/uv/syntax/xsl.syntax +0 -60
  323. data/lib/kitabu/vendor/ruby1.8/uv/syntax/yaml.syntax +0 -160
  324. data/lib/kitabu/vendor/ruby1.8/uv/syntax/yui_javascript.syntax +0 -176
  325. data/lib/kitabu/vendor/ruby1.8/uv/uv.rb +0 -88
  326. data/lib/kitabu/vendor/ruby1.8/uv/uv/render_processor.rb +0 -131
  327. data/lib/kitabu/vendor/ruby1.8/uv/uv/utility.rb +0 -67
  328. data/lib/kitabu/vendor/ruby1.9/plist/plist.rb +0 -23
  329. data/lib/kitabu/vendor/ruby1.9/plist/plist/generator.rb +0 -228
  330. data/lib/kitabu/vendor/ruby1.9/plist/plist/parser.rb +0 -225
  331. data/lib/kitabu/vendor/ruby1.9/textpow/textpow.rb +0 -10
  332. data/lib/kitabu/vendor/ruby1.9/textpow/textpow/debug_processor.rb +0 -37
  333. data/lib/kitabu/vendor/ruby1.9/textpow/textpow/score_manager.rb +0 -66
  334. data/lib/kitabu/vendor/ruby1.9/textpow/textpow/syntax.rb +0 -286
  335. data/lib/kitabu/vendor/ruby1.9/uv/render/xhtml/active4d.render +0 -140
  336. data/lib/kitabu/vendor/ruby1.9/uv/render/xhtml/all_hallows_eve.render +0 -104
  337. data/lib/kitabu/vendor/ruby1.9/uv/render/xhtml/amy.render +0 -179
  338. data/lib/kitabu/vendor/ruby1.9/uv/render/xhtml/blackboard.render +0 -119
  339. data/lib/kitabu/vendor/ruby1.9/uv/render/xhtml/brilliance_black.render +0 -560
  340. data/lib/kitabu/vendor/ruby1.9/uv/render/xhtml/brilliance_dull.render +0 -569
  341. data/lib/kitabu/vendor/ruby1.9/uv/render/xhtml/cobalt.render +0 -170
  342. data/lib/kitabu/vendor/ruby1.9/uv/render/xhtml/dawn.render +0 -134
  343. data/lib/kitabu/vendor/ruby1.9/uv/render/xhtml/eiffel.render +0 -140
  344. data/lib/kitabu/vendor/ruby1.9/uv/render/xhtml/espresso_libre.render +0 -131
  345. data/lib/kitabu/vendor/ruby1.9/uv/render/xhtml/files/css/active4d.css +0 -114
  346. data/lib/kitabu/vendor/ruby1.9/uv/render/xhtml/files/css/all_hallows_eve.css +0 -72
  347. data/lib/kitabu/vendor/ruby1.9/uv/render/xhtml/files/css/amy.css +0 -147
  348. data/lib/kitabu/vendor/ruby1.9/uv/render/xhtml/files/css/blackboard.css +0 -88
  349. data/lib/kitabu/vendor/ruby1.9/uv/render/xhtml/files/css/brilliance_black.css +0 -605
  350. data/lib/kitabu/vendor/ruby1.9/uv/render/xhtml/files/css/brilliance_dull.css +0 -599
  351. data/lib/kitabu/vendor/ruby1.9/uv/render/xhtml/files/css/cobalt.css +0 -149
  352. data/lib/kitabu/vendor/ruby1.9/uv/render/xhtml/files/css/eiffel.css +0 -121
  353. data/lib/kitabu/vendor/ruby1.9/uv/render/xhtml/files/css/espresso_libre.css +0 -109
  354. data/lib/kitabu/vendor/ruby1.9/uv/render/xhtml/files/css/iplastic.css +0 -80
  355. data/lib/kitabu/vendor/ruby1.9/uv/render/xhtml/files/css/lazy.css +0 -73
  356. data/lib/kitabu/vendor/ruby1.9/uv/render/xhtml/files/css/mac_classic.css +0 -123
  357. data/lib/kitabu/vendor/ruby1.9/uv/render/xhtml/files/css/magicwb_amiga.css +0 -104
  358. data/lib/kitabu/vendor/ruby1.9/uv/render/xhtml/files/css/pastels_on_dark.css +0 -188
  359. data/lib/kitabu/vendor/ruby1.9/uv/render/xhtml/files/css/slush_poppies.css +0 -85
  360. data/lib/kitabu/vendor/ruby1.9/uv/render/xhtml/files/css/spacecadet.css +0 -51
  361. data/lib/kitabu/vendor/ruby1.9/uv/render/xhtml/files/css/sunburst.css +0 -180
  362. data/lib/kitabu/vendor/ruby1.9/uv/render/xhtml/files/css/twilight.css +0 -137
  363. data/lib/kitabu/vendor/ruby1.9/uv/render/xhtml/files/css/zenburnesque.css +0 -91
  364. data/lib/kitabu/vendor/ruby1.9/uv/render/xhtml/idle.render +0 -101
  365. data/lib/kitabu/vendor/ruby1.9/uv/render/xhtml/iplastic.render +0 -107
  366. data/lib/kitabu/vendor/ruby1.9/uv/render/xhtml/lazy.render +0 -104
  367. data/lib/kitabu/vendor/ruby1.9/uv/render/xhtml/mac_classic.render +0 -143
  368. data/lib/kitabu/vendor/ruby1.9/uv/render/xhtml/magicwb_amiga.render +0 -125
  369. data/lib/kitabu/vendor/ruby1.9/uv/render/xhtml/pastels_on_dark.render +0 -212
  370. data/lib/kitabu/vendor/ruby1.9/uv/render/xhtml/slush_poppies.render +0 -131
  371. data/lib/kitabu/vendor/ruby1.9/uv/render/xhtml/spacecadet.render +0 -89
  372. data/lib/kitabu/vendor/ruby1.9/uv/render/xhtml/sunburst.render +0 -194
  373. data/lib/kitabu/vendor/ruby1.9/uv/render/xhtml/twilight.render +0 -161
  374. data/lib/kitabu/vendor/ruby1.9/uv/render/xhtml/zenburnesque.render +0 -134
  375. data/lib/kitabu/vendor/ruby1.9/uv/syntax/actionscript.syntax +0 -97
  376. data/lib/kitabu/vendor/ruby1.9/uv/syntax/active4d.syntax +0 -276
  377. data/lib/kitabu/vendor/ruby1.9/uv/syntax/active4d_html.syntax +0 -311
  378. data/lib/kitabu/vendor/ruby1.9/uv/syntax/active4d_ini.syntax +0 -50
  379. data/lib/kitabu/vendor/ruby1.9/uv/syntax/active4d_library.syntax +0 -21
  380. data/lib/kitabu/vendor/ruby1.9/uv/syntax/ada.syntax +0 -33
  381. data/lib/kitabu/vendor/ruby1.9/uv/syntax/antlr.syntax +0 -151
  382. data/lib/kitabu/vendor/ruby1.9/uv/syntax/apache.syntax +0 -191
  383. data/lib/kitabu/vendor/ruby1.9/uv/syntax/applescript.syntax +0 -384
  384. data/lib/kitabu/vendor/ruby1.9/uv/syntax/asp.syntax +0 -70
  385. data/lib/kitabu/vendor/ruby1.9/uv/syntax/asp_vb.net.syntax +0 -129
  386. data/lib/kitabu/vendor/ruby1.9/uv/syntax/bibtex.syntax +0 -151
  387. data/lib/kitabu/vendor/ruby1.9/uv/syntax/blog_html.syntax +0 -41
  388. data/lib/kitabu/vendor/ruby1.9/uv/syntax/blog_markdown.syntax +0 -42
  389. data/lib/kitabu/vendor/ruby1.9/uv/syntax/blog_text.syntax +0 -27
  390. data/lib/kitabu/vendor/ruby1.9/uv/syntax/blog_textile.syntax +0 -27
  391. data/lib/kitabu/vendor/ruby1.9/uv/syntax/build.syntax +0 -53
  392. data/lib/kitabu/vendor/ruby1.9/uv/syntax/bulletin_board.syntax +0 -287
  393. data/lib/kitabu/vendor/ruby1.9/uv/syntax/cake.syntax +0 -55
  394. data/lib/kitabu/vendor/ruby1.9/uv/syntax/camlp4.syntax +0 -36
  395. data/lib/kitabu/vendor/ruby1.9/uv/syntax/cm.syntax +0 -32
  396. data/lib/kitabu/vendor/ruby1.9/uv/syntax/coldfusion.syntax +0 -119
  397. data/lib/kitabu/vendor/ruby1.9/uv/syntax/context_free.syntax +0 -176
  398. data/lib/kitabu/vendor/ruby1.9/uv/syntax/cs.syntax +0 -59
  399. data/lib/kitabu/vendor/ruby1.9/uv/syntax/css.syntax +0 -195
  400. data/lib/kitabu/vendor/ruby1.9/uv/syntax/css_experimental.syntax +0 -1925
  401. data/lib/kitabu/vendor/ruby1.9/uv/syntax/csv.syntax +0 -68
  402. data/lib/kitabu/vendor/ruby1.9/uv/syntax/d.syntax +0 -142
  403. data/lib/kitabu/vendor/ruby1.9/uv/syntax/diff.syntax +0 -81
  404. data/lib/kitabu/vendor/ruby1.9/uv/syntax/dokuwiki.syntax +0 -204
  405. data/lib/kitabu/vendor/ruby1.9/uv/syntax/dot.syntax +0 -47
  406. data/lib/kitabu/vendor/ruby1.9/uv/syntax/doxygen.syntax +0 -43
  407. data/lib/kitabu/vendor/ruby1.9/uv/syntax/dylan.syntax +0 -62
  408. data/lib/kitabu/vendor/ruby1.9/uv/syntax/eiffel.syntax +0 -78
  409. data/lib/kitabu/vendor/ruby1.9/uv/syntax/erlang.syntax +0 -922
  410. data/lib/kitabu/vendor/ruby1.9/uv/syntax/f-script.syntax +0 -80
  411. data/lib/kitabu/vendor/ruby1.9/uv/syntax/fortran.syntax +0 -141
  412. data/lib/kitabu/vendor/ruby1.9/uv/syntax/fxscript.syntax +0 -142
  413. data/lib/kitabu/vendor/ruby1.9/uv/syntax/greasemonkey.syntax +0 -34
  414. data/lib/kitabu/vendor/ruby1.9/uv/syntax/gri.syntax +0 -83
  415. data/lib/kitabu/vendor/ruby1.9/uv/syntax/groovy.syntax +0 -191
  416. data/lib/kitabu/vendor/ruby1.9/uv/syntax/gtd.syntax +0 -22
  417. data/lib/kitabu/vendor/ruby1.9/uv/syntax/gtdalt.syntax +0 -143
  418. data/lib/kitabu/vendor/ruby1.9/uv/syntax/haml.syntax +0 -88
  419. data/lib/kitabu/vendor/ruby1.9/uv/syntax/haskell.syntax +0 -88
  420. data/lib/kitabu/vendor/ruby1.9/uv/syntax/html-asp.syntax +0 -27
  421. data/lib/kitabu/vendor/ruby1.9/uv/syntax/html.syntax +0 -362
  422. data/lib/kitabu/vendor/ruby1.9/uv/syntax/html_django.syntax +0 -36
  423. data/lib/kitabu/vendor/ruby1.9/uv/syntax/html_for_asp.net.syntax +0 -424
  424. data/lib/kitabu/vendor/ruby1.9/uv/syntax/html_mason.syntax +0 -119
  425. data/lib/kitabu/vendor/ruby1.9/uv/syntax/html_rails.syntax +0 -40
  426. data/lib/kitabu/vendor/ruby1.9/uv/syntax/html_tcl.syntax +0 -26
  427. data/lib/kitabu/vendor/ruby1.9/uv/syntax/icalendar.syntax +0 -32
  428. data/lib/kitabu/vendor/ruby1.9/uv/syntax/inform.syntax +0 -48
  429. data/lib/kitabu/vendor/ruby1.9/uv/syntax/ini.syntax +0 -55
  430. data/lib/kitabu/vendor/ruby1.9/uv/syntax/installer_distribution_script.syntax +0 -77
  431. data/lib/kitabu/vendor/ruby1.9/uv/syntax/io.syntax +0 -81
  432. data/lib/kitabu/vendor/ruby1.9/uv/syntax/java.syntax +0 -211
  433. data/lib/kitabu/vendor/ruby1.9/uv/syntax/javaproperties.syntax +0 -20
  434. data/lib/kitabu/vendor/ruby1.9/uv/syntax/javascript.syntax +0 -256
  435. data/lib/kitabu/vendor/ruby1.9/uv/syntax/javascript_+_prototype.syntax +0 -72
  436. data/lib/kitabu/vendor/ruby1.9/uv/syntax/javascript_+_prototype_bracketed.syntax +0 -140
  437. data/lib/kitabu/vendor/ruby1.9/uv/syntax/jquery_javascript.syntax +0 -114
  438. data/lib/kitabu/vendor/ruby1.9/uv/syntax/json.syntax +0 -136
  439. data/lib/kitabu/vendor/ruby1.9/uv/syntax/languagedefinition.syntax +0 -708
  440. data/lib/kitabu/vendor/ruby1.9/uv/syntax/latex.syntax +0 -566
  441. data/lib/kitabu/vendor/ruby1.9/uv/syntax/latex_beamer.syntax +0 -41
  442. data/lib/kitabu/vendor/ruby1.9/uv/syntax/latex_log.syntax +0 -50
  443. data/lib/kitabu/vendor/ruby1.9/uv/syntax/latex_memoir.syntax +0 -64
  444. data/lib/kitabu/vendor/ruby1.9/uv/syntax/lexflex.syntax +0 -219
  445. data/lib/kitabu/vendor/ruby1.9/uv/syntax/lighttpd.syntax +0 -54
  446. data/lib/kitabu/vendor/ruby1.9/uv/syntax/lilypond.syntax +0 -492
  447. data/lib/kitabu/vendor/ruby1.9/uv/syntax/lisp.syntax +0 -61
  448. data/lib/kitabu/vendor/ruby1.9/uv/syntax/literate_haskell.syntax +0 -24
  449. data/lib/kitabu/vendor/ruby1.9/uv/syntax/logo.syntax +0 -29
  450. data/lib/kitabu/vendor/ruby1.9/uv/syntax/logtalk.syntax +0 -152
  451. data/lib/kitabu/vendor/ruby1.9/uv/syntax/lua.syntax +0 -86
  452. data/lib/kitabu/vendor/ruby1.9/uv/syntax/m.syntax +0 -142
  453. data/lib/kitabu/vendor/ruby1.9/uv/syntax/macports_portfile.syntax +0 -163
  454. data/lib/kitabu/vendor/ruby1.9/uv/syntax/makefile.syntax +0 -36
  455. data/lib/kitabu/vendor/ruby1.9/uv/syntax/man.syntax +0 -17
  456. data/lib/kitabu/vendor/ruby1.9/uv/syntax/mediawiki.syntax +0 -567
  457. data/lib/kitabu/vendor/ruby1.9/uv/syntax/mips.syntax +0 -66
  458. data/lib/kitabu/vendor/ruby1.9/uv/syntax/mod_perl.syntax +0 -50
  459. data/lib/kitabu/vendor/ruby1.9/uv/syntax/modula-3.syntax +0 -47
  460. data/lib/kitabu/vendor/ruby1.9/uv/syntax/moinmoin.syntax +0 -189
  461. data/lib/kitabu/vendor/ruby1.9/uv/syntax/mootools.syntax +0 -572
  462. data/lib/kitabu/vendor/ruby1.9/uv/syntax/movable_type.syntax +0 -162
  463. data/lib/kitabu/vendor/ruby1.9/uv/syntax/multimarkdown.syntax +0 -39
  464. data/lib/kitabu/vendor/ruby1.9/uv/syntax/objective-c++.syntax +0 -18
  465. data/lib/kitabu/vendor/ruby1.9/uv/syntax/objective-c.syntax +0 -233
  466. data/lib/kitabu/vendor/ruby1.9/uv/syntax/ocaml.syntax +0 -764
  467. data/lib/kitabu/vendor/ruby1.9/uv/syntax/ocamllex.syntax +0 -167
  468. data/lib/kitabu/vendor/ruby1.9/uv/syntax/ocamlyacc.syntax +0 -184
  469. data/lib/kitabu/vendor/ruby1.9/uv/syntax/opengl.syntax +0 -14
  470. data/lib/kitabu/vendor/ruby1.9/uv/syntax/pascal.syntax +0 -77
  471. data/lib/kitabu/vendor/ruby1.9/uv/syntax/perl.syntax +0 -1115
  472. data/lib/kitabu/vendor/ruby1.9/uv/syntax/plain_text.syntax +0 -32
  473. data/lib/kitabu/vendor/ruby1.9/uv/syntax/postscript.syntax +0 -114
  474. data/lib/kitabu/vendor/ruby1.9/uv/syntax/processing.syntax +0 -106
  475. data/lib/kitabu/vendor/ruby1.9/uv/syntax/prolog.syntax +0 -40
  476. data/lib/kitabu/vendor/ruby1.9/uv/syntax/property_list.syntax +0 -635
  477. data/lib/kitabu/vendor/ruby1.9/uv/syntax/python.syntax +0 -868
  478. data/lib/kitabu/vendor/ruby1.9/uv/syntax/python_django.syntax +0 -21
  479. data/lib/kitabu/vendor/ruby1.9/uv/syntax/qmake_project.syntax +0 -114
  480. data/lib/kitabu/vendor/ruby1.9/uv/syntax/qt_c++.syntax +0 -26
  481. data/lib/kitabu/vendor/ruby1.9/uv/syntax/quake3_config.syntax +0 -32
  482. data/lib/kitabu/vendor/ruby1.9/uv/syntax/r.syntax +0 -81
  483. data/lib/kitabu/vendor/ruby1.9/uv/syntax/r_console.syntax +0 -16
  484. data/lib/kitabu/vendor/ruby1.9/uv/syntax/ragel.syntax +0 -201
  485. data/lib/kitabu/vendor/ruby1.9/uv/syntax/rd_r_documentation.syntax +0 -91
  486. data/lib/kitabu/vendor/ruby1.9/uv/syntax/regexp.syntax +0 -50
  487. data/lib/kitabu/vendor/ruby1.9/uv/syntax/release_notes.syntax +0 -46
  488. data/lib/kitabu/vendor/ruby1.9/uv/syntax/remind.syntax +0 -253
  489. data/lib/kitabu/vendor/ruby1.9/uv/syntax/restructuredtext.syntax +0 -250
  490. data/lib/kitabu/vendor/ruby1.9/uv/syntax/rez.syntax +0 -80
  491. data/lib/kitabu/vendor/ruby1.9/uv/syntax/ruby.syntax +0 -1035
  492. data/lib/kitabu/vendor/ruby1.9/uv/syntax/ruby_experimental.syntax +0 -145
  493. data/lib/kitabu/vendor/ruby1.9/uv/syntax/ruby_on_rails.syntax +0 -88
  494. data/lib/kitabu/vendor/ruby1.9/uv/syntax/s5.syntax +0 -69
  495. data/lib/kitabu/vendor/ruby1.9/uv/syntax/scheme.syntax +0 -347
  496. data/lib/kitabu/vendor/ruby1.9/uv/syntax/scilab.syntax +0 -41
  497. data/lib/kitabu/vendor/ruby1.9/uv/syntax/setext.syntax +0 -147
  498. data/lib/kitabu/vendor/ruby1.9/uv/syntax/shell-unix-generic.syntax +0 -384
  499. data/lib/kitabu/vendor/ruby1.9/uv/syntax/slate.syntax +0 -149
  500. data/lib/kitabu/vendor/ruby1.9/uv/syntax/sql.syntax +0 -237
  501. data/lib/kitabu/vendor/ruby1.9/uv/syntax/sql_rails.syntax +0 -18
  502. data/lib/kitabu/vendor/ruby1.9/uv/syntax/ssh-config.syntax +0 -33
  503. data/lib/kitabu/vendor/ruby1.9/uv/syntax/standard_ml.syntax +0 -121
  504. data/lib/kitabu/vendor/ruby1.9/uv/syntax/strings_file.syntax +0 -39
  505. data/lib/kitabu/vendor/ruby1.9/uv/syntax/subversion_commit_message.syntax +0 -36
  506. data/lib/kitabu/vendor/ruby1.9/uv/syntax/sweave.syntax +0 -84
  507. data/lib/kitabu/vendor/ruby1.9/uv/syntax/swig.syntax +0 -57
  508. data/lib/kitabu/vendor/ruby1.9/uv/syntax/tcl.syntax +0 -152
  509. data/lib/kitabu/vendor/ruby1.9/uv/syntax/template_toolkit.syntax +0 -121
  510. data/lib/kitabu/vendor/ruby1.9/uv/syntax/tex.syntax +0 -86
  511. data/lib/kitabu/vendor/ruby1.9/uv/syntax/tex_math.syntax +0 -49
  512. data/lib/kitabu/vendor/ruby1.9/uv/syntax/textile.syntax +0 -215
  513. data/lib/kitabu/vendor/ruby1.9/uv/syntax/tsv.syntax +0 -50
  514. data/lib/kitabu/vendor/ruby1.9/uv/syntax/twiki.syntax +0 -241
  515. data/lib/kitabu/vendor/ruby1.9/uv/syntax/txt2tags.syntax +0 -79
  516. data/lib/kitabu/vendor/ruby1.9/uv/syntax/vectorscript.syntax +0 -57
  517. data/lib/kitabu/vendor/ruby1.9/uv/syntax/xhtml_1.0.syntax +0 -4027
  518. data/lib/kitabu/vendor/ruby1.9/uv/syntax/xml.syntax +0 -180
  519. data/lib/kitabu/vendor/ruby1.9/uv/syntax/xml_strict.syntax +0 -92
  520. data/lib/kitabu/vendor/ruby1.9/uv/syntax/xsl.syntax +0 -60
  521. data/lib/kitabu/vendor/ruby1.9/uv/syntax/yaml.syntax +0 -160
  522. data/lib/kitabu/vendor/ruby1.9/uv/syntax/yui_javascript.syntax +0 -176
  523. data/lib/kitabu/vendor/ruby1.9/uv/uv.rb +0 -89
  524. data/lib/kitabu/vendor/ruby1.9/uv/uv/render_processor.rb +0 -132
  525. data/lib/kitabu/vendor/ruby1.9/uv/uv/utility.rb +0 -68
  526. data/spec/exit_matcher.rb +0 -45
  527. data/spec/fixtures/rails-guides/code/sample.rb +0 -7
  528. data/spec/kitabu/base_spec.rb +0 -159
  529. data/spec/kitabu/blackcloth_spec.rb +0 -41
  530. data/spec/kitabu/bug_fix_spec.rb +0 -25
  531. data/spec/kitabu/command_spec.rb +0 -82
  532. data/templates/Rakefile +0 -3
  533. data/templates/config.yml +0 -9
  534. data/templates/layouts/boom/layout.css +0 -469
  535. data/templates/syntax.css +0 -1065
  536. data/templates/themes/active4d.css +0 -114
  537. data/templates/themes/blackboard.css +0 -88
  538. data/templates/themes/dawn.css +0 -121
  539. data/templates/themes/eiffel.css +0 -121
  540. data/templates/themes/idle.css +0 -62
  541. data/templates/themes/iplastic.css +0 -80
  542. data/templates/themes/lazy.css +0 -73
  543. data/templates/themes/slush_poppies.css +0 -85
  544. data/templates/themes/sunburst.css +0 -180
@@ -0,0 +1,3 @@
1
+ .DS_Store
2
+ pkg
3
+ tmp
@@ -0,0 +1,3 @@
1
+ [submodule "lib/kitabu/vendor/colorize"]
2
+ path = lib/kitabu/vendor/colorize
3
+ url = git://github.com/fnando/colorize.git
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color --format documentation
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source :rubygems
2
+ gemspec
@@ -0,0 +1,62 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ kitabu (1.0.0.rc1)
5
+ RedCloth (~> 4.2)
6
+ activesupport (~> 3.0)
7
+ eeepub-with-cover-support (~> 0.8)
8
+ i18n (~> 0.5)
9
+ nokogiri (~> 1.5)
10
+ notifier (~> 0.1)
11
+ pygments.rb (~> 0.2)
12
+ rdiscount (~> 1.6)
13
+ thor (~> 0.14)
14
+ watchr (~> 0.7)
15
+
16
+ GEM
17
+ remote: http://rubygems.org/
18
+ specs:
19
+ RedCloth (4.2.9)
20
+ activesupport (3.2.3)
21
+ i18n (~> 0.6)
22
+ multi_json (~> 1.0)
23
+ blankslate (2.1.2.4)
24
+ builder (3.0.0)
25
+ diff-lcs (1.1.3)
26
+ eeepub-with-cover-support (0.8.7)
27
+ builder
28
+ rubyzip
29
+ ffi (1.0.11)
30
+ i18n (0.6.0)
31
+ multi_json (1.3.2)
32
+ nokogiri (1.5.2)
33
+ notifier (0.1.4)
34
+ pygments.rb (0.2.12)
35
+ rubypython (~> 0.5.3)
36
+ rake (0.9.2.2)
37
+ rdiscount (1.6.8)
38
+ rspec (2.9.0)
39
+ rspec-core (~> 2.9.0)
40
+ rspec-expectations (~> 2.9.0)
41
+ rspec-mocks (~> 2.9.0)
42
+ rspec-core (2.9.0)
43
+ rspec-expectations (2.9.1)
44
+ diff-lcs (~> 1.1.3)
45
+ rspec-mocks (2.9.0)
46
+ rubypython (0.5.3)
47
+ blankslate (>= 2.1.2.3)
48
+ ffi (~> 1.0.7)
49
+ rubyzip (0.9.7)
50
+ test_notifier (1.0.0)
51
+ notifier
52
+ thor (0.14.6)
53
+ watchr (0.7)
54
+
55
+ PLATFORMS
56
+ ruby
57
+
58
+ DEPENDENCIES
59
+ kitabu!
60
+ rake
61
+ rspec
62
+ test_notifier
@@ -0,0 +1,187 @@
1
+ = kitabu
2
+
3
+ While Prince is too expensive (495USD for a single user license), the free version available at http://www.princexml.com/download/ generates a PDF with a small logo on the first page, which is removed when sent to a printer.
4
+
5
+ == Features
6
+
7
+ * Write PDF using Markdown, Textile or plain HTML
8
+ * Book layout support
9
+ * Syntax highlight
10
+ * Generate a HTML, PDF and e-Pub files
11
+ * Table of Contents automatically generated from chapter titles
12
+
13
+ == Installation
14
+
15
+ To install Kitabu, you’ll need a working Ruby installation with Rubygems.
16
+ If you’re cool with it, just run the following command to install it.
17
+
18
+ gem install kitabu --pre
19
+
20
+ == Usage
21
+
22
+ To create a new e-book, just run
23
+
24
+ $ kitabu new mybook
25
+
26
+ This command creates a directory <tt>mybook</tt> with the following structure:
27
+
28
+ * mypdf
29
+ * code
30
+ * config
31
+ * kitabu.yml
32
+ * helper.rb
33
+ * images
34
+ * output
35
+ * Rakefile
36
+ * templates
37
+ * layout.css
38
+ * layout.html
39
+ * syntax.css
40
+ * user.css
41
+ * text
42
+
43
+ The <tt>config/kitabu.yml</tt> file holds some information about your book; so you'll always change it.
44
+
45
+ Now it's time to write your e-book. All your book content will be placed on the text directory. Kitabu requires you to separate your book into chapters. A chapter is nothing but a directory that holds lots of text files. The e-book will be generated using every folder/file alphabetically. So be sure to use a sequential numbering as the name. Here's a sample:
46
+
47
+ * text
48
+ * 01_Introduction
49
+ * 01_introduction.markdown
50
+ * 02_What_is_Ruby_on_Rails
51
+ * 01_MVC.html
52
+ * 02_DRY.html
53
+ * 03_Convention_Over_Configuration.markdown
54
+ * 03_Installing_Ruby_on_Rails
55
+ * 01_Installing.textile
56
+ * 02_Mac_OS_X_instructions.textile
57
+ * 03_Windows_instructions.markdown
58
+ * 04_Ubuntu_Linux_instructions.markdown
59
+
60
+ If you prefer, you can add a chapter per file:
61
+
62
+ * text
63
+ * 01_Introduction.markdown
64
+ * 02_What_is_Ruby_on_Rails.markdown
65
+ * 03_Installing_Ruby_on_Rails.markdown
66
+
67
+ Note that you can use any format you want at the same time. Just use one of the following extensions: <tt>.html</tt>, <tt>.markdown</tt>, <tt>.mkdn</tt> or <tt>.textile</tt>.
68
+
69
+ You'll want to see your progress eventually; it's time for you to generate the book PDF. Just run the command <tt>kitabu export</tt> and your book will be created on the <tt>output</tt> directory.
70
+
71
+ Kitabu can generate a Table of Contents (TOC) based on your h2-h6 tags. The h1 tag is discarded because it's meant to be the book title.
72
+
73
+ To print the TOC, you need to print a variable called +toc+, using the eRb tag.
74
+
75
+ <%= toc %>
76
+
77
+ === Syntax Highlighting
78
+
79
+ We use Ultraviolet[http://rubygems.org/gems/ultraviolet] for syntax highlighting. To highlight a given code snippet, just do something like
80
+
81
+ @@@ ruby
82
+ class User < ActiveRecord::Base
83
+ validates_presence_of :login, :password, :email
84
+ validates_uniqueness_of :login, :email
85
+ end
86
+ @@@
87
+
88
+ Run @kitabu syntaxes@ to list all available syntaxes.
89
+
90
+ <b>Don't indent</b> the source block or will end up with unexpected results.
91
+
92
+ If you want to highlight a file, you need to place it into the <tt>code</tt> directory and call it like this:
93
+
94
+ @@@ ruby some_file.rb @@@
95
+
96
+ You can specify the lines you want to highlight; the example below will highlight lines 10-17 from some_file.rb.
97
+
98
+ @@@ ruby some_file.rb:10,15 @@@
99
+
100
+ You can also specify named blocks. Named blocks are identified by <tt>@begin</tt> and <tt>@end</tt> marks. If <tt>some_file.rb</tt> has the following code
101
+
102
+ require "rubygems"
103
+ require "hpricot"
104
+ require "open"
105
+
106
+ # @begin: get_all_h2_tags
107
+ doc = Hpricot(open('http://simplesideias.com.br'))
108
+ (doc/"h2").each {|h2| puts h2.inner_text }
109
+ # @end: get_all_h2_tags
110
+
111
+ and you can get the code between <tt>get_all_h2_tags</tt> using
112
+
113
+ @@@ ruby some_file#get_all_h2_tags @@@
114
+
115
+ NOTE: Any named block annotation will be removed from <tt>@@@ ruby file.rb</tt> usage.
116
+
117
+ === Markdown Processors
118
+
119
+ By default, RDiscount[http://github.com/rtomayko/rdiscount/tree/master] is the Markdown processor. However, you can switch to different implementations by simply installing any of the following processors:
120
+
121
+ * Maruku: https://rubygems.org/gems/maruku
122
+ * PEGMarkdown: https://rubygems.org/gems/rpeg-markdown
123
+ * BlueCloth: https://rubygems.org/gems/bluecloth
124
+
125
+ === References
126
+
127
+ * Textile: http://hobix.com/textile/
128
+ * Markdown: http://daringfireball.net/projects/markdown/syntax
129
+
130
+ == Samples
131
+
132
+ I published two PDFs (portuguese only) exploring lots of features. Check it out:
133
+
134
+ * http://howtocode.com.br/downloads/rdoc.pdf
135
+ * http://howtocode.com.br/downloads/rails-application-templates.pdf
136
+
137
+ There's also an (probably) outdated Rails Guide section in the <tt>examples</tt> directory.
138
+
139
+ == Upgrading from previous version
140
+
141
+ To upgrade a e-book created with Kitabu < 1.0.0, create a new e-book.
142
+
143
+ $ kitabu new mybook
144
+
145
+ Then copy all your configuration info from <tt>config.yml</tt> to <tt>config/kitabu.yml</tt>.
146
+
147
+ Any Textile extension need to be ported to <tt>config/helper.rb</tt>. Check commented source for examples.
148
+
149
+ Move directories <tt>text</tt>, <tt>code</tt>, and <tt>images</tt> to your new e-book directory.
150
+
151
+ The last thing you need to upgrade is the syntax highlighting. You have to move from
152
+
153
+ syntax. some_file.rb
154
+
155
+ to
156
+
157
+ @@@ ruby some_file.rb @@@
158
+
159
+ Check the section "Syntax Highlighting" for detailed info.
160
+
161
+ == Maintainer
162
+
163
+ * Nando Vieira (http://nandovieira.com.br)
164
+ * Jesse Storimer (http://jstorimer.com)
165
+
166
+ == License
167
+
168
+ (The MIT License)
169
+
170
+ Permission is hereby granted, free of charge, to any person obtaining
171
+ a copy of this software and associated documentation files (the
172
+ 'Software'), to deal in the Software without restriction, including
173
+ without limitation the rights to use, copy, modify, merge, publish,
174
+ distribute, sublicense, and/or sell copies of the Software, and to
175
+ permit persons to whom the Software is furnished to do so, subject to
176
+ the following conditions:
177
+
178
+ The above copyright notice and this permission notice shall be
179
+ included in all copies or substantial portions of the Software.
180
+
181
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
182
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
183
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
184
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
185
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
186
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
187
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile CHANGED
@@ -1,37 +1,5 @@
1
- require "rake"
2
- require "jeweler"
3
- require File.dirname(__FILE__) + "/lib/kitabu"
1
+ require "bundler"
2
+ Bundler::GemHelper.install_tasks
4
3
 
5
- JEWEL = Jeweler::Tasks.new do |gem|
6
- gem.name = "kitabu"
7
- gem.version = ENV["VERSION"] || Kitabu::VERSION
8
- gem.summary = "A framework for creating e-books from Markdown/Textile text markup using Ruby."
9
- gem.description = <<-TXT
10
- A framework for creating e-books from Markdown/Textile text markup using Ruby.
11
- With Prince PDF generator, you'll be able to get high quality PDFs.
12
- TXT
13
-
14
- gem.authors = ["Nando Vieira"]
15
- gem.email = "fnando.vieira@gmail.com"
16
- gem.homepage = "http://fnando.github.com/public/kitabu.html"
17
-
18
- gem.has_rdoc = false
19
- gem.files = %w(Rakefile kitabu.gemspec VERSION README.markdown) + Dir["{bin,templates,lib}/**/*"]
20
- gem.bindir = "bin"
21
- gem.executables = "kitabu"
22
-
23
- gem.add_dependency "activesupport", ">=2.3"
24
- end
25
-
26
- desc "Build and install the gem"
27
- task :package => :build_gem do
28
- FileUtils.cp "VERSION", File.expand_path("~/Sites/github/fnando.github.com/views/version/_#{JEWEL.gemspec.name}.haml")
29
- end
30
-
31
- desc "Generate gemspec and build gem"
32
- task :build_gem do
33
- File.open("VERSION", "w+") {|f| f << (ENV["VERSION"] || Kitabu::VERSION.to_s) }
34
-
35
- Rake::Task["gemspec"].invoke
36
- Rake::Task["build"].invoke
37
- end
4
+ require "rspec/core/rake_task"
5
+ RSpec::Core::RakeTask.new
data/bin/kitabu CHANGED
@@ -1,8 +1,5 @@
1
1
  #!/usr/bin/env ruby
2
+ # -*- mode: ruby -*-
2
3
 
3
- require "rubygems"
4
4
  require "kitabu"
5
- require "kitabu/command"
6
- require "optparse"
7
-
8
- Kitabu::Command.run!
5
+ Kitabu::Cli.start
@@ -0,0 +1,29 @@
1
+ # Put any Ruby code in here.
2
+ # Here's an example on how you can extend RedCloth (Textile) with new formatters.
3
+ #
4
+ # module RedCloth
5
+ # module Formatters
6
+ # module HTML
7
+ # def attention(options)
8
+ # %[<p class="attention">#{options[:text]}</p>]
9
+ # end
10
+ # end
11
+ # end
12
+ # end
13
+ #
14
+ # Then you can just use `attention. This is an important note!` on your text.
15
+ #
16
+ # You can add inline formatters as well. The approach is slightly different,
17
+ # but pretty straightforward.
18
+ #
19
+ # Add your formatter to `RedCloth::INLINE_FORMATTERS` and implement the method, making sure that it
20
+ # replaces the content using `String#gsub!`.
21
+ #
22
+ # module RedCloth
23
+ # def my_formatter(text)
24
+ # text.gsub!(/Hello/, "Hi")
25
+ # end
26
+ # end
27
+ #
28
+ # RedCloth::INLINE_FORMATTERS << :my_formatter
29
+ #
@@ -0,0 +1,48 @@
1
+ # The book's title. Will be used everywhere!
2
+ title: "Rails Guides: Getting Started"
3
+
4
+ # The book's language.
5
+ language: en
6
+
7
+ # Your book copyright info.
8
+ # Here's some examples:
9
+ #
10
+ # Copyright 2010 by Nando Vieira.
11
+ # Copyright 2010 by Nando Vieira. This work is licensed under MIT License.
12
+ # This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.
13
+ # This work is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License.
14
+ # This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.
15
+ #
16
+ copyright: "Copyright (C) 2010 Rails Documentation Team."
17
+
18
+ # Who's publishing this book.
19
+ publisher: "Nando Vieira"
20
+
21
+ # When this book was published.
22
+ published_at: "2010-12-19"
23
+
24
+ # Some book description.
25
+ subject: "These guides are designed to make you immediately productive with Rails, and to help you understand how all of the pieces fit together."
26
+
27
+ # Some keywords that identify this book.
28
+ keywords: "Ruby on Rails, Ruby, MVC, Getting Started, PDF, HOWTO, Web Development, Framework"
29
+
30
+ # Some unique identification. Works great with your domain
31
+ # like `http://yourbook.example.com`.
32
+ uid: "b731cad288c08e875982ebc3d30f9b2d"
33
+
34
+ # Your book identification like ISBN or ISSN.
35
+ identifier:
36
+ id: "http://yourbook.example.com"
37
+ type: "URL" # can be ISBN, ISSN or URL
38
+
39
+ # This book authors.
40
+ authors:
41
+ - "Rails Documentation Team"
42
+
43
+ # The theme that will be used for source
44
+ # code syntax highlighting.
45
+ theme: idle
46
+
47
+ # The base URL from your source code.
48
+ base_url: http://example.com
@@ -0,0 +1,1556 @@
1
+ <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
2
+ "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
3
+ <head>
4
+ <title>Rails Guides: Getting Started</title>
5
+ <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
6
+ <link rel="stylesheet" type="text/css" href="../templates/layout.css"/>
7
+ <link rel="stylesheet" type="text/css" href="../templates/syntax.css"/>
8
+ <link rel="stylesheet" type="text/css" href="../templates/user.css"/>
9
+
10
+ <meta name="author" content="Rails Documentation Team" />
11
+ <meta name="subject" content="These guides are designed to make you immediately productive with Rails, and to help you understand how all of the pieces fit together." />
12
+ <meta name="keywords" content="Ruby on Rails, Ruby, MVC, Getting Started, PDF, HOWTO, Web Development, Framework" />
13
+ <meta name="date" content="2010-12-19" />
14
+ </head>
15
+ <body>
16
+ <div class="frontcover container">
17
+ <div>
18
+ <h1>Rails Guides: Getting Started</h1>
19
+ <p class="description">These guides are designed to make you immediately productive with Rails, and to help you understand how all of the pieces fit together.</p>
20
+ <p class="authors">Rails Documentation Team</p>
21
+ </div>
22
+ </div>
23
+
24
+ <div class="table-of-contents">
25
+ <h2 class="no-toc">Content</h2>
26
+ <div id="toc">
27
+ <div class="level2 guide-assumptions"><a href="#guide-assumptions"><span>Guide Assumptions</span></a></div><div class="level2 what-is-rails"><a href="#what-is-rails"><span>What is Rails?</span></a></div><div class="level3 the-mvc-architecture"><a href="#the-mvc-architecture"><span>The MVC Architecture</span></a></div><div class="level3 models"><a href="#models"><span>Models</span></a></div><div class="level3 views"><a href="#views"><span>Views</span></a></div><div class="level3 controllers"><a href="#controllers"><span>Controllers</span></a></div><div class="level3 the-components-of-rails"><a href="#the-components-of-rails"><span>The Components of Rails</span></a></div><div class="level3 action-pack"><a href="#action-pack"><span>Action Pack</span></a></div><div class="level3 action-controller"><a href="#action-controller"><span>Action Controller</span></a></div><div class="level3 action-view"><a href="#action-view"><span>Action View</span></a></div><div class="level3 action-dispatch"><a href="#action-dispatch"><span>Action Dispatch</span></a></div><div class="level3 action-mailer"><a href="#action-mailer"><span>Action Mailer</span></a></div><div class="level3 active-model"><a href="#active-model"><span>Active Model</span></a></div><div class="level3 active-record"><a href="#active-record"><span>Active Record</span></a></div><div class="level3 active-resource"><a href="#active-resource"><span>Active Resource</span></a></div><div class="level3 active-support"><a href="#active-support"><span>Active Support</span></a></div><div class="level3 railties"><a href="#railties"><span>Railties</span></a></div><div class="level3 rest"><a href="#rest"><span>REST</span></a></div><div class="level2 creating-a-new-rails-project"><a href="#creating-a-new-rails-project"><span>Creating a New Rails Project</span></a></div><div class="level3 installing-rails"><a href="#installing-rails"><span>Installing Rails</span></a></div><div class="level3 creating-the-blog-application"><a href="#creating-the-blog-application"><span>Creating the Blog Application</span></a></div><div class="level3 installing-the-required-gems"><a href="#installing-the-required-gems"><span>Installing the Required Gems</span></a></div><div class="level3 configuring-a-database"><a href="#configuring-a-database"><span>Configuring a Database</span></a></div><div class="level3 configuring-an-sqlite3-database"><a href="#configuring-an-sqlite3-database"><span>Configuring an SQLite3 Database</span></a></div><div class="level3 configuring-a-mysql-database"><a href="#configuring-a-mysql-database"><span>Configuring a MySQL Database</span></a></div><div class="level3 configuring-a-postgresql-database"><a href="#configuring-a-postgresql-database"><span>Configuring a PostgreSQL Database</span></a></div><div class="level3 creating-the-database"><a href="#creating-the-database"><span>Creating the Database</span></a></div><div class="level2 hello-rails"><a href="#hello-rails"><span>Hello, Rails!</span></a></div><div class="level3 starting-up-the-web-server"><a href="#starting-up-the-web-server"><span>Starting up the Web Server</span></a></div><div class="level3 say-hello-rails"><a href="#say-hello-rails"><span>Say “Hello”, Rails</span></a></div><div class="level3 setting-the-application-home-page"><a href="#setting-the-application-home-page"><span>Setting the Application Home Page</span></a></div><div class="level2 getting-up-and-running-quickly-with-scaffolding"><a href="#getting-up-and-running-quickly-with-scaffolding"><span>Getting Up and Running Quickly with Scaffolding</span></a></div><div class="level2 creating-a-resource"><a href="#creating-a-resource"><span>Creating a Resource</span></a></div><div class="level3 running-a-migration"><a href="#running-a-migration"><span>Running a Migration</span></a></div><div class="level3 adding-a-link"><a href="#adding-a-link"><span>Adding a Link</span></a></div><div class="level3 working-with-posts-in-the-browser"><a href="#working-with-posts-in-the-browser"><span>Working with Posts in the Browser</span></a></div><div class="level3 the-model"><a href="#the-model"><span>The Model</span></a></div><div class="level3 adding-some-validation"><a href="#adding-some-validation"><span>Adding Some Validation</span></a></div><div class="level3 using-the-console"><a href="#using-the-console"><span>Using the Console</span></a></div><div class="level3 listing-all-posts"><a href="#listing-all-posts"><span>Listing All Posts</span></a></div><div class="level3 customizing-the-layout"><a href="#customizing-the-layout"><span>Customizing the Layout</span></a></div><div class="level3 creating-new-posts"><a href="#creating-new-posts"><span>Creating New Posts</span></a></div><div class="level3 showing-an-individual-post"><a href="#showing-an-individual-post"><span>Showing an Individual Post</span></a></div><div class="level3 editing-posts"><a href="#editing-posts"><span>Editing Posts</span></a></div><div class="level4 destroying-a-post"><a href="#destroying-a-post"><span>Destroying a Post</span></a></div><div class="level2 adding-a-second-model"><a href="#adding-a-second-model"><span>Adding a Second Model</span></a></div><div class="level3 generating-a-model"><a href="#generating-a-model"><span>Generating a Model</span></a></div><div class="level3 associating-models"><a href="#associating-models"><span>Associating Models</span></a></div><div class="level3 adding-a-route-for-comments"><a href="#adding-a-route-for-comments"><span>Adding a Route for Comments</span></a></div><div class="level3 generating-a-controller"><a href="#generating-a-controller"><span>Generating a Controller</span></a></div><div class="level2 refactoring"><a href="#refactoring"><span>Refactoring</span></a></div><div class="level3 rendering-partial-collections"><a href="#rendering-partial-collections"><span>Rendering Partial Collections</span></a></div><div class="level3 rendering-a-partial-form"><a href="#rendering-a-partial-form"><span>Rendering a Partial Form</span></a></div><div class="level2 deleting-comments"><a href="#deleting-comments"><span>Deleting Comments</span></a></div><div class="level3 deleting-associated-objects"><a href="#deleting-associated-objects"><span>Deleting Associated Objects</span></a></div><div class="level2 security"><a href="#security"><span>Security</span></a></div><div class="level2 building-a-multi-model-form"><a href="#building-a-multi-model-form"><span>Building a Multi-Model Form</span></a></div><div class="level2 view-helpers"><a href="#view-helpers"><span>View Helpers</span></a></div><div class="level2 whats-next"><a href="#whats-next"><span>What’s Next?</span></a></div><div class="level2 configuration-gotchas"><a href="#configuration-gotchas"><span>Configuration Gotchas</span></a></div>
28
+ </div>
29
+ </div>
30
+
31
+ <div id="chapters">
32
+
33
+ <div class="chapter"><h2 id="guide-assumptions">Guide Assumptions</h2>
34
+
35
+ <p>This guide is designed for beginners who want to get started with a Rails application from scratch. It does not assume that you have any prior experience with Rails. However, to get the most out of it, you need to have some prerequisites installed:</p>
36
+
37
+ <ul><li>The <a href="http://www.ruby-lang.org/en/downloads">Ruby</a> language version 1.8.7 or higher</li>
38
+ <li>The <a href="http://rubyforge.org/frs/?group_id=126">RubyGems</a> packaging system</li>
39
+ <li>A working installation of the <a href="http://www.sqlite.org/">SQLite3</a> Database</li>
40
+ </ul><p>Rails is a web application framework running on the Ruby programming language. If you have no prior experience with Ruby, you will find a very steep learning curve diving straight into Rails. There are some good free resources on the internet for learning Ruby, including:</p>
41
+
42
+ <ul><li><a href="http://www.humblelittlerubybook.com/">Mr. Neighborly&#x2019;s Humble Little Ruby Book</a></li>
43
+ <li><a href="http://www.ruby-doc.org/docs/ProgrammingRuby/">Programming Ruby</a></li>
44
+ <li><a href="http://mislav.uniqpath.com/poignant-guide/">Why's (Poignant) Guide to Ruby</a></li>
45
+ </ul></div>
46
+ <div class="chapter"><h2 id="what-is-rails">What is Rails?</h2>
47
+
48
+ <p>Rails is a web application development framework written in the Ruby language. It is designed to make programming web applications easier by making assumptions about what every developer needs to get started. It allows you to write less code while accomplishing more than many other languages and frameworks. Experienced Rails developers also report that it makes web application development more fun.</p>
49
+
50
+ <p>Rails is opinionated software. It makes the assumption that there is a &#x201C;best&#x201D; way to do things, and it&#x2019;s designed to encourage that way &#x2013; and in some cases to discourage alternatives. If you learn &#x201C;The Rails Way&#x201D; you&#x2019;ll probably discover a tremendous increase in productivity. If you persist in bringing old habits from other languages to your Rails development, and trying to use patterns you learned elsewhere, you may have a less happy experience.</p>
51
+
52
+ <p>The Rails philosophy includes several guiding principles:</p>
53
+
54
+ <ul><li>DRY &#x2013; "Don't Repeat Yourself" &#x2013; suggests that writing the same code over and over again is a bad thing.</li>
55
+ <li>Convention Over Configuration &#x2013; means that Rails makes assumptions about what you want to do and how you&#x2019;re going to do it, rather than requiring you to specify every little thing through endless configuration files.</li>
56
+ <li>REST is the best pattern for web applications &#x2013; organizing your application around resources and standard HTTP verbs is the fastest way to go.</li>
57
+ </ul><h3 id="the-mvc-architecture">The MVC Architecture</h3>
58
+
59
+ <p>At the core of Rails is the Model, View, Controller architecture, usually just called MVC. MVC benefits include:</p>
60
+
61
+ <ul><li>Isolation of business logic from the user interface</li>
62
+ <li>Ease of keeping code DRY</li>
63
+ <li>Making it clear where different types of code belong for easier maintenance</li>
64
+ </ul><h3 id="models">Models</h3>
65
+
66
+ <p>A model represents the information (data) of the application and the rules to manipulate that data. In the case of Rails, models are primarily used for managing the rules of interaction with a corresponding database table. In most cases, one table in your database will correspond to one model in your application. The bulk of your application&#x2019;s business logic will be concentrated in the models.</p>
67
+
68
+ <h3 id="views">Views</h3>
69
+
70
+ <p>Views represent the user interface of your application. In Rails, views are often HTML files with embedded Ruby code that perform tasks related solely to the presentation of the data. Views handle the job of providing data to the web browser or other tool that is used to make requests from your application.</p>
71
+
72
+ <h3 id="controllers">Controllers</h3>
73
+
74
+ <p>Controllers provide the &#x201C;glue&#x201D; between models and views. In Rails, controllers are responsible for processing the incoming requests from the web browser, interrogating the models for data, and passing that data on to the views for presentation.</p>
75
+
76
+ <h3 id="the-components-of-rails">The Components of Rails</h3>
77
+
78
+ <p>Rails ships as many individual components.</p>
79
+
80
+ <ul><li>Action Pack
81
+
82
+ <ul><li>Action Controller</li>
83
+ <li>Action Dispatch</li>
84
+ <li>Action View</li>
85
+ </ul></li>
86
+ <li>Action Mailer</li>
87
+ <li>Active Model</li>
88
+ <li>Active Record</li>
89
+ <li>Active Resource</li>
90
+ <li>Active Support</li>
91
+ <li>Railties</li>
92
+ </ul><h3 id="action-pack">Action Pack</h3>
93
+
94
+ <p>Action Pack is a single gem that contains Action Controller, Action View and Action Dispatch. The "VC" part of "MVC".</p>
95
+
96
+ <h3 id="action-controller">Action Controller</h3>
97
+
98
+ <p>Action Controller is the component that manages the controllers in a Rails application. The Action Controller framework processes incoming requests to a Rails application, extracts parameters, and dispatches them to the intended action. Services provided by Action Controller include session management, template rendering, and redirect management.</p>
99
+
100
+ <h3 id="action-view">Action View</h3>
101
+
102
+ <p>Action View manages the views of your Rails application. It can create both HTML and XML output by default. Action View manages rendering templates, including nested and partial templates, and includes built-in AJAX support.</p>
103
+
104
+ <h3 id="action-dispatch">Action Dispatch</h3>
105
+
106
+ <p>Action Dispatch handles routing of web requests and dispatches them as you want, either to your application or any other Rack application.</p>
107
+
108
+ <h3 id="action-mailer">Action Mailer</h3>
109
+
110
+ <p>Action Mailer is a framework for building e-mail services. You can use Action Mailer to receive and process incoming email and send simple plain text or complex multipart emails based on flexible templates.</p>
111
+
112
+ <h3 id="active-model">Active Model</h3>
113
+
114
+ <p>Active Model provides a defined interface between the Action Pack gem services and Object Relationship Mapping gems such as Active Record. Active Model allows Rails to utilize other ORM frameworks in place of Active Record if your application needs this.</p>
115
+
116
+ <h3 id="active-record">Active Record</h3>
117
+
118
+ <p>Active Record is the base for the models in a Rails application. It provides database independence, basic CRUD functionality, advanced finding capabilities, and the ability to relate models to one another, among other services.</p>
119
+
120
+ <h3 id="active-resource">Active Resource</h3>
121
+
122
+ <p>Active Resource provides a framework for managing the connection between business objects and RESTful web services. It implements a way to map web-based resources to local objects with CRUD semantics.</p>
123
+
124
+ <h3 id="active-support">Active Support</h3>
125
+
126
+ <p>Active Support is an extensive collection of utility classes and standard Ruby library extensions that are used in the Rails, both by the core code and by your applications.</p>
127
+
128
+ <h3 id="railties">Railties</h3>
129
+
130
+ <p>Railties is the core Rails code that builds new Rails applications and glues the various frameworks and plugins together in any Rails application.</p>
131
+
132
+ <h3 id="rest">REST</h3>
133
+
134
+ <p>Rest stands for Representational State Transfer and is the foundation of the RESTful architecture. This is generally considered to be Roy Fielding&#x2019;s doctoral thesis, <a href="http://www.ics.uci.edu/~fielding/pubs/dissertation/top.htm">Architectural Styles and the Design of Network-based Software Architectures</a>. While you can read through the thesis, REST in terms of Rails boils down to two main principles:</p>
135
+
136
+ <ul><li>Using resource identifiers such as URLs to represent resources.</li>
137
+ <li>Transferring representations of the state of that resource between system components.</li>
138
+ </ul><p>For example, to a Rails application a request such as this:</p>
139
+
140
+ <pre><code>DELETE /photos/17
141
+ </code></pre>
142
+
143
+ <p>would be understood to refer to a photo resource with the ID of 17, and to indicate a desired action &#x2013; deleting that resource. REST is a natural style for the architecture of web applications, and Rails hooks into this shielding you from many of the RESTful complexities and browser quirks.</p>
144
+
145
+ <p>If you&#x2019;d like more details on REST as an architectural style, these resources are more approachable than Fielding&#x2019;s thesis:</p>
146
+
147
+ <ul><li><a href="http://www.infoq.com/articles/rest-introduction">A Brief Introduction to REST</a> by Stefan Tilkov</li>
148
+ <li><a href="http://bitworking.org/news/373/An-Introduction-to-REST">An Introduction to REST</a> (video tutorial) by Joe Gregorio</li>
149
+ <li><a href="http://en.wikipedia.org/wiki/Representational_State_Transfer">Representational State Transfer</a> article in Wikipedia</li>
150
+ <li><a href="http://www.infoq.com/articles/webber-rest-workflow">How to GET a Cup of Coffee</a> by Jim Webber, Savas Parastatidis &amp; Ian Robinson</li>
151
+ </ul></div>
152
+ <div class="chapter"><h2 id="creating-a-new-rails-project">Creating a New Rails Project</h2>
153
+
154
+ <p>If you follow this guide, you&#x2019;ll create a Rails project called blog, a (very) simple weblog. Before you can start building the application, you need to make sure that you have Rails itself installed.</p>
155
+
156
+ <h3 id="installing-rails">Installing Rails</h3>
157
+
158
+ <p>In most cases, the easiest way to install Rails is to take advantage of RubyGems:</p>
159
+
160
+ <pre><code>Usually run this as the root user:
161
+ # gem install rails
162
+ </code></pre>
163
+
164
+ <p class="notice">
165
+ If you&#x2019;re working on Windows, you should be aware that the vast majority of Rails development is done in Unix environments. While Ruby and Rails themselves install easily using for example Ruby Installer, the supporting ecosystem often assumes you are able to build C-based rubygems and work in a command window. If at all possible, we suggest that you install a Linux virtual machine and use that for Rails development, instead of using Windows.
166
+ </p>
167
+
168
+
169
+ <h3 id="creating-the-blog-application">Creating the Blog Application</h3>
170
+
171
+ <p>The best way to use this guide is to follow each step as it happens, no code or step needed to make this example application has been left out, so you can literally follow along step by step. If you need to see the completed code, you can download it from <a href="http://github.com/mikel/getting-started-code">Getting Started Code</a>.</p>
172
+
173
+ <p>To begin, open a terminal, navigate to a folder where you have rights to create files, and type:</p>
174
+
175
+ <pre><code>$ rails new blog
176
+ </code></pre>
177
+
178
+ <p>This will create a Rails application called Blog in a directory called blog.</p>
179
+
180
+ <p class="notice">
181
+ You can see all of the switches that the Rails application builder accepts by running <code>rails -h</code>.
182
+ </p>
183
+
184
+
185
+ <p>After you create the blog application, switch to its folder to continue work directly in that application:</p>
186
+
187
+ <pre><code>$ cd blog
188
+ </code></pre>
189
+
190
+ <p>In any case, Rails will create a folder in your working directory called <code>blog</code>. Open up that folder and explore its contents. Most of the work in this tutorial will happen in the <code>app/</code> folder, but here&#x2019;s a basic rundown on the function of each folder that Rails creates in a new application by default:</p>
191
+
192
+ <table><thead><tr><th>File/Folder </th>
193
+ <th>Purpose</th>
194
+ </tr></thead><tbody><tr><td>Gemfile</td>
195
+ <td>This file allows you to specify what gem dependencies are needed for your Rails application.</td>
196
+ </tr><tr><td>README.rdoc</td>
197
+ <td>This is a brief instruction manual for your application. Use it to tell others what your application does, how to set it up, and so on.</td>
198
+ </tr><tr><td>Rakefile</td>
199
+ <td>This file contains batch jobs that can be run from the terminal.</td>
200
+ </tr><tr><td>app/</td>
201
+ <td>Contains the controllers, models, and views for your application. You&#x2019;ll focus on this folder for the remainder of this guide.</td>
202
+ </tr><tr><td>config/</td>
203
+ <td>Configure your application&#x2019;s runtime rules, routes, database, and more.</td>
204
+ </tr><tr><td>config.ru</td>
205
+ <td>Rack configuration for Rack based servers used to start the application.</td>
206
+ </tr><tr><td>db/</td>
207
+ <td>Shows your current database schema, as well as the database migrations. You&#x2019;ll learn about migrations shortly.</td>
208
+ </tr><tr><td>doc/</td>
209
+ <td>In-depth documentation for your application.</td>
210
+ </tr><tr><td>lib/</td>
211
+ <td>Extended modules for your application (not covered in this guide).</td>
212
+ </tr><tr><td>log/</td>
213
+ <td>Application log files.</td>
214
+ </tr><tr><td>public/</td>
215
+ <td>The only folder seen to the world as-is. This is where your images, javascript, stylesheets (CSS), and other static files go.</td>
216
+ </tr><tr><td>script/</td>
217
+ <td>Contains the rails script that starts your app and can contain other scripts you use to deploy or run your application.</td>
218
+ </tr><tr><td>test/</td>
219
+ <td>Unit tests, fixtures, and other test apparatus.</td>
220
+ </tr><tr><td>tmp/</td>
221
+ <td>Temporary files.</td>
222
+ </tr><tr><td>vendor/</td>
223
+ <td>A place for all third-party code. In a typical Rails application, this includes Ruby Gems, the Rails source code (if you install it into your project) and plugins containing additional prepackaged functionality.</td>
224
+ </tr></tbody></table><h3 id="installing-the-required-gems">Installing the Required Gems</h3>
225
+
226
+ <p>Rails applications manage gem dependencies with <a href="http://www.github.com/carlhuda/bundler">Bundler</a> by default. As we don&#x2019;t need any other gems beyond the ones in the generated <code>Gemfile</code> we can directly run</p>
227
+
228
+ <pre><code>bundle install
229
+ </code></pre>
230
+
231
+ <p>to have them ready.</p>
232
+
233
+ <h3 id="configuring-a-database">Configuring a Database</h3>
234
+
235
+ <p>Just about every Rails application will interact with a database. The database to use is specified in a configuration file, <code>config/database.yml</code>. If you open this file in a new Rails application, you&#x2019;ll see a default database configuration using SQLite3. The file contains sections for three different environments in which Rails can run by default:</p>
236
+
237
+ <ul><li>The <code>development</code> environment is used on your development computer as you interact manually with the application</li>
238
+ <li>The <code>test</code> environment is used to run automated tests</li>
239
+ <li>The <code>production</code> environment is used when you deploy your application for the world to use.</li>
240
+ </ul><h3 id="configuring-an-sqlite3-database">Configuring an SQLite3 Database</h3>
241
+
242
+ <p>Rails comes with built-in support for <a href="http://www.sqlite.org/">SQLite3</a>, which is a lightweight serverless database application. While a busy production environment may overload SQLite, it works well for development and testing. Rails defaults to using an SQLite database when creating a new project, but you can always change it later.</p>
243
+
244
+ <p>Here&#x2019;s the section of the default configuration file (<code>config/database.yml</code>) with connection information for the development environment:</p>
245
+
246
+ <pre class="idle"><span class="TagName">development</span>:
247
+ <span class="String"><span class="TagName">adapter<span class="TagName">:</span></span> <span class="String">sqlite3</span></span>
248
+ <span class="String"><span class="TagName">database<span class="TagName">:</span></span> <span class="String">db/development.sqlite3</span></span>
249
+ <span class="Number"><span class="TagName">pool<span class="TagName">:</span></span> 5</span>
250
+ <span class="Number"><span class="TagName">timeout<span class="TagName">:</span></span> 5000</span>
251
+ </pre>
252
+
253
+
254
+
255
+
256
+ <p class="notice">
257
+ In this guide we are using an SQLite3 database for data storage, because it is a zero configuration database that just works. Rails also supports MySQL and PostgreSQL &#x201C;out of the box&#x201D;, and has plugins for many database systems. If you are using a database in a production environment Rails most likely has an adapter for it.
258
+ </p>
259
+
260
+
261
+ <h3 id="configuring-a-mysql-database">Configuring a MySQL Database</h3>
262
+
263
+ <p>If you choose to use MySQL instead of the shipped Sqlite3 database, your <code>config/database.yml</code> will look a little different. Here&#x2019;s the development section:</p>
264
+
265
+ <pre class="idle"><span class="TagName">development</span>:
266
+ <span class="String"><span class="TagName">adapter<span class="TagName">:</span></span> <span class="String">mysql2</span></span>
267
+ <span class="String"><span class="TagName">encoding<span class="TagName">:</span></span> <span class="String">utf8</span></span>
268
+ <span class="String"><span class="TagName">database<span class="TagName">:</span></span> <span class="String">blog_development</span></span>
269
+ <span class="Number"><span class="TagName">pool<span class="TagName">:</span></span> 5</span>
270
+ <span class="String"><span class="TagName">username<span class="TagName">:</span></span> <span class="String">root</span></span>
271
+ <span class="TagName">password</span>:
272
+ <span class="TagName">socket</span>: /tmp/mysql.sock
273
+ </pre>
274
+
275
+
276
+ <p>If your development computer&#x2019;s MySQL installation includes a root user with an empty password, this configuration should work for you. Otherwise, change the username and password in the <code>development</code> section as appropriate.</p>
277
+
278
+ <h3 id="configuring-a-postgresql-database">Configuring a PostgreSQL Database</h3>
279
+
280
+ <p>Finally if you choose to use PostgreSQL, your config/database.yml will be customized to use PostgreSQL databases:</p>
281
+
282
+ <pre class="idle"><span class="TagName">development</span>:
283
+ <span class="String"><span class="TagName">adapter<span class="TagName">:</span></span> <span class="String">postgresql</span></span>
284
+ <span class="String"><span class="TagName">encoding<span class="TagName">:</span></span> <span class="String">unicode</span></span>
285
+ <span class="String"><span class="TagName">database<span class="TagName">:</span></span> <span class="String">blog_development</span></span>
286
+ <span class="Number"><span class="TagName">pool<span class="TagName">:</span></span> 5</span>
287
+ <span class="String"><span class="TagName">username<span class="TagName">:</span></span> <span class="String">blog</span></span>
288
+ <span class="TagName">password</span>:
289
+ </pre>
290
+
291
+
292
+ <p>Change the username and password in the <code>development</code> section as appropriate.</p>
293
+
294
+ <h3 id="creating-the-database">Creating the Database</h3>
295
+
296
+ <p>Now that you have your database configured, it&#x2019;s time to have Rails create an empty database for you. You can do this by running a rake command:</p>
297
+
298
+ <pre><code>$ rake db:create
299
+ </code></pre>
300
+
301
+ <p>This will create your development and test SQLite3 databases inside the <code>db/</code> folder.</p>
302
+
303
+ <p class="notice">
304
+ Rake is a general-purpose command-runner that Rails uses for many things. You can see the list of available rake commands in your application by running <code>rake -T</code>.
305
+ </p>
306
+
307
+
308
+
309
+ </div>
310
+ <div class="chapter"><h2 id="hello-rails">Hello, Rails!</h2>
311
+
312
+ <p>One of the traditional places to start with a new language is by getting some text up on screen quickly, to do this, you need to get your Rails application server running.</p>
313
+
314
+ <h3 id="starting-up-the-web-server">Starting up the Web Server</h3>
315
+
316
+ <p class="figure">
317
+ <img src="../images/rails_welcome.png" alt="Rails Welcome page" /></p>
318
+
319
+
320
+ <p>You actually have a functional Rails application already. To see it, you need to start a web server on your development machine. You can do this by running:</p>
321
+
322
+ <pre><code>$ rails server
323
+ </code></pre>
324
+
325
+ <p>This will fire up an instance of the Mongrel web server by default (Rails can also use several other web servers). To see your application in action, open a browser window and navigate to <a href="http://localhost:3000">http://localhost:3000</a>. You should see Rails&#x2019; default information page:</p>
326
+
327
+ <p>To stop the web server, hit <code>Ctrl+C</code> in the terminal window where it&#x2019;s running. In development mode, Rails does not generally require you to stop the server; changes you make in files will be automatically picked up by the server.</p>
328
+
329
+ <p>The &#x201C;Welcome Aboard&#x201D; page is the <em>smoke test</em> for a new Rails application: it makes sure that you have your software configured correctly enough to serve a page. You can also click on the <em>About your application&#x2019;s environment</em> link to see a summary of your Application&#x2019;s environment.</p>
330
+
331
+ <h3 id="say-hello-rails">Say &#x201C;Hello&#x201D;, Rails</h3>
332
+
333
+ <p>To get Rails saying &#x201C;Hello&#x201D;, you need to create at minimum a controller and a view. Fortunately, you can do that in a single command. Enter this command in your terminal:</p>
334
+
335
+ <pre><code>$ rails generate controller home index
336
+ </code></pre>
337
+
338
+ <p class="notice">
339
+ If you&#x2019;re on Windows, or your Ruby is set up in some non-standard fashion, you may need to explicitly pass Rails <code>rails</code> commands to Ruby: <code>ruby \path\to\rails controller home index</code>.
340
+ </p>
341
+
342
+
343
+ <p>Rails will create several files for you, including <code>app/views/home/index.html.erb</code>. This is the template that will be used to display the results of the index action (method) in the home controller. Open this file in your text editor and edit it to contain a single line of code:</p>
344
+
345
+ <pre class="idle">&lt;<span class="TagName">h1</span>&gt;Hello, Rails!&lt;/<span class="TagName">h1</span>&gt;
346
+ </pre>
347
+
348
+
349
+ <h3 id="setting-the-application-home-page">Setting the Application Home Page</h3>
350
+
351
+ <p>Now that we have made the controller and view, we need to tell Rails when we want &#x201C;Hello Rails&#x201D; to show up. In our case, we want it to show up when we navigate to the root URL of our site, <a href="http://localhost:3000">http://localhost:3000</a>, instead of the &#x201C;Welcome Aboard&#x201D; smoke test.</p>
352
+
353
+ <p>The first step to doing this is to delete the default page from your application:</p>
354
+
355
+ <pre><code>$ rm public/index.html
356
+ </code></pre>
357
+
358
+ <p>We need to do this as Rails will deliver any static file in the <code>public</code> directory in preference to any dynamic contact we generate from the controllers.</p>
359
+
360
+ <p>Now, you have to tell Rails where your actual home page is located. Open the file <code>config/routes.rb</code> in your editor. This is your application&#x2019;s routing file which holds entries in a special DSL (domain-specific language) that tells Rails how to connect incoming requests to controllers and actions. This file contains many sample routes on commented lines, and one of them actually shows you how to connect the root of your site to a specific controller and action. Find the line beginning with :root to, uncomment it and change it like the following:</p>
361
+
362
+ <pre class="idle"><span class="LibraryClassType">Blog</span>::<span class="FunctionName">Application</span>.<span class="FunctionName">routes</span>.<span class="FunctionName">draw</span> <span class="Keyword">do</span>
363
+
364
+ <span class="Comment"> <span class="Comment">#</span>...</span>
365
+ <span class="Comment"> <span class="Comment">#</span> You can have the root of your site routed with "root"</span>
366
+ <span class="Comment"> <span class="Comment">#</span> just remember to delete public/index.html.</span>
367
+ root <span class="UserDefinedConstant"><span class="UserDefinedConstant">:</span>to</span> =&gt; <span class="String"><span class="String">"</span>home#index<span class="String">"</span></span>
368
+ </pre>
369
+
370
+
371
+ <p>The <code>root :to =&gt; "home#index"</code> tells Rails to map the root action to the home controller&#x2019;s index action.</p>
372
+
373
+ <p>Now if you navigate to <a href="http://localhost:3000">http://localhost:3000</a> in your browser, you&#x2019;ll see <code>Hello, Rails!</code>.</p>
374
+
375
+ <p>For more information about routing, refer to <a href="http://guides.rubyonrails.org/routing.html">Rails Routing from the Outside In</a>.</p>
376
+
377
+
378
+ </div>
379
+ <div class="chapter"><h2 id="getting-up-and-running-quickly-with-scaffolding">Getting Up and Running Quickly with Scaffolding</h2>
380
+
381
+ <p>Rails <em>scaffolding</em> is a quick way to generate some of the major pieces of an application. If you want to create the models, views, and controllers for a new resource in a single operation, scaffolding is the tool for the job.</p>
382
+
383
+
384
+ </div>
385
+ <div class="chapter"><h2 id="creating-a-resource">Creating a Resource</h2>
386
+
387
+ <p>In the case of the blog application, you can start by generating a scaffolded Post resource: this will represent a single blog posting. To do this, enter this command in your terminal:</p>
388
+
389
+ <pre><code>$ rails generate scaffold Post name:string title:string content:text
390
+ </code></pre>
391
+
392
+ <p class="notice">
393
+ While scaffolding will get you up and running quickly, the &#x201C;one size fits all&#x201D; code that it generates is unlikely to be a perfect fit for your application. In most cases, you&#x2019;ll need to customize the generated code. Many experienced Rails developers avoid scaffolding entirely, preferring to write all or most of their source code from scratch.
394
+ </p>
395
+
396
+
397
+ <p>The scaffold generator will build 15 files in your application, along with some folders, and edit one more. Here&#x2019;s a quick overview of what it creates:</p>
398
+
399
+ <table><thead><tr><th>File</th>
400
+ <th>Purpose</th>
401
+ </tr></thead><tbody><tr><td>db/migrate/20100207214725_create_posts.rb.rb</td>
402
+ <td>Migration to create the posts table in your database (your name will include a different timestamp)</td>
403
+ </tr><tr><td>app/models/post.rb</td>
404
+ <td>The Post model</td>
405
+ </tr><tr><td>test/fixtures/posts.yml</td>
406
+ <td>Dummy posts for use in testing</td>
407
+ </tr><tr><td>app/controllers/posts_controller.rb</td>
408
+ <td>The Posts controller</td>
409
+ </tr><tr><td>app/views/posts/index.html.erb</td>
410
+ <td>A view to display an index of all posts</td>
411
+ </tr><tr><td>db/migrate/20100207214725_create_posts.rb</td>
412
+ <td>Migration to create the posts table in your database (your name will include a different timestamp)
413
+ </td>
414
+ </tr><tr><td>app/views/posts/edit.html.erb</td>
415
+ <td>A view to edit an existing post</td>
416
+ </tr><tr><td>app/views/posts/show.html.erb</td>
417
+ <td>A view to display a single post</td>
418
+ </tr><tr><td>app/views/posts/_form.html.erb</td>
419
+ <td>A partial to control the overall look and feel of the form used in edit and new views</td>
420
+ </tr><tr><td>app/helpers/posts_helper.rb</td>
421
+ <td>Helper functions to be used from the post views</td>
422
+ </tr><tr><td>test/unit/post_test.rb</td>
423
+ <td>Unit testing harness for the posts model</td>
424
+ </tr><tr><td>test/functional/posts_controller_test.rb</td>
425
+ <td>Functional testing harness for the posts controller</td>
426
+ </tr><tr><td>test/unit/helpers/posts_helper_test.rb</td>
427
+ <td>Unit testing harness for the posts helper</td>
428
+ </tr><tr><td>config/routes.rb</td>
429
+ <td>Edited to include routing information for posts</td>
430
+ </tr><tr><td>public/stylesheets/scaffold.css</td>
431
+ <td>Cascading style sheet to make the scaffolded views look better</td>
432
+ </tr></tbody></table><h3 id="running-a-migration">Running a Migration</h3>
433
+
434
+ <p>One of the products of the <code>rails generate scaffold</code> command is a database <em>migration</em>. Migrations are Ruby classes that are designed to make it simple to create and modify database tables. Rails uses rake commands to run migrations, and it&#x2019;s possible to undo a migration after it&#x2019;s been applied to your database. Migration filenames include a timestamp to ensure that they&#x2019;re processed in the order that they were created.</p>
435
+
436
+ <p>If you look in the <code>db/migrate/20100207214725_create_posts.rb</code> file (remember, yours will have a slightly different name), here&#x2019;s what you&#x2019;ll find:</p>
437
+
438
+ <pre class="idle"><span class="Keyword">class</span> <span class="TypeName">CreatePosts<span class="InheritedClass"> <span class="InheritedClass">&lt;</span> ActiveRecord::Migration</span></span>
439
+ <span class="Keyword">def</span> <span class="FunctionName">self.up</span>
440
+ create_table <span class="UserDefinedConstant"><span class="UserDefinedConstant">:</span>posts</span> <span class="Keyword">do </span>|<span class="Variable">t</span>|
441
+ t.<span class="FunctionName">string</span> <span class="UserDefinedConstant"><span class="UserDefinedConstant">:</span>name</span>
442
+ t.<span class="FunctionName">string</span> <span class="UserDefinedConstant"><span class="UserDefinedConstant">:</span>title</span>
443
+ t.<span class="FunctionName">text</span> <span class="UserDefinedConstant"><span class="UserDefinedConstant">:</span>content</span>
444
+
445
+ t.<span class="FunctionName">timestamps</span>
446
+ <span class="Keyword">end</span>
447
+ <span class="Keyword">end</span>
448
+
449
+ <span class="Keyword">def</span> <span class="FunctionName">self.down</span>
450
+ drop_table <span class="UserDefinedConstant"><span class="UserDefinedConstant">:</span>posts</span>
451
+ <span class="Keyword">end</span>
452
+ <span class="Keyword">end</span>
453
+ </pre>
454
+
455
+
456
+ <p>The above migration creates two methods, <code>up</code>, called when you run this migration into the database, and <code>down</code> in case you need to reverse the changes made by this migration at a later date. The up command in this case creates a posts table with two string columns and a text column. It also creates two timestamp fields to track record creation and updating. More information about Rails migrations can be found in the <a href="http://guides.rubyonrails.org/migrations.html">Rails Database Migrations</a> guide.</p>
457
+
458
+ <p>At this point, you can use a rake command to run the migration:</p>
459
+
460
+ <pre><code>$ rake db:migrate
461
+ </code></pre>
462
+
463
+ <p>Rails will execute this migration command and tell you it created the Posts table.</p>
464
+
465
+ <pre><code>== CreatePosts: migrating ===========
466
+ -- create_table(:posts)
467
+ -&gt; 0.0019s
468
+ == CreatePosts: migrated (0.0020s) ==
469
+ </code></pre>
470
+
471
+ <p class="notice">
472
+ Because you&#x2019;re working in the development environment by default, this command will apply to the database defined in the <code>development</code> section of your <code>config/database.yml</code> file.
473
+ </p>
474
+
475
+
476
+ <h3 id="adding-a-link">Adding a Link</h3>
477
+
478
+ <p>To hook the posts up to the home page you&#x2019;ve already created, you can add a link to the home page. Open <code>app/views/home/index.html.erb</code> and modify it as follows:</p>
479
+
480
+ <pre><code>&lt;h1&gt;Hello, Rails!&lt;/h1&gt; &lt;%= link_to "My Blog", posts_path %&gt;
481
+ </code></pre>
482
+
483
+ <p>The <code>link_to</code> method is one of Rails&#x2019; built-in view helpers. It creates a hyperlink based on text to display and where to go &#x2013; in this case, to the path for posts.</p>
484
+
485
+ <h3 id="working-with-posts-in-the-browser">Working with Posts in the Browser</h3>
486
+
487
+ <p class="figure">
488
+ <img src="../images/posts_index.png" alt="Post index page" /></p>
489
+
490
+
491
+ <p>Now you&#x2019;re ready to start working with posts. To do that, navigate to <a href="http://localhost:3000">http://localhost:3000</a> and then click the &#x201C;My Blog&#x201D; link:</p>
492
+
493
+ <p>This is the result of Rails rendering the <code>index</code> view of your posts. There aren&#x2019;t currently any posts in the database, but if you click the <code>New Post</code> link you can create one. After that, you&#x2019;ll find that you can edit posts, look at their details, or destroy them. All of the logic and HTML to handle this was built by the single <code>rails generate scaffold</code> command.</p>
494
+
495
+ <p class="notice">
496
+ In development mode (which is what you&#x2019;re working in by default), Rails reloads your application with every browser request, so there&#x2019;s no need to stop and restart the web server.
497
+ </p>
498
+
499
+
500
+ <p>Congratulations, you&#x2019;re riding the rails! Now it&#x2019;s time to see how it all works.</p>
501
+
502
+ <h3 id="the-model">The Model</h3>
503
+
504
+ <p>The model file, <code>app/models/post.rb</code> is about as simple as it can get:</p>
505
+
506
+ <pre class="idle"><span class="Keyword">class</span> <span class="TypeName">Post<span class="InheritedClass"> <span class="InheritedClass">&lt;</span> ActiveRecord::Base</span></span>
507
+ <span class="Keyword">end</span>
508
+ </pre>
509
+
510
+
511
+ <p>There isn&#x2019;t much to this file &#x2013; but note that the <code>Post</code> class inherits from <code>ActiveRecord::Base</code>. Active Record supplies a great deal of functionality to your Rails models for free, including basic database CRUD (Create, Read, Update, Destroy) operations, data validation, as well as sophisticated search support and the ability to relate multiple models to one another.</p>
512
+
513
+ <h3 id="adding-some-validation">Adding Some Validation</h3>
514
+
515
+ <p>Rails includes methods to help you validate the data that you send to models. Open the <code>app/models/post.rb</code> file and edit it:</p>
516
+
517
+ <pre class="idle"><span class="Keyword">class</span> <span class="TypeName">Post<span class="InheritedClass"> <span class="InheritedClass">&lt;</span> ActiveRecord::Base</span></span>
518
+ validates <span class="UserDefinedConstant"><span class="UserDefinedConstant">:</span>name</span>, <span class="UserDefinedConstant"><span class="UserDefinedConstant">:</span>presence</span> =&gt; <span class="BuiltInConstant">true</span>
519
+ validates <span class="UserDefinedConstant"><span class="UserDefinedConstant">:</span>title</span>, <span class="UserDefinedConstant"><span class="UserDefinedConstant">:</span>presence</span> =&gt; <span class="BuiltInConstant">true</span>,
520
+ <span class="UserDefinedConstant"><span class="UserDefinedConstant">:</span>length</span> =&gt; { <span class="UserDefinedConstant"><span class="UserDefinedConstant">:</span>minimum</span> =&gt; <span class="Number">5</span> }
521
+ <span class="Keyword">end</span>
522
+ </pre>
523
+
524
+
525
+ <p>These changes will ensure that all posts have a name and a title, and that the title is at least five characters long. Rails can validate a variety of conditions in a model, including the presence or uniqueness of columns, their format, and the existence of associated objects.</p>
526
+
527
+ <h3 id="using-the-console">Using the Console</h3>
528
+
529
+ <p>To see your validations in action, you can use the console. The console is a command-line tool that lets you execute Ruby code in the context of your application:</p>
530
+
531
+ <pre><code>$ rails console
532
+ </code></pre>
533
+
534
+ <p>After the console loads, you can use it to work with your application&#x2019;s models:</p>
535
+
536
+ <pre class="idle"><code>&gt;&gt; p = Post.new(:content =&gt; "A new post")
537
+ =&gt; #&lt;Post id: nil, name: nil, title: nil,
538
+ content: "A new post", created_at: nil,
539
+ updated_at: nil&gt;
540
+ &gt;&gt; p.save
541
+ =&gt; false
542
+ &gt;&gt; p.errors
543
+ =&gt; #&lt;OrderedHash { :title=&gt;["can't be blank",
544
+ "is too short (minimum is 5 characters)"],
545
+ :name=&gt;["can't be blank"] }&gt;</code></pre>
546
+
547
+
548
+ <p>This code shows creating a new <code>Post</code> instance, attempting to save it and getting <code>false</code> for a return value (indicating that the save failed), and inspecting the errors of the post.</p>
549
+
550
+ <p>When you&#x2019;re finished, type <code>exit</code> and hit <code>return</code> to exit the console.</p>
551
+
552
+ <p class="notice">
553
+ Unlike the development web server, the console does not automatically load your code afresh for each line. If you make changes to your models while the console is open, type reload! at the console prompt to load them.
554
+ </p>
555
+
556
+
557
+ <h3 id="listing-all-posts">Listing All Posts</h3>
558
+
559
+ <p>The easiest place to start looking at functionality is with the code that lists all posts. Open the file <code>app/controllers/posts_controller.rb</code> and look at the <code>index</code> action:</p>
560
+
561
+ <pre class="idle"><span class="Keyword">def</span> <span class="FunctionName">index</span>
562
+ <span class="Variable"><span class="Variable">@</span>posts</span> <span class="Keyword">=</span> <span class="LibraryClassType">Post</span>.<span class="FunctionName">all</span>
563
+
564
+ respond_to <span class="Keyword">do </span>|<span class="Variable">format</span>|
565
+ format.<span class="FunctionName">html</span> <span class="Comment"><span class="Comment">#</span> index.html.erb</span>
566
+ format.<span class="FunctionName">xml</span> { render <span class="UserDefinedConstant"><span class="UserDefinedConstant">:</span>xml</span> =&gt; <span class="Variable"><span class="Variable">@</span>posts</span> }
567
+ <span class="Keyword">end</span>
568
+ <span class="Keyword">end</span>
569
+ </pre>
570
+
571
+
572
+ <p><code>Post.all</code> calls the <code>Post</code> model to return all of the posts currently in the database. The result of this call is an array containing the posts which has been saved in an instance variable called <code>@posts</code>.</p>
573
+
574
+ <p class="notice">
575
+ For more information on finding records with Active Record, see <a href="http://guides.rubyonrails.org/active_record_querying.html" title="Ruby on Rails Guides: Active Record Query Interface">Active Record Query Interface</a>.
576
+ </p>
577
+
578
+
579
+ <p>The respond_to block handles both HTML and XML calls to this action. If you browse to <a href="http://localhost:3000/posts.xml">http://localhost:3000/posts.xml</a>, you&#x2019;ll see all of the posts in XML format. The HTML format looks for a view in <code>app/views/posts/</code> with a name that corresponds to the action name. Rails makes all of the instance variables from the action available to the view. Here&#x2019;s <code>app/views/posts/index.html.erb</code>:</p>
580
+
581
+ <pre class="idle">&lt;<span class="TagName">h1</span>&gt;Listing posts&lt;/<span class="TagName">h1</span>&gt;
582
+
583
+ &lt;<span class="TagName">table</span>&gt;
584
+ &lt;<span class="TagName">tr</span>&gt;
585
+ &lt;<span class="TagName">th</span>&gt;Name&lt;/<span class="TagName">th</span>&gt;
586
+ &lt;<span class="TagName">th</span>&gt;Title&lt;/<span class="TagName">th</span>&gt;
587
+ &lt;<span class="TagName">th</span>&gt;Content&lt;/<span class="TagName">th</span>&gt;
588
+ &lt;<span class="TagName">th</span>&gt;&lt;/<span class="TagName">th</span>&gt;
589
+ &lt;<span class="TagName">th</span>&gt;&lt;/<span class="TagName">th</span>&gt;
590
+ &lt;<span class="TagName">th</span>&gt;&lt;/<span class="TagName">th</span>&gt;
591
+ &lt;/<span class="TagName">tr</span>&gt;
592
+
593
+ &lt;% <span class="Variable"><span class="Variable">@</span>posts</span>.<span class="FunctionName">each</span> <span class="Keyword">do </span>|<span class="Variable">post</span>| %&gt;
594
+ &lt;<span class="TagName">tr</span>&gt;
595
+ &lt;<span class="TagName">td</span>&gt;&lt;%= post.<span class="FunctionName">name</span> %&gt;&lt;/<span class="TagName">td</span>&gt;
596
+ &lt;<span class="TagName">td</span>&gt;&lt;%= post.<span class="FunctionName">title</span> %&gt;&lt;/<span class="TagName">td</span>&gt;
597
+ &lt;<span class="TagName">td</span>&gt;&lt;%= post.<span class="FunctionName">content</span> %&gt;&lt;/<span class="TagName">td</span>&gt;
598
+ &lt;<span class="TagName">td</span>&gt;&lt;%= link_to <span class="String"><span class="String">'</span>Show<span class="String">'</span></span>, post %&gt;&lt;/<span class="TagName">td</span>&gt;
599
+ &lt;<span class="TagName">td</span>&gt;&lt;%= link_to <span class="String"><span class="String">'</span>Edit<span class="String">'</span></span>, <span class="FunctionName">edit_post_path</span>(post) %&gt;&lt;/<span class="TagName">td</span>&gt;
600
+ &lt;<span class="TagName">td</span>&gt;&lt;%= link_to <span class="String"><span class="String">'</span>Destroy<span class="String">'</span></span>, post, <span class="UserDefinedConstant"><span class="UserDefinedConstant">:</span>confirm</span> =&gt; <span class="String"><span class="String">'</span>Are you sure?<span class="String">'</span></span>, <span class="UserDefinedConstant"><span class="UserDefinedConstant">:</span>method</span> =&gt; <span class="UserDefinedConstant"><span class="UserDefinedConstant">:</span>delete</span> %&gt;&lt;/<span class="TagName">td</span>&gt;
601
+ &lt;/<span class="TagName">tr</span>&gt;
602
+ &lt;% <span class="Keyword">end</span> %&gt;
603
+ &lt;/<span class="TagName">table</span>&gt;
604
+
605
+ &lt;<span class="TagName">br</span> /&gt;
606
+
607
+ &lt;%= link_to <span class="String"><span class="String">'</span>New post<span class="String">'</span></span>, new_post_path %&gt;
608
+ </pre>
609
+
610
+
611
+ <p>This view iterates over the contents of the <code>@posts</code> array to display content and links. A few things to note in the view:</p>
612
+
613
+ <ul><li><code>link_to</code> builds a hyperlink to a particular destination</li>
614
+ <li><code>edit_post_path</code> and <code>new_post_path</code> are helpers that Rails provides as part of RESTful routing. You&#x2019;ll see a variety of these helpers for the different actions that the controller includes.</li>
615
+ </ul><p class="notice">
616
+ In previous versions of Rails, you had to use <code>&lt;%=h post.name %&gt;</code> so that any HTML would be escaped before being inserted into the page. In Rails 3.0, this is now the default. To get unescaped HTML, you now use <code>&lt;%= raw post.name %&gt;</code>. For more details on the rendering process, see <a href="http://guides.rubyonrails.org/layouts_and_rendering.html" title="Ruby on Rails Guides: Layouts and Rendering in Rails">Layouts and Rendering in Rails</a>.
617
+ </p>
618
+
619
+
620
+ <h3 id="customizing-the-layout">Customizing the Layout</h3>
621
+
622
+ <p>The view is only part of the story of how HTML is displayed in your web browser. Rails also has the concept of <em>layouts</em>, which are containers for views. When Rails renders a view to the browser, it does so by putting the view&#x2019;s HTML into a layout&#x2019;s HTML. In previous versions of Rails, the <code>rails generate scaffold</code> command would automatically create a controller specific layout, like <code>app/views/layouts/posts.html.erb</code>, for the posts controller. However this has been changed in Rails 3.0. A application specific layout is used for all the controllers and can be found in <code>app/views/layouts/application.html.erb</code>. Open this layout in your editor and modify the body tag:</p>
623
+
624
+ <pre class="idle">&lt;!<span class="TagName">DOCTYPE</span> html&gt;
625
+ &lt;<span class="TagName">html</span>&gt;
626
+ &lt;<span class="TagName">head</span>&gt;
627
+ &lt;<span class="TagName">title</span>&gt;Blog&lt;/<span class="TagName">title</span>&gt;
628
+ &lt;%= stylesheet_link_tag <span class="UserDefinedConstant"><span class="UserDefinedConstant">:</span>all</span> %&gt;
629
+ &lt;%= javascript_include_tag <span class="UserDefinedConstant"><span class="UserDefinedConstant">:</span>defaults</span> %&gt;
630
+ &lt;%= csrf_meta_tag %&gt;
631
+ &lt;/<span class="TagName">head</span>&gt;
632
+ &lt;<span class="TagName">body</span> <span class="TagAttribute">style</span>=<span class="String"><span class="String">"</span>background: #EEEEEE;<span class="String">"</span></span>&gt;
633
+
634
+ &lt;%= <span class="Keyword">yield</span> %&gt;
635
+
636
+ &lt;/<span class="TagName">body</span>&gt;
637
+ &lt;/<span class="TagName">html</span>&gt;
638
+ </pre>
639
+
640
+
641
+ <p>Now when you refresh the <code>/posts</code> page, you&#x2019;ll see a gray background to the page. This same gray background will be used throughout all the views for posts.</p>
642
+
643
+ <h3 id="creating-new-posts">Creating New Posts</h3>
644
+
645
+ <p>Creating a new post involves two actions. The first is the <code>new</code> action, which instantiates an empty <code>Post</code> object:</p>
646
+
647
+ <pre class="idle"><span class="Keyword">def</span> <span class="FunctionName">new</span>
648
+ &#xA0;&#xA0;<span class="Variable"><span class="Variable">@</span>post</span> <span class="Keyword">=</span> <span class="LibraryClassType">Post</span>.<span class="FunctionName">new</span>
649
+
650
+ &#xA0;&#xA0;respond_to <span class="Keyword">do </span>|<span class="Variable">format</span>|
651
+ &#xA0;&#xA0;&#xA0;&#xA0;format.<span class="FunctionName">html</span> <span class="Comment"><span class="Comment">#</span> new.html.erb</span>
652
+ &#xA0;&#xA0;&#xA0;&#xA0;format.<span class="FunctionName">xml</span>&#xA0; { render <span class="UserDefinedConstant"><span class="UserDefinedConstant">:</span>xml</span> =&gt; <span class="Variable"><span class="Variable">@</span>post</span> }
653
+ &#xA0;&#xA0;<span class="Keyword">end</span>
654
+ <span class="Keyword">end</span>
655
+ </pre>
656
+
657
+
658
+ <p>The <code>new.html.erb</code> view displays this empty <code>Post</code> to the user:</p>
659
+
660
+ <pre class="idle">&lt;<span class="TagName">h1</span>&gt;New post&lt;/<span class="TagName">h1</span>&gt;
661
+
662
+ &lt;%= render <span class="String"><span class="String">'</span>form<span class="String">'</span></span> %&gt;
663
+
664
+ &lt;%= link_to <span class="String"><span class="String">'</span>Back<span class="String">'</span></span>, posts_path %&gt;
665
+ </pre>
666
+
667
+
668
+ <p>The <code>&lt;%= render 'form' %&gt;</code> line is our first introduction to <em>partials</em> in Rails. A partial is a snippet of HTML and Ruby code that can be reused in multiple locations. In this case, the form used to make a new post, is basically identical to a form used to edit a post, both have text fields for the name and title and a text area for the content with a button to make a new post or update the existing post.</p>
669
+
670
+ <p>If you take a look at <code>views/posts/_form.html.erb</code> file, you will see the following:</p>
671
+
672
+ <pre class="idle">&lt;%= <span class="FunctionName">form_for</span>(<span class="Variable"><span class="Variable">@</span>post</span>) <span class="Keyword">do </span>|<span class="Variable">f</span>| %&gt;
673
+ &#xA0;&#xA0;&lt;% <span class="Keyword">if</span> <span class="Variable"><span class="Variable">@</span>post</span>.<span class="FunctionName">errors</span>.<span class="FunctionName">any?</span> %&gt;
674
+ &#xA0;&#xA0;&lt;<span class="TagName">div</span> <span class="TagAttribute">id</span>=<span class="String"><span class="String">"</span><span class="String">errorExplanation</span><span class="String">"</span></span>&gt;
675
+ &#xA0;&#xA0;&#xA0;&#xA0;&lt;<span class="TagName">h2</span>&gt;&lt;%= <span class="FunctionName">pluralize</span>(<span class="Variable"><span class="Variable">@</span>post</span>.<span class="FunctionName">errors</span>.<span class="FunctionName">count</span>, <span class="String"><span class="String">"</span>error<span class="String">"</span></span>) %&gt; prohibited this post from being saved:&lt;/<span class="TagName">h2</span>&gt;
676
+ &#xA0;&#xA0;&#xA0;&#xA0;&lt;<span class="TagName">ul</span>&gt;
677
+ &#xA0;&#xA0;&#xA0;&#xA0;&lt;% <span class="Variable"><span class="Variable">@</span>post</span>.<span class="FunctionName">errors</span>.<span class="FunctionName">full_messages</span>.<span class="FunctionName">each</span> <span class="Keyword">do </span>|<span class="Variable">msg</span>| %&gt;
678
+ &#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&lt;<span class="TagName">li</span>&gt;&lt;%= msg %&gt;&lt;/<span class="TagName">li</span>&gt;
679
+ &#xA0;&#xA0;&#xA0;&#xA0;&lt;% <span class="Keyword">end</span> %&gt;
680
+ &#xA0;&#xA0;&#xA0;&#xA0;&lt;/<span class="TagName">ul</span>&gt;
681
+ &#xA0;&#xA0;&lt;/<span class="TagName">div</span>&gt;
682
+ &#xA0;&#xA0;&lt;% <span class="Keyword">end</span> %&gt;
683
+
684
+ &#xA0;&#xA0;&lt;<span class="TagName">div</span> <span class="TagAttribute">class</span>=<span class="String"><span class="String">"</span>field<span class="String">"</span></span>&gt;
685
+ &#xA0;&#xA0;&#xA0;&#xA0;&lt;%= f.<span class="FunctionName">label</span> <span class="UserDefinedConstant"><span class="UserDefinedConstant">:</span>name</span> %&gt;&lt;<span class="TagName">br</span> /&gt;
686
+ &#xA0;&#xA0;&#xA0;&#xA0;&lt;%= f.<span class="FunctionName">text_field</span> <span class="UserDefinedConstant"><span class="UserDefinedConstant">:</span>name</span> %&gt;
687
+ &#xA0;&#xA0;&lt;/<span class="TagName">div</span>&gt;
688
+ &#xA0;&#xA0;&lt;<span class="TagName">div</span> <span class="TagAttribute">class</span>=<span class="String"><span class="String">"</span>field<span class="String">"</span></span>&gt;
689
+ &#xA0;&#xA0;&#xA0;&#xA0;&lt;%= f.<span class="FunctionName">label</span> <span class="UserDefinedConstant"><span class="UserDefinedConstant">:</span>title</span> %&gt;&lt;<span class="TagName">br</span> /&gt;
690
+ &#xA0;&#xA0;&#xA0;&#xA0;&lt;%= f.<span class="FunctionName">text_field</span> <span class="UserDefinedConstant"><span class="UserDefinedConstant">:</span>title</span> %&gt;
691
+ &#xA0;&#xA0;&lt;/<span class="TagName">div</span>&gt;
692
+ &#xA0;&#xA0;&lt;<span class="TagName">div</span> <span class="TagAttribute">class</span>=<span class="String"><span class="String">"</span>field<span class="String">"</span></span>&gt;
693
+ &#xA0;&#xA0;&#xA0;&#xA0;&lt;%= f.<span class="FunctionName">label</span> <span class="UserDefinedConstant"><span class="UserDefinedConstant">:</span>content</span> %&gt;&lt;<span class="TagName">br</span> /&gt;
694
+ &#xA0;&#xA0;&#xA0;&#xA0;&lt;%= f.<span class="FunctionName">text_area</span> <span class="UserDefinedConstant"><span class="UserDefinedConstant">:</span>content</span> %&gt;
695
+ &#xA0;&#xA0;&lt;/<span class="TagName">div</span>&gt;
696
+ &#xA0;&#xA0;&lt;<span class="TagName">div</span> <span class="TagAttribute">class</span>=<span class="String"><span class="String">"</span>actions<span class="String">"</span></span>&gt;
697
+ &#xA0;&#xA0;&#xA0;&#xA0;&lt;%= f.<span class="FunctionName">submit</span> %&gt;
698
+ &#xA0;&#xA0;&lt;/<span class="TagName">div</span>&gt;
699
+ &lt;% <span class="Keyword">end</span> %&gt;
700
+ </pre>
701
+
702
+
703
+ <p>This partial receives all the instance variables defined in the calling view file, so in this case, the controller assigned the new <code>Post</code> object to <code>@post</code> and so, this is available in both the view and partial as <code>@post</code>.</p>
704
+
705
+ <p>For more information on partials, refer to the <a href="http://guides.rubyonrails.org/layouts_and_rendering.html#using-partials">Layouts and Rendering in Rails</a> guide.</p>
706
+
707
+ <p>The <code>form_for</code> block is used to create an HTML form. Within this block, you have access to methods to build various controls on the form. For example, <code>f.text_field :name</code> tells Rails to create a text input on the form, and to hook it up to the <code>name</code> attribute of the instance being displayed. You can only use these methods with attributes of the model that the form is based on (in this case <code>name</code>, <code>title</code>, and <code>content</code>). Rails uses <code>form_for</code> in preference to having you write raw HTML because the code is more succinct, and because it explicitly ties the form to a particular model instance.</p>
708
+
709
+ <p>The <code>form_for</code> block is also smart enough to work out if you are doing a <em>New Post</em> or an <em>Edit Post</em> action, and will set the form <code>action</code> tags and submit button names appropriately in the HTML output.</p>
710
+
711
+ <p class="notice">
712
+ If you need to create an HTML form that displays arbitrary fields, not tied to a model, you should use the <code>form_tag</code> method, which provides shortcuts for building forms that are not necessarily tied to a model instance.
713
+ </p>
714
+
715
+
716
+ <p>When the user clicks the <em>Create Post</em> button on this form, the browser will send information back to the create method of the controller (Rails knows to call the <code>create</code> method because the form is sent with an HTTP POST request; that&#x2019;s one of the conventions that I mentioned earlier):</p>
717
+
718
+ <pre class="idle"><span class="Keyword">def</span> <span class="FunctionName">create</span>
719
+ &#xA0;&#xA0;<span class="Variable"><span class="Variable">@</span>post</span> <span class="Keyword">=</span> <span class="LibraryClassType">Post</span>.<span class="FunctionName">new</span>(params[<span class="UserDefinedConstant"><span class="UserDefinedConstant">:</span>post</span>])
720
+
721
+ &#xA0;&#xA0;respond_to <span class="Keyword">do </span>|<span class="Variable">format</span>|
722
+ &#xA0;&#xA0;&#xA0;&#xA0;<span class="Keyword">if</span> <span class="Variable"><span class="Variable">@</span>post</span>.<span class="FunctionName">save</span>
723
+ &#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;format.<span class="FunctionName">html</span> { <span class="FunctionName">redirect_to</span>(<span class="Variable"><span class="Variable">@</span>post</span>,
724
+ &#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;<span class="UserDefinedConstant"><span class="UserDefinedConstant">:</span>notice</span> =&gt; <span class="String"><span class="String">'</span>Post was successfully created.<span class="String">'</span></span>) }
725
+ &#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;format.<span class="FunctionName">xml</span>&#xA0; { render <span class="UserDefinedConstant"><span class="UserDefinedConstant">:</span>xml</span> =&gt; <span class="Variable"><span class="Variable">@</span>post</span>,
726
+ &#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;<span class="UserDefinedConstant"><span class="UserDefinedConstant">:</span>status</span> =&gt; <span class="UserDefinedConstant"><span class="UserDefinedConstant">:</span>created</span>, <span class="UserDefinedConstant"><span class="UserDefinedConstant">:</span>location</span> =&gt; <span class="Variable"><span class="Variable">@</span>post</span> }
727
+ &#xA0;&#xA0;&#xA0;&#xA0;<span class="Keyword">else</span>
728
+ &#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;format.<span class="FunctionName">html</span> { render <span class="UserDefinedConstant"><span class="UserDefinedConstant">:</span>action</span> =&gt; <span class="String"><span class="String">"</span>new<span class="String">"</span></span> }
729
+ &#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;format.<span class="FunctionName">xml</span>&#xA0; { render <span class="UserDefinedConstant"><span class="UserDefinedConstant">:</span>xml</span> =&gt; <span class="Variable"><span class="Variable">@</span>post</span>.<span class="FunctionName">errors</span>,
730
+ &#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;<span class="UserDefinedConstant"><span class="UserDefinedConstant">:</span>status</span> =&gt; <span class="UserDefinedConstant"><span class="UserDefinedConstant">:</span>unprocessable_entity</span> }
731
+ &#xA0;&#xA0;&#xA0;&#xA0;<span class="Keyword">end</span>
732
+ &#xA0;&#xA0;<span class="Keyword">end</span>
733
+ <span class="Keyword">end</span>
734
+ </pre>
735
+
736
+
737
+ <p>The <code>create</code> action instantiates a new Post object from the data supplied by the user on the form, which Rails makes available in the <code>params</code> hash. After successfully saving the new post, <code>create</code> returns the appropriate format that the user has requested (HTML in our case). It then redirects the user to the resulting post show action and sets a notice to the user that the Post was successfully created.</p>
738
+
739
+ <p>If the post was not successfully saved, due to a validation error, then the controller returns the user back to the new action with any error messages so that the user has the chance to fix the error and try again.</p>
740
+
741
+ <p>The &#x201C;Post was successfully created&#x201D; message is stored inside of the Rails <code>flash</code> hash, (usually just called the Flash) so that messages can be carried over to another action, providing the user with useful information on the status of their request. In the case of <code>create</code>, the user never actually sees any page rendered during the Post creation process, because it immediately redirects to the new Post as soon Rails saves the record. The Flash carries over a message to the next action, so that when the user is redirected back to the <code>show</code> action, they are presented with a message saying &#x201C;Post was successfully created.&#x201D;</p>
742
+
743
+ <h3 id="showing-an-individual-post">Showing an Individual Post</h3>
744
+
745
+ <p>When you click the <code>show</code> link for a post on the index page, it will bring you to a URL like <code>http://localhost:3000/posts/1</code>. Rails interprets this as a call to the <code>show</code> action for the resource, and passes in <code>1</code> as the <code>:id</code> parameter. Here&#x2019;s the show action:</p>
746
+
747
+ <pre class="idle"><span class="Keyword">def</span> <span class="FunctionName">show</span>
748
+ &#xA0;&#xA0;<span class="Variable"><span class="Variable">@</span>post</span> <span class="Keyword">=</span> <span class="LibraryClassType">Post</span>.<span class="FunctionName">find</span>(params[<span class="UserDefinedConstant"><span class="UserDefinedConstant">:</span>id</span>])
749
+
750
+ &#xA0;&#xA0;respond_to <span class="Keyword">do </span>|<span class="Variable">format</span>|
751
+ &#xA0;&#xA0;&#xA0;&#xA0;format.<span class="FunctionName">html</span> <span class="Comment"><span class="Comment">#</span> show.html.erb</span>
752
+ &#xA0;&#xA0;&#xA0;&#xA0;format.<span class="FunctionName">xml</span>&#xA0; { render <span class="UserDefinedConstant"><span class="UserDefinedConstant">:</span>xml</span> =&gt; <span class="Variable"><span class="Variable">@</span>post</span> }
753
+ &#xA0;&#xA0;<span class="Keyword">end</span>
754
+ <span class="Keyword">end</span>
755
+ </pre>
756
+
757
+
758
+ <p>The <code>show</code> action uses <code>Post.find</code> to search for a single record in the database by its id value. After finding the record, Rails displays it by using <code>show.html.erb</code>:</p>
759
+
760
+ <pre class="idle">&lt;<span class="TagName">p</span> <span class="TagAttribute">class</span>=<span class="String"><span class="String">"</span>notice<span class="String">"</span></span>&gt;&lt;%= notice %&gt;&lt;/<span class="TagName">p</span>&gt;
761
+
762
+ &lt;<span class="TagName">p</span>&gt;
763
+ &#xA0;&#xA0;&lt;<span class="TagName">b</span>&gt;Name:&lt;/<span class="TagName">b</span>&gt;
764
+ &#xA0;&#xA0;&lt;%= <span class="Variable"><span class="Variable">@</span>post</span>.<span class="FunctionName">name</span> %&gt;
765
+ &lt;/<span class="TagName">p</span>&gt;
766
+
767
+ &lt;<span class="TagName">p</span>&gt;
768
+ &#xA0;&#xA0;&lt;<span class="TagName">b</span>&gt;Title:&lt;/<span class="TagName">b</span>&gt;
769
+ &#xA0;&#xA0;&lt;%= <span class="Variable"><span class="Variable">@</span>post</span>.<span class="FunctionName">title</span> %&gt;
770
+ &lt;/<span class="TagName">p</span>&gt;
771
+
772
+ &lt;<span class="TagName">p</span>&gt;
773
+ &#xA0;&#xA0;&lt;<span class="TagName">b</span>&gt;Content:&lt;/<span class="TagName">b</span>&gt;
774
+ &#xA0;&#xA0;&lt;%= <span class="Variable"><span class="Variable">@</span>post</span>.<span class="FunctionName">content</span> %&gt;
775
+ &lt;/<span class="TagName">p</span>&gt;
776
+
777
+ &lt;%= link_to <span class="String"><span class="String">'</span>Edit<span class="String">'</span></span>, <span class="FunctionName">edit_post_path</span>(<span class="Variable"><span class="Variable">@</span>post</span>) %&gt; |
778
+ &lt;%= link_to <span class="String"><span class="String">'</span>Back<span class="String">'</span></span>, posts_path %&gt;
779
+ </pre>
780
+
781
+
782
+ <h3 id="editing-posts">Editing Posts</h3>
783
+
784
+ <p>Like creating a new post, editing a post is a two-part process. The first step is a request to <code>edit_post_path(@post)</code> with a particular post. This calls the <code>edit</code> action in the controller:</p>
785
+
786
+ <pre class="idle"><span class="Keyword">def</span> <span class="FunctionName">edit</span>
787
+ &#xA0;&#xA0;<span class="Variable"><span class="Variable">@</span>post</span> <span class="Keyword">=</span> <span class="LibraryClassType">Post</span>.<span class="FunctionName">find</span>(params[<span class="UserDefinedConstant"><span class="UserDefinedConstant">:</span>id</span>])
788
+ <span class="Keyword">end</span>
789
+ </pre>
790
+
791
+
792
+ <p>After finding the requested post, Rails uses the <code>edit.html.erb</code> view to display it:</p>
793
+
794
+ <pre class="idle">&lt;<span class="TagName">h1</span>&gt;Editing post&lt;/<span class="TagName">h1</span>&gt;
795
+
796
+ &lt;%= render <span class="String"><span class="String">'</span>form<span class="String">'</span></span> %&gt;
797
+
798
+ &lt;%= link_to <span class="String"><span class="String">'</span>Show<span class="String">'</span></span>, <span class="Variable"><span class="Variable">@</span>post</span> %&gt; |
799
+ &lt;%= link_to <span class="String"><span class="String">'</span>Back<span class="String">'</span></span>, posts_path %&gt;
800
+ </pre>
801
+
802
+
803
+ <p>Again, as with the <code>new</code> action, the <code>edit</code> action is using the <code>form</code> partial, this time however, the form will do a PUT action to the PostsController and the submit button will display &#x201C;Update Post&#x201D;.</p>
804
+
805
+ <p>Submitting the form created by this view will invoke the <code>update</code> action within the controller:</p>
806
+
807
+ <pre class="idle"><span class="Keyword">def</span> <span class="FunctionName">update</span>
808
+ &#xA0;&#xA0;<span class="Variable"><span class="Variable">@</span>post</span> <span class="Keyword">=</span> <span class="LibraryClassType">Post</span>.<span class="FunctionName">find</span>(params[<span class="UserDefinedConstant"><span class="UserDefinedConstant">:</span>id</span>])
809
+
810
+ &#xA0;&#xA0;respond_to <span class="Keyword">do </span>|<span class="Variable">format</span>|
811
+ &#xA0;&#xA0;&#xA0;&#xA0;<span class="Keyword">if</span> <span class="Variable"><span class="Variable">@</span>post</span>.<span class="FunctionName">update_attributes</span>(params[<span class="UserDefinedConstant"><span class="UserDefinedConstant">:</span>post</span>])
812
+ &#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;format.<span class="FunctionName">html</span> { <span class="FunctionName">redirect_to</span>(<span class="Variable"><span class="Variable">@</span>post</span>,
813
+ &#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;<span class="UserDefinedConstant"><span class="UserDefinedConstant">:</span>notice</span> =&gt; <span class="String"><span class="String">'</span>Post was successfully updated.<span class="String">'</span></span>) }
814
+ &#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;format.<span class="FunctionName">xml</span>&#xA0; { head <span class="UserDefinedConstant"><span class="UserDefinedConstant">:</span>ok</span> }
815
+ &#xA0;&#xA0;&#xA0;&#xA0;<span class="Keyword">else</span>
816
+ &#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;format.<span class="FunctionName">html</span> { render <span class="UserDefinedConstant"><span class="UserDefinedConstant">:</span>action</span> =&gt; <span class="String"><span class="String">"</span>edit<span class="String">"</span></span> }
817
+ &#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;format.<span class="FunctionName">xml</span>&#xA0; { render <span class="UserDefinedConstant"><span class="UserDefinedConstant">:</span>xml</span> =&gt; <span class="Variable"><span class="Variable">@</span>post</span>.<span class="FunctionName">errors</span>,
818
+ &#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;<span class="UserDefinedConstant"><span class="UserDefinedConstant">:</span>status</span> =&gt; <span class="UserDefinedConstant"><span class="UserDefinedConstant">:</span>unprocessable_entity</span> }
819
+ &#xA0;&#xA0;&#xA0;&#xA0;<span class="Keyword">end</span>
820
+ &#xA0;&#xA0;<span class="Keyword">end</span>
821
+ <span class="Keyword">end</span>
822
+ </pre>
823
+
824
+
825
+ <p>In the <code>update</code> action, Rails first uses the <code>:id</code> parameter passed back from the edit view to locate the database record that&#x2019;s being edited. The <code>update_attributes</code> call then takes the rest of the parameters from the request and applies them to this record. If all goes well, the user is redirected to the post&#x2019;s <code>show</code> view. If there are any problems, it&#x2019;s back to the <code>edit</code> view to correct them.</p>
826
+
827
+ <h4 id="destroying-a-post">Destroying a Post</h4>
828
+
829
+ <p>Finally, clicking one of the <code>destroy</code> links sends the associated id to the destroy action:</p>
830
+
831
+ <pre class="idle"><span class="Keyword">def</span> <span class="FunctionName">destroy</span>
832
+ &#xA0;&#xA0;<span class="Variable"><span class="Variable">@</span>post</span> <span class="Keyword">=</span> <span class="LibraryClassType">Post</span>.<span class="FunctionName">find</span>(params[<span class="UserDefinedConstant"><span class="UserDefinedConstant">:</span>id</span>])
833
+ &#xA0;&#xA0;<span class="Variable"><span class="Variable">@</span>post</span>.<span class="FunctionName">destroy</span>
834
+
835
+ &#xA0;&#xA0;respond_to <span class="Keyword">do </span>|<span class="Variable">format</span>|
836
+ &#xA0;&#xA0;&#xA0;&#xA0;format.<span class="FunctionName">html</span> { <span class="FunctionName">redirect_to</span>(posts_url) }
837
+ &#xA0;&#xA0;&#xA0;&#xA0;format.<span class="FunctionName">xml</span>&#xA0; { head <span class="UserDefinedConstant"><span class="UserDefinedConstant">:</span>ok</span> }
838
+ &#xA0;&#xA0;<span class="Keyword">end</span>
839
+ <span class="Keyword">end</span>
840
+ </pre>
841
+
842
+
843
+ <p>The <code>destroy</code> method of an Active Record model instance removes the corresponding record from the database. After that&#x2019;s done, there isn&#x2019;t any record to display, so Rails redirects the user&#x2019;s browser to the index view for the model.</p>
844
+
845
+
846
+ </div>
847
+ <div class="chapter"><h2 id="adding-a-second-model">Adding a Second Model</h2>
848
+
849
+ <p>Now that you&#x2019;ve seen how a model built with scaffolding looks like, it&#x2019;s time to add a second model to the application. The second model will handle comments on blog posts.</p>
850
+
851
+ <h3 id="generating-a-model">Generating a Model</h3>
852
+
853
+ <p>Models in Rails use a singular name, and their corresponding database tables use a plural name. For the model to hold comments, the convention is to use the name Comment. Even if you don&#x2019;t want to use the entire apparatus set up by scaffolding, most Rails developers still use generators to make things like models and controllers. To create the new model, run this command in your terminal:</p>
854
+
855
+ <pre><code>$ rails generate model Comment commenter:string body:text post:references
856
+ </code></pre>
857
+
858
+ <p>This command will generate four files:</p>
859
+
860
+ <ul><li><code>app/models/comment.rb</code> &#x2013; The model</li>
861
+ <li><code>db/migrate/20100207235629_create_comments.rb</code> &#x2013; The migration</li>
862
+ <li><code>test/unit/comment_test.rb and test/fixtures/comments.yml</code> &#x2013; The test harness.</li>
863
+ </ul><p>First, take a look at <code>comment.rb</code>:</p>
864
+
865
+ <pre class="idle"><span class="Keyword">class</span> <span class="TypeName">Comment<span class="InheritedClass"> <span class="InheritedClass">&lt;</span> ActiveRecord::Base</span></span>
866
+ &#xA0;&#xA0;belongs_to <span class="UserDefinedConstant"><span class="UserDefinedConstant">:</span>post</span>
867
+ <span class="Keyword">end</span>
868
+ </pre>
869
+
870
+
871
+ <p>This is very similar to the <code>post.rb</code> model that you saw earlier. The difference is the line <code>belongs_to :post</code>, which sets up an Active Record <em>association</em>. You&#x2019;ll learn a little about associations in the next section of this guide.</p>
872
+
873
+ <p>In addition to the model, Rails has also made a migration to create the corresponding database table:</p>
874
+
875
+ <pre class="idle"><span class="Keyword">class</span> <span class="TypeName">CreateComments<span class="InheritedClass"> <span class="InheritedClass">&lt;</span> ActiveRecord::Migration</span></span>
876
+ &#xA0;&#xA0;def <span class="Variable">self</span>.<span class="FunctionName">up</span>
877
+ &#xA0;&#xA0;&#xA0;&#xA0;create_table <span class="UserDefinedConstant"><span class="UserDefinedConstant">:</span>comments</span> <span class="Keyword">do </span>|<span class="Variable">t</span>|
878
+ &#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;t.<span class="FunctionName">string</span> <span class="UserDefinedConstant"><span class="UserDefinedConstant">:</span>commenter</span>
879
+ &#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;t.<span class="FunctionName">text</span> <span class="UserDefinedConstant"><span class="UserDefinedConstant">:</span>body</span>
880
+ &#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;t.<span class="FunctionName">references</span> <span class="UserDefinedConstant"><span class="UserDefinedConstant">:</span>post</span>
881
+
882
+ &#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;t.<span class="FunctionName">timestamps</span>
883
+ &#xA0;&#xA0;&#xA0;&#xA0;<span class="Keyword">end</span>
884
+ &#xA0;&#xA0;<span class="Keyword">end</span>
885
+
886
+ &#xA0;&#xA0;def <span class="Variable">self</span>.<span class="FunctionName">down</span>
887
+ &#xA0;&#xA0;&#xA0;&#xA0;drop_table <span class="UserDefinedConstant"><span class="UserDefinedConstant">:</span>comments</span>
888
+ &#xA0;&#xA0;<span class="Keyword">end</span>
889
+ <span class="Keyword">end</span>
890
+ </pre>
891
+
892
+
893
+ <p>The <code>t.references</code> line sets up a foreign key column for the association between the two models. Go ahead and run the migration:</p>
894
+
895
+ <pre><code>$ rake db:migrate
896
+ </code></pre>
897
+
898
+ <p>Rails is smart enough to only execute the migrations that have not already been run against the current database, so in this case you will just see:</p>
899
+
900
+ <pre><code>==&#xA0; CreateComments: migrating ============
901
+ -- create_table(:comments)
902
+ &#xA0;&#xA0;&#xA0;-&gt; 0.0017s
903
+ ==&#xA0; CreateComments: migrated (0.0018s) ===
904
+ </code></pre>
905
+
906
+ <h3 id="associating-models">Associating Models</h3>
907
+
908
+ <p>Active Record associations let you easily declare the relationship between two models. In the case of comments and posts, you could write out the relationships this way:</p>
909
+
910
+ <ul><li>Each comment belongs to one post</li>
911
+ <li>One post can have many comments</li>
912
+ </ul><p>In fact, this is very close to the syntax that Rails uses to declare this association. You&#x2019;ve already seen the line of code inside the Comment model that makes each comment belong to a Post:</p>
913
+
914
+ <pre class="idle"><span class="Keyword">class</span> <span class="TypeName">Comment<span class="InheritedClass"> <span class="InheritedClass">&lt;</span> ActiveRecord::Base</span></span>
915
+ &#xA0;&#xA0;belongs_to <span class="UserDefinedConstant"><span class="UserDefinedConstant">:</span>post</span>
916
+ <span class="Keyword">end</span>
917
+ </pre>
918
+
919
+
920
+ <p>You&#x2019;ll need to edit the <code>post.rb</code> file to add the other side of the association:</p>
921
+
922
+ <pre class="idle"><span class="Keyword">class</span> <span class="TypeName">Post<span class="InheritedClass"> <span class="InheritedClass">&lt;</span> ActiveRecord::Base</span></span>
923
+ &#xA0;&#xA0;validates <span class="UserDefinedConstant"><span class="UserDefinedConstant">:</span>name</span>,&#xA0; <span class="UserDefinedConstant"><span class="UserDefinedConstant">:</span>presence</span> =&gt; <span class="BuiltInConstant">true</span>
924
+ &#xA0;&#xA0;validates <span class="UserDefinedConstant"><span class="UserDefinedConstant">:</span>title</span>, <span class="UserDefinedConstant"><span class="UserDefinedConstant">:</span>presence</span> =&gt; <span class="BuiltInConstant">true</span>,
925
+ &#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;<span class="UserDefinedConstant"><span class="UserDefinedConstant">:</span>length</span> =&gt; { <span class="UserDefinedConstant"><span class="UserDefinedConstant">:</span>minimum</span> =&gt; <span class="Number">5</span> }
926
+
927
+ &#xA0;&#xA0;has_many <span class="UserDefinedConstant"><span class="UserDefinedConstant">:</span>comments</span>
928
+ <span class="Keyword">end</span>
929
+ </pre>
930
+
931
+
932
+ <p>These two declarations enable a good bit of automatic behavior. For example, if you have an instance variable <code>@post</code> containing a post, you can retrieve all the comments belonging to that post as the array <code>@post.comments</code>.</p>
933
+
934
+ <p class="notice">
935
+ For more information on Active Record associations, see the <a href="http://guides.rubyonrails.org/association_basics.html" title="Ruby on Rails Guides: A Guide to Active Record Associations">Active Record Associations</a> guide.
936
+ </p>
937
+
938
+
939
+ <h3 id="adding-a-route-for-comments">Adding a Route for Comments</h3>
940
+
941
+ <p>As with the <code>home</code> controller, we will need to add a route so that Rails knows where we would like to navigate to see <code>comments</code>. Open up the <code>config/routes.rb</code> file again, you will see an entry that was added automatically for posts near the top by the scaffold generator, <code>resources :posts</code>, edit it as follows:</p>
942
+
943
+ <pre class="idle">resources <span class="UserDefinedConstant"><span class="UserDefinedConstant">:</span>posts</span> <span class="Keyword">do</span>
944
+ &#xA0;&#xA0;resources <span class="UserDefinedConstant"><span class="UserDefinedConstant">:</span>comments</span>
945
+ <span class="Keyword">end</span>
946
+ </pre>
947
+
948
+
949
+ <p>This creates <code>comments</code> as a <em>nested resource</em> within <code>posts</code>. This is another part of capturing the hierarchical relationship that exists between posts and comments.</p>
950
+
951
+ <p class="notice">
952
+ For more information on routing, see the <a href="http://guides.rubyonrails.org/routing.html" title="Ruby on Rails Guides: Rails Routing from the Outside In">Rails Routing from the Outside In</a> guide.
953
+ </p>
954
+
955
+
956
+ <h3 id="generating-a-controller">Generating a Controller</h3>
957
+
958
+ <p>With the model in hand, you can turn your attention to creating a matching controller. Again, there&#x2019;s a generator for this:</p>
959
+
960
+ <pre><code>$ rails generate controller Comments
961
+ </code></pre>
962
+
963
+ <p>This creates four files and one empty directory:</p>
964
+
965
+ <ul><li><code>app/controllers/comments_controller.rb</code> &#x2013; The controller</li>
966
+ <li><code>app/helpers/comments_helper.rb</code> &#x2013; A view helper file</li>
967
+ <li><code>test/functional/comments_controller_test.rb</code> &#x2013; The functional tests for the controller</li>
968
+ <li><code>test/unit/helpers/comments_helper_test.rb</code> &#x2013; The unit tests for the helper</li>
969
+ <li><code>app/views/comments/</code> &#x2013; Views of the controller are stored here</li>
970
+ </ul><p>Like with any blog, our readers will create their comments directly after reading the post, and once they have added their comment, will be sent back to the post show page to see their comment now listed. Due to this, our <code>CommentsController</code> is there to provide a method to create comments and delete SPAM comments when they arrive.</p>
971
+
972
+ <p>So first, we&#x2019;ll wire up the Post show template (<code>/app/views/posts/show.html.erb</code>) to let us make a new comment:</p>
973
+
974
+ <pre class="idle">&lt;<span class="TagName">p</span> <span class="TagAttribute">class</span>=<span class="String"><span class="String">"</span>notice<span class="String">"</span></span>&gt;&lt;%= notice %&gt;&lt;/<span class="TagName">p</span>&gt;
975
+
976
+ &lt;<span class="TagName">p</span>&gt;
977
+ &#xA0;&#xA0;&lt;<span class="TagName">b</span>&gt;Name:&lt;/<span class="TagName">b</span>&gt;
978
+ &#xA0;&#xA0;&lt;%= <span class="Variable"><span class="Variable">@</span>post</span>.<span class="FunctionName">name</span> %&gt;
979
+ &lt;/<span class="TagName">p</span>&gt;
980
+
981
+ &lt;<span class="TagName">p</span>&gt;
982
+ &#xA0;&#xA0;&lt;<span class="TagName">b</span>&gt;Title:&lt;/<span class="TagName">b</span>&gt;
983
+ &#xA0;&#xA0;&lt;%= <span class="Variable"><span class="Variable">@</span>post</span>.<span class="FunctionName">title</span> %&gt;
984
+ &lt;/<span class="TagName">p</span>&gt;
985
+
986
+ &lt;<span class="TagName">p</span>&gt;
987
+ &#xA0;&#xA0;&lt;<span class="TagName">b</span>&gt;Content:&lt;/<span class="TagName">b</span>&gt;
988
+ &#xA0;&#xA0;&lt;%= <span class="Variable"><span class="Variable">@</span>post</span>.<span class="FunctionName">content</span> %&gt;
989
+ &lt;/<span class="TagName">p</span>&gt;
990
+
991
+ &lt;<span class="TagName">h2</span>&gt;Add a comment:&lt;/<span class="TagName">h2</span>&gt;
992
+ &lt;%= <span class="FunctionName">form_for</span>([<span class="Variable"><span class="Variable">@</span>post</span>, <span class="Variable"><span class="Variable">@</span>post</span>.<span class="FunctionName">comments</span>.<span class="FunctionName">build</span>]) <span class="Keyword">do </span>|<span class="Variable">f</span>| %&gt;
993
+ &#xA0;&#xA0;&lt;<span class="TagName">div</span> <span class="TagAttribute">class</span>=<span class="String"><span class="String">"</span>field<span class="String">"</span></span>&gt;
994
+ &#xA0;&#xA0;&#xA0;&#xA0;&lt;%= f.<span class="FunctionName">label</span> <span class="UserDefinedConstant"><span class="UserDefinedConstant">:</span>commenter</span> %&gt;&lt;<span class="TagName">br</span> /&gt;
995
+ &#xA0;&#xA0;&#xA0;&#xA0;&lt;%= f.<span class="FunctionName">text_field</span> <span class="UserDefinedConstant"><span class="UserDefinedConstant">:</span>commenter</span> %&gt;
996
+ &#xA0;&#xA0;&lt;/<span class="TagName">div</span>&gt;
997
+ &#xA0;&#xA0;&lt;<span class="TagName">div</span> <span class="TagAttribute">class</span>=<span class="String"><span class="String">"</span>field<span class="String">"</span></span>&gt;
998
+ &#xA0;&#xA0;&#xA0;&#xA0;&lt;%= f.<span class="FunctionName">label</span> <span class="UserDefinedConstant"><span class="UserDefinedConstant">:</span>body</span> %&gt;&lt;<span class="TagName">br</span> /&gt;
999
+ &#xA0;&#xA0;&#xA0;&#xA0;&lt;%= f.<span class="FunctionName">text_area</span> <span class="UserDefinedConstant"><span class="UserDefinedConstant">:</span>body</span> %&gt;
1000
+ &#xA0;&#xA0;&lt;/<span class="TagName">div</span>&gt;
1001
+ &#xA0;&#xA0;&lt;<span class="TagName">div</span> <span class="TagAttribute">class</span>=<span class="String"><span class="String">"</span>actions<span class="String">"</span></span>&gt;
1002
+ &#xA0;&#xA0;&#xA0;&#xA0;&lt;%= f.<span class="FunctionName">submit</span> %&gt;
1003
+ &#xA0;&#xA0;&lt;/<span class="TagName">div</span>&gt;
1004
+ &lt;% <span class="Keyword">end</span> %&gt;
1005
+
1006
+ &lt;%= link_to <span class="String"><span class="String">'</span>Edit Post<span class="String">'</span></span>, <span class="FunctionName">edit_post_path</span>(<span class="Variable"><span class="Variable">@</span>post</span>) %&gt; |
1007
+ &lt;%= link_to <span class="String"><span class="String">'</span>Back to Posts<span class="String">'</span></span>, posts_path %&gt; |
1008
+ </pre>
1009
+
1010
+
1011
+ <p>This adds a form on the Post show page that creates a new comment, which will call the <code>CommentsController</code> create action, so let&#x2019;s wire that up:</p>
1012
+
1013
+ <pre class="idle"><span class="Keyword">class</span> <span class="TypeName">CommentsController<span class="InheritedClass"> <span class="InheritedClass">&lt;</span> ApplicationController</span></span>
1014
+ &#xA0;&#xA0;def create
1015
+ &#xA0;&#xA0;&#xA0;&#xA0;<span class="Variable"><span class="Variable">@</span>post</span> <span class="Keyword">=</span> <span class="LibraryClassType">Post</span>.<span class="FunctionName">find</span>(params[<span class="UserDefinedConstant"><span class="UserDefinedConstant">:</span>post_id</span>])
1016
+ &#xA0;&#xA0;&#xA0;&#xA0;<span class="Variable"><span class="Variable">@</span>comment</span> <span class="Keyword">=</span> <span class="Variable"><span class="Variable">@</span>post</span>.<span class="FunctionName">comments</span>.<span class="FunctionName">create</span>(params[<span class="UserDefinedConstant"><span class="UserDefinedConstant">:</span>comment</span>])
1017
+ &#xA0;&#xA0;&#xA0;&#xA0;redirect_to <span class="FunctionName">post_path</span>(<span class="Variable"><span class="Variable">@</span>post</span>)
1018
+ &#xA0;&#xA0;<span class="Keyword">end</span>
1019
+ <span class="Keyword">end</span>
1020
+ </pre>
1021
+
1022
+
1023
+ <p>You&#x2019;ll see a bit more complexity here than you did in the controller for posts. That&#x2019;s a side-effect of the nesting that you&#x2019;ve set up; each request for a comment has to keep track of the post to which the comment is attached, thus the initial find action to the Post model to get the post in question.</p>
1024
+
1025
+ <p>In addition, the code takes advantage of some of the methods available for an association. We use the <code>create</code> method on <code>@post.comments</code> to create and save the comment. This will automatically link the comment so that it belongs to that particular post.</p>
1026
+
1027
+ <p>Once we have made the new comment, we send the user back to the original post using the <code>post_path(@post)</code> helper. As we have already seen, this calls the <code>show</code> action of the <code>PostsController</code> which in turn renders the <code>show.html.erb</code> template. This is where we want the comment to show, so let&#x2019;s add that to the <code>app/views/posts/show.html.erb</code>.</p>
1028
+
1029
+ <pre class="idle">&lt;<span class="TagName">p</span> <span class="TagAttribute">class</span>=<span class="String"><span class="String">"</span>notice<span class="String">"</span></span>&gt;&lt;%= notice %&gt;&lt;/<span class="TagName">p</span>&gt;
1030
+
1031
+ &lt;<span class="TagName">p</span>&gt;
1032
+ &#xA0;&#xA0;&lt;<span class="TagName">b</span>&gt;Name:&lt;/<span class="TagName">b</span>&gt;
1033
+ &#xA0;&#xA0;&lt;%= <span class="Variable"><span class="Variable">@</span>post</span>.<span class="FunctionName">name</span> %&gt;
1034
+ &lt;/<span class="TagName">p</span>&gt;
1035
+
1036
+ &lt;<span class="TagName">p</span>&gt;
1037
+ &#xA0;&#xA0;&lt;<span class="TagName">b</span>&gt;Title:&lt;/<span class="TagName">b</span>&gt;
1038
+ &#xA0;&#xA0;&lt;%= <span class="Variable"><span class="Variable">@</span>post</span>.<span class="FunctionName">title</span> %&gt;
1039
+ &lt;/<span class="TagName">p</span>&gt;
1040
+
1041
+ &lt;<span class="TagName">p</span>&gt;
1042
+ &#xA0;&#xA0;&lt;<span class="TagName">b</span>&gt;Content:&lt;/<span class="TagName">b</span>&gt;
1043
+ &#xA0;&#xA0;&lt;%= <span class="Variable"><span class="Variable">@</span>post</span>.<span class="FunctionName">content</span> %&gt;
1044
+ &lt;/<span class="TagName">p</span>&gt;
1045
+
1046
+ &lt;<span class="TagName">h2</span>&gt;Comments&lt;/<span class="TagName">h2</span>&gt;
1047
+ &lt;% <span class="Variable"><span class="Variable">@</span>post</span>.<span class="FunctionName">comments</span>.<span class="FunctionName">each</span> <span class="Keyword">do </span>|<span class="Variable">comment</span>| %&gt;
1048
+ &#xA0;&#xA0;&lt;<span class="TagName">p</span>&gt;
1049
+ &#xA0;&#xA0;&#xA0;&#xA0;&lt;<span class="TagName">b</span>&gt;Commenter:&lt;/<span class="TagName">b</span>&gt;
1050
+ &#xA0;&#xA0;&#xA0;&#xA0;&lt;%= comment.<span class="FunctionName">commenter</span> %&gt;
1051
+ &#xA0;&#xA0;&lt;/<span class="TagName">p</span>&gt;
1052
+
1053
+ &#xA0;&#xA0;&lt;<span class="TagName">p</span>&gt;
1054
+ &#xA0;&#xA0;&#xA0;&#xA0;&lt;<span class="TagName">b</span>&gt;Comment:&lt;/<span class="TagName">b</span>&gt;
1055
+ &#xA0;&#xA0;&#xA0;&#xA0;&lt;%= comment.<span class="FunctionName">body</span> %&gt;
1056
+ &#xA0;&#xA0;&lt;/<span class="TagName">p</span>&gt;
1057
+ &lt;% <span class="Keyword">end</span> %&gt;
1058
+
1059
+ &lt;<span class="TagName">h2</span>&gt;Add a comment:&lt;/<span class="TagName">h2</span>&gt;
1060
+ &lt;%= <span class="FunctionName">form_for</span>([<span class="Variable"><span class="Variable">@</span>post</span>, <span class="Variable"><span class="Variable">@</span>post</span>.<span class="FunctionName">comments</span>.<span class="FunctionName">build</span>]) <span class="Keyword">do </span>|<span class="Variable">f</span>| %&gt;
1061
+ &#xA0;&#xA0;&lt;<span class="TagName">div</span> <span class="TagAttribute">class</span>=<span class="String"><span class="String">"</span>field<span class="String">"</span></span>&gt;
1062
+ &#xA0;&#xA0;&#xA0;&#xA0;&lt;%= f.<span class="FunctionName">label</span> <span class="UserDefinedConstant"><span class="UserDefinedConstant">:</span>commenter</span> %&gt;&lt;<span class="TagName">br</span> /&gt;
1063
+ &#xA0;&#xA0;&#xA0;&#xA0;&lt;%= f.<span class="FunctionName">text_field</span> <span class="UserDefinedConstant"><span class="UserDefinedConstant">:</span>commenter</span> %&gt;
1064
+ &#xA0;&#xA0;&lt;/<span class="TagName">div</span>&gt;
1065
+ &#xA0;&#xA0;&lt;<span class="TagName">div</span> <span class="TagAttribute">class</span>=<span class="String"><span class="String">"</span>field<span class="String">"</span></span>&gt;
1066
+ &#xA0;&#xA0;&#xA0;&#xA0;&lt;%= f.<span class="FunctionName">label</span> <span class="UserDefinedConstant"><span class="UserDefinedConstant">:</span>body</span> %&gt;&lt;<span class="TagName">br</span> /&gt;
1067
+ &#xA0;&#xA0;&#xA0;&#xA0;&lt;%= f.<span class="FunctionName">text_area</span> <span class="UserDefinedConstant"><span class="UserDefinedConstant">:</span>body</span> %&gt;
1068
+ &#xA0;&#xA0;&lt;/<span class="TagName">div</span>&gt;
1069
+ &#xA0;&#xA0;&lt;<span class="TagName">div</span> <span class="TagAttribute">class</span>=<span class="String"><span class="String">"</span>actions<span class="String">"</span></span>&gt;
1070
+ &#xA0;&#xA0;&#xA0;&#xA0;&lt;%= f.<span class="FunctionName">submit</span> %&gt;
1071
+ &#xA0;&#xA0;&lt;/<span class="TagName">div</span>&gt;
1072
+ &lt;% <span class="Keyword">end</span> %&gt;
1073
+
1074
+ &lt;<span class="TagName">br</span> /&gt;
1075
+
1076
+ &lt;%= link_to <span class="String"><span class="String">'</span>Edit Post<span class="String">'</span></span>, <span class="FunctionName">edit_post_path</span>(<span class="Variable"><span class="Variable">@</span>post</span>) %&gt; |
1077
+ &lt;%= link_to <span class="String"><span class="String">'</span>Back to Posts<span class="String">'</span></span>, posts_path %&gt; |
1078
+ </pre>
1079
+
1080
+
1081
+ <p>Now you can add posts and comments to your blog and have them show up in the right places.</p>
1082
+
1083
+
1084
+ </div>
1085
+ <div class="chapter"><h2 id="refactoring">Refactoring</h2>
1086
+
1087
+ <p>Now that we have Posts and Comments working, if we take a look at the <code>app/views/posts/show.html.erb</code> template, it&#x2019;s getting long and awkward. We can use partials to clean this up.</p>
1088
+
1089
+ <h3 id="rendering-partial-collections">Rendering Partial Collections</h3>
1090
+
1091
+ <p>First we will make a comment partial to extract showing all the comments for the post. Create the file <code>app/views/comments/_comment.html.erb</code> and put the following into it:</p>
1092
+
1093
+ <pre class="idle">&lt;<span class="TagName">p</span>&gt;
1094
+ &#xA0;&#xA0;&lt;<span class="TagName">b</span>&gt;Commenter:&lt;/<span class="TagName">b</span>&gt;
1095
+ &#xA0;&#xA0;&lt;%= comment.<span class="FunctionName">commenter</span> %&gt;
1096
+ &lt;/<span class="TagName">p</span>&gt;
1097
+
1098
+ &lt;<span class="TagName">p</span>&gt;
1099
+ &#xA0;&#xA0;&lt;<span class="TagName">b</span>&gt;Comment:&lt;/<span class="TagName">b</span>&gt;
1100
+ &#xA0;&#xA0;&lt;%= comment.<span class="FunctionName">body</span> %&gt;
1101
+ &lt;/<span class="TagName">p</span>&gt;
1102
+ </pre>
1103
+
1104
+
1105
+ <p>Then in the <code>app/views/posts/show.html.erb</code> you can change it to look like the following:</p>
1106
+
1107
+ <pre class="idle">&lt;<span class="TagName">p</span> <span class="TagAttribute">class</span>=<span class="String"><span class="String">"</span>notice<span class="String">"</span></span>&gt;&lt;%= notice %&gt;&lt;/<span class="TagName">p</span>&gt;
1108
+
1109
+ &lt;<span class="TagName">p</span>&gt;
1110
+ &#xA0;&#xA0;&lt;<span class="TagName">b</span>&gt;Name:&lt;/<span class="TagName">b</span>&gt;
1111
+ &#xA0;&#xA0;&lt;%= <span class="Variable"><span class="Variable">@</span>post</span>.<span class="FunctionName">name</span> %&gt;
1112
+ &lt;/<span class="TagName">p</span>&gt;
1113
+
1114
+ &lt;<span class="TagName">p</span>&gt;
1115
+ &#xA0;&#xA0;&lt;<span class="TagName">b</span>&gt;Title:&lt;/<span class="TagName">b</span>&gt;
1116
+ &#xA0;&#xA0;&lt;%= <span class="Variable"><span class="Variable">@</span>post</span>.<span class="FunctionName">title</span> %&gt;
1117
+ &lt;/<span class="TagName">p</span>&gt;
1118
+
1119
+ &lt;<span class="TagName">p</span>&gt;
1120
+ &#xA0;&#xA0;&lt;<span class="TagName">b</span>&gt;Content:&lt;/<span class="TagName">b</span>&gt;
1121
+ &#xA0;&#xA0;&lt;%= <span class="Variable"><span class="Variable">@</span>post</span>.<span class="FunctionName">content</span> %&gt;
1122
+ &lt;/<span class="TagName">p</span>&gt;
1123
+
1124
+ &lt;<span class="TagName">h2</span>&gt;Comments&lt;/<span class="TagName">h2</span>&gt;
1125
+ &lt;%= render <span class="UserDefinedConstant"><span class="UserDefinedConstant">:</span>partial</span> =&gt; <span class="String"><span class="String">"</span>comments/comment<span class="String">"</span></span>,
1126
+ &#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;<span class="UserDefinedConstant"><span class="UserDefinedConstant">:</span>collection</span> =&gt; <span class="Variable"><span class="Variable">@</span>post</span>.<span class="FunctionName">comments</span> %&gt;
1127
+
1128
+ &lt;<span class="TagName">h2</span>&gt;Add a comment:&lt;/<span class="TagName">h2</span>&gt;
1129
+ &lt;%= <span class="FunctionName">form_for</span>([<span class="Variable"><span class="Variable">@</span>post</span>, <span class="Variable"><span class="Variable">@</span>post</span>.<span class="FunctionName">comments</span>.<span class="FunctionName">build</span>]) <span class="Keyword">do </span>|<span class="Variable">f</span>| %&gt;
1130
+ &#xA0;&#xA0;&lt;<span class="TagName">div</span> <span class="TagAttribute">class</span>=<span class="String"><span class="String">"</span>field<span class="String">"</span></span>&gt;
1131
+ &#xA0;&#xA0;&#xA0;&#xA0;&lt;%= f.<span class="FunctionName">label</span> <span class="UserDefinedConstant"><span class="UserDefinedConstant">:</span>commenter</span> %&gt;&lt;<span class="TagName">br</span> /&gt;
1132
+ &#xA0;&#xA0;&#xA0;&#xA0;&lt;%= f.<span class="FunctionName">text_field</span> <span class="UserDefinedConstant"><span class="UserDefinedConstant">:</span>commenter</span> %&gt;
1133
+ &#xA0;&#xA0;&lt;/<span class="TagName">div</span>&gt;
1134
+ &#xA0;&#xA0;&lt;<span class="TagName">div</span> <span class="TagAttribute">class</span>=<span class="String"><span class="String">"</span>field<span class="String">"</span></span>&gt;
1135
+ &#xA0;&#xA0;&#xA0;&#xA0;&lt;%= f.<span class="FunctionName">label</span> <span class="UserDefinedConstant"><span class="UserDefinedConstant">:</span>body</span> %&gt;&lt;<span class="TagName">br</span> /&gt;
1136
+ &#xA0;&#xA0;&#xA0;&#xA0;&lt;%= f.<span class="FunctionName">text_area</span> <span class="UserDefinedConstant"><span class="UserDefinedConstant">:</span>body</span> %&gt;
1137
+ &#xA0;&#xA0;&lt;/<span class="TagName">div</span>&gt;
1138
+ &#xA0;&#xA0;&lt;<span class="TagName">div</span> <span class="TagAttribute">class</span>=<span class="String"><span class="String">"</span>actions<span class="String">"</span></span>&gt;
1139
+ &#xA0;&#xA0;&#xA0;&#xA0;&lt;%= f.<span class="FunctionName">submit</span> %&gt;
1140
+ &#xA0;&#xA0;&lt;/<span class="TagName">div</span>&gt;
1141
+ &lt;% <span class="Keyword">end</span> %&gt;
1142
+
1143
+ &lt;<span class="TagName">br</span> /&gt;
1144
+
1145
+ &lt;%= link_to <span class="String"><span class="String">'</span>Edit Post<span class="String">'</span></span>, <span class="FunctionName">edit_post_path</span>(<span class="Variable"><span class="Variable">@</span>post</span>) %&gt; |
1146
+ &lt;%= link_to <span class="String"><span class="String">'</span>Back to Posts<span class="String">'</span></span>, posts_path %&gt; |
1147
+ </pre>
1148
+
1149
+
1150
+ <p>This will now render the partial in <code>app/views/comments/_comment.html.erb</code> once for each comment that is in the <code>@post.comments collection</code>. As the render method iterates over the <code>@post.comments</code> collection, it assigns each comment to a local variable named the same as the partial, in this case comment which is then available in the partial for us to show.</p>
1151
+
1152
+ <h3 id="rendering-a-partial-form">Rendering a Partial Form</h3>
1153
+
1154
+ <p>Lets also move that new comment section out to it&#x2019;s own partial, again, you create a file <code>app/views/comments/_form.html.erb</code> and in it you put:</p>
1155
+
1156
+ <pre class="idle">&lt;%= <span class="FunctionName">form_for</span>([<span class="Variable"><span class="Variable">@</span>post</span>, <span class="Variable"><span class="Variable">@</span>post</span>.<span class="FunctionName">comments</span>.<span class="FunctionName">build</span>]) <span class="Keyword">do </span>|<span class="Variable">f</span>| %&gt;
1157
+ &#xA0;&#xA0;&lt;<span class="TagName">div</span> <span class="TagAttribute">class</span>=<span class="String"><span class="String">"</span>field<span class="String">"</span></span>&gt;
1158
+ &#xA0;&#xA0;&#xA0;&#xA0;&lt;%= f.<span class="FunctionName">label</span> <span class="UserDefinedConstant"><span class="UserDefinedConstant">:</span>commenter</span> %&gt;&lt;<span class="TagName">br</span> /&gt;
1159
+ &#xA0;&#xA0;&#xA0;&#xA0;&lt;%= f.<span class="FunctionName">text_field</span> <span class="UserDefinedConstant"><span class="UserDefinedConstant">:</span>commenter</span> %&gt;
1160
+ &#xA0;&#xA0;&lt;/<span class="TagName">div</span>&gt;
1161
+ &#xA0;&#xA0;&lt;<span class="TagName">div</span> <span class="TagAttribute">class</span>=<span class="String"><span class="String">"</span>field<span class="String">"</span></span>&gt;
1162
+ &#xA0;&#xA0;&#xA0;&#xA0;&lt;%= f.<span class="FunctionName">label</span> <span class="UserDefinedConstant"><span class="UserDefinedConstant">:</span>body</span> %&gt;&lt;<span class="TagName">br</span> /&gt;
1163
+ &#xA0;&#xA0;&#xA0;&#xA0;&lt;%= f.<span class="FunctionName">text_area</span> <span class="UserDefinedConstant"><span class="UserDefinedConstant">:</span>body</span> %&gt;
1164
+ &#xA0;&#xA0;&lt;/<span class="TagName">div</span>&gt;
1165
+ &#xA0;&#xA0;&lt;<span class="TagName">div</span> <span class="TagAttribute">class</span>=<span class="String"><span class="String">"</span>actions<span class="String">"</span></span>&gt;
1166
+ &#xA0;&#xA0;&#xA0;&#xA0;&lt;%= f.<span class="FunctionName">submit</span> %&gt;
1167
+ &#xA0;&#xA0;&lt;/<span class="TagName">div</span>&gt;
1168
+ &lt;% <span class="Keyword">end</span> %&gt;
1169
+ </pre>
1170
+
1171
+
1172
+ <p>Then you make the <code>app/views/posts/show.html.erb</code> look like the following:</p>
1173
+
1174
+ <pre class="idle">&lt;<span class="TagName">p</span> <span class="TagAttribute">class</span>=<span class="String"><span class="String">"</span>notice<span class="String">"</span></span>&gt;&lt;%= notice %&gt;&lt;/<span class="TagName">p</span>&gt;
1175
+
1176
+ &lt;<span class="TagName">p</span>&gt;
1177
+ &#xA0;&#xA0;&lt;<span class="TagName">b</span>&gt;Name:&lt;/<span class="TagName">b</span>&gt;
1178
+ &#xA0;&#xA0;&lt;%= <span class="Variable"><span class="Variable">@</span>post</span>.<span class="FunctionName">name</span> %&gt;
1179
+ &lt;/<span class="TagName">p</span>&gt;
1180
+
1181
+ &lt;<span class="TagName">p</span>&gt;
1182
+ &#xA0;&#xA0;&lt;<span class="TagName">b</span>&gt;Title:&lt;/<span class="TagName">b</span>&gt;
1183
+ &#xA0;&#xA0;&lt;%= <span class="Variable"><span class="Variable">@</span>post</span>.<span class="FunctionName">title</span> %&gt;
1184
+ &lt;/<span class="TagName">p</span>&gt;
1185
+
1186
+ &lt;<span class="TagName">p</span>&gt;
1187
+ &#xA0;&#xA0;&lt;<span class="TagName">b</span>&gt;Content:&lt;/<span class="TagName">b</span>&gt;
1188
+ &#xA0;&#xA0;&lt;%= <span class="Variable"><span class="Variable">@</span>post</span>.<span class="FunctionName">content</span> %&gt;
1189
+ &lt;/<span class="TagName">p</span>&gt;
1190
+
1191
+ &lt;<span class="TagName">h2</span>&gt;Comments&lt;/<span class="TagName">h2</span>&gt;
1192
+ &lt;%= render <span class="UserDefinedConstant"><span class="UserDefinedConstant">:</span>partial</span> =&gt; <span class="String"><span class="String">"</span>comments/comment<span class="String">"</span></span>,
1193
+ &#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;<span class="UserDefinedConstant"><span class="UserDefinedConstant">:</span>collection</span> =&gt; <span class="Variable"><span class="Variable">@</span>post</span>.<span class="FunctionName">comments</span> %&gt;
1194
+
1195
+ &lt;<span class="TagName">h2</span>&gt;Add a comment:&lt;/<span class="TagName">h2</span>&gt;
1196
+ &lt;%= render <span class="String"><span class="String">"</span>comments/form<span class="String">"</span></span> %&gt;
1197
+
1198
+ &lt;<span class="TagName">br</span> /&gt;
1199
+
1200
+ &lt;%= link_to <span class="String"><span class="String">'</span>Edit Post<span class="String">'</span></span>, <span class="FunctionName">edit_post_path</span>(<span class="Variable"><span class="Variable">@</span>post</span>) %&gt; |
1201
+ &lt;%= link_to <span class="String"><span class="String">'</span>Back to Posts<span class="String">'</span></span>, posts_path %&gt; |
1202
+ </pre>
1203
+
1204
+
1205
+ <p>The second render just defines the partial template we want to render, <code>comments/form</code>, Rails is smart enough to spot the forward slash in that string and realize that you want to render the <code>_form.html.erb</code> file in the <code>app/views/comments</code> directory.</p>
1206
+
1207
+ <p>The <code>@post</code> object is available to any partials rendered in the view because we defined it as an instance variable.</p>
1208
+
1209
+
1210
+ </div>
1211
+ <div class="chapter"><h2 id="deleting-comments">Deleting Comments</h2>
1212
+
1213
+ <p>Another important feature on a blog is being able to delete SPAM comments. To do this, we need to implement a link of some sort in the view and a <code>DELETE</code> action in the <code>CommentsController</code>.</p>
1214
+
1215
+ <p>So first, let&#x2019;s add the delete link in the <code>app/views/comments/_comment.html.erb</code> partial:</p>
1216
+
1217
+ <pre class="idle">&lt;<span class="TagName">p</span>&gt;
1218
+ &#xA0;&#xA0;&lt;<span class="TagName">b</span>&gt;Commenter:&lt;/<span class="TagName">b</span>&gt;
1219
+ &#xA0;&#xA0;&lt;%= comment.<span class="FunctionName">commenter</span> %&gt;
1220
+ &lt;/<span class="TagName">p</span>&gt;
1221
+
1222
+ &lt;<span class="TagName">p</span>&gt;
1223
+ &#xA0;&#xA0;&lt;<span class="TagName">b</span>&gt;Comment:&lt;/<span class="TagName">b</span>&gt;
1224
+ &#xA0;&#xA0;&lt;%= comment.<span class="FunctionName">body</span> %&gt;
1225
+ &lt;/<span class="TagName">p</span>&gt;
1226
+
1227
+ &lt;<span class="TagName">p</span>&gt;
1228
+ &#xA0;&#xA0;&lt;%= link_to <span class="String"><span class="String">'</span>Destroy Comment<span class="String">'</span></span>, [comment.<span class="FunctionName">post</span>, comment],
1229
+ &#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;<span class="UserDefinedConstant"><span class="UserDefinedConstant">:</span>confirm</span> =&gt; <span class="String"><span class="String">'</span>Are you sure?<span class="String">'</span></span>,
1230
+ &#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;<span class="UserDefinedConstant"><span class="UserDefinedConstant">:</span>method</span> =&gt; <span class="UserDefinedConstant"><span class="UserDefinedConstant">:</span>delete</span> %&gt;
1231
+ &lt;/<span class="TagName">p</span>&gt;
1232
+ </pre>
1233
+
1234
+
1235
+ <p>Clicking this new &#x201C;Destroy Comment&#x201D; link will fire off a <code>DELETE /posts/:id/comments/:id</code> to our <code>CommentsController</code>, which can then use this to find the comment we want to delete, so let&#x2019;s add a <code>destroy</code> action to our controller:</p>
1236
+
1237
+ <pre class="idle"><span class="Keyword">class</span> <span class="TypeName">CommentsController<span class="InheritedClass"> <span class="InheritedClass">&lt;</span> ApplicationController</span></span>
1238
+ &#xA0;&#xA0;def create
1239
+ &#xA0;&#xA0;&#xA0;&#xA0;<span class="Variable"><span class="Variable">@</span>post</span> <span class="Keyword">=</span> <span class="LibraryClassType">Post</span>.<span class="FunctionName">find</span>(params[<span class="UserDefinedConstant"><span class="UserDefinedConstant">:</span>post_id</span>])
1240
+ &#xA0;&#xA0;&#xA0;&#xA0;<span class="Variable"><span class="Variable">@</span>comment</span> <span class="Keyword">=</span> <span class="Variable"><span class="Variable">@</span>post</span>.<span class="FunctionName">comments</span>.<span class="FunctionName">create</span>(params[<span class="UserDefinedConstant"><span class="UserDefinedConstant">:</span>comment</span>])
1241
+ &#xA0;&#xA0;&#xA0;&#xA0;redirect_to <span class="FunctionName">post_path</span>(<span class="Variable"><span class="Variable">@</span>post</span>)
1242
+ &#xA0;&#xA0;<span class="Keyword">end</span>
1243
+
1244
+ &#xA0;&#xA0;def destroy
1245
+ &#xA0;&#xA0;&#xA0;&#xA0;<span class="Variable"><span class="Variable">@</span>post</span> <span class="Keyword">=</span> <span class="LibraryClassType">Post</span>.<span class="FunctionName">find</span>(params[<span class="UserDefinedConstant"><span class="UserDefinedConstant">:</span>post_id</span>])
1246
+ &#xA0;&#xA0;&#xA0;&#xA0;<span class="Variable"><span class="Variable">@</span>comment</span> <span class="Keyword">=</span> <span class="Variable"><span class="Variable">@</span>post</span>.<span class="FunctionName">comments</span>.<span class="FunctionName">find</span>(params[<span class="UserDefinedConstant"><span class="UserDefinedConstant">:</span>id</span>])
1247
+ &#xA0;&#xA0;&#xA0;&#xA0;<span class="Variable"><span class="Variable">@</span>comment</span>.<span class="FunctionName">destroy</span>
1248
+ &#xA0;&#xA0;&#xA0;&#xA0;redirect_to <span class="FunctionName">post_path</span>(<span class="Variable"><span class="Variable">@</span>post</span>)
1249
+ &#xA0;&#xA0;<span class="Keyword">end</span>
1250
+ <span class="Keyword">end</span>
1251
+ </pre>
1252
+
1253
+
1254
+ <p>The <code>destroy</code> action will find the post we are looking at, locate the comment within the <code>@post.comments</code> collection, and then remove it from the database and send us back to the <code>show</code> action for the post.</p>
1255
+
1256
+ <h3 id="deleting-associated-objects">Deleting Associated Objects</h3>
1257
+
1258
+ <p>If you delete a post then its associated comments will also need to be deleted. Otherwise they would simply occupy space in the database. Rails allows you to use the <code>dependent</code> option of an association to achieve this. Modify the <code>Post</code> model, <code>app/models/post.rb</code>, as follows:</p>
1259
+
1260
+ <pre class="idle"><span class="Keyword">class</span> <span class="TypeName">Post<span class="InheritedClass"> <span class="InheritedClass">&lt;</span> ActiveRecord::Base</span></span>
1261
+ &#xA0;&#xA0;validates <span class="UserDefinedConstant"><span class="UserDefinedConstant">:</span>name</span>,&#xA0; <span class="UserDefinedConstant"><span class="UserDefinedConstant">:</span>presence</span> =&gt; <span class="BuiltInConstant">true</span>
1262
+ &#xA0;&#xA0;validates <span class="UserDefinedConstant"><span class="UserDefinedConstant">:</span>title</span>, <span class="UserDefinedConstant"><span class="UserDefinedConstant">:</span>presence</span> =&gt; <span class="BuiltInConstant">true</span>,
1263
+ &#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;<span class="UserDefinedConstant"><span class="UserDefinedConstant">:</span>length</span> =&gt; { <span class="UserDefinedConstant"><span class="UserDefinedConstant">:</span>minimum</span> =&gt; <span class="Number">5</span> }
1264
+ &#xA0;&#xA0;has_many <span class="UserDefinedConstant"><span class="UserDefinedConstant">:</span>comments</span>, <span class="UserDefinedConstant"><span class="UserDefinedConstant">:</span>dependent</span> =&gt; <span class="UserDefinedConstant"><span class="UserDefinedConstant">:</span>destroy</span>
1265
+ <span class="Keyword">end</span>
1266
+ </pre>
1267
+
1268
+
1269
+
1270
+ </div>
1271
+ <div class="chapter"><h2 id="security">Security</h2>
1272
+
1273
+ <p>If you were to publish your blog online, anybody would be able to add, edit and delete posts or delete comments.</p>
1274
+
1275
+ <p>Rails provides a very simple HTTP authentication system that will work nicely in this situation. First, we enable simple HTTP based authentication in our <code>app/controllers/application_controller.rb</code>:</p>
1276
+
1277
+ <pre class="idle"><span class="Keyword">class</span> <span class="TypeName">ApplicationController<span class="InheritedClass"> <span class="InheritedClass">&lt;</span> ActionController::Base</span></span>
1278
+ &#xA0;&#xA0;protect_from_forgery
1279
+
1280
+ &#xA0;&#xA0;<span class="Keyword">private</span>
1281
+
1282
+ &#xA0;&#xA0;def authenticate
1283
+ &#xA0;&#xA0;&#xA0;&#xA0;authenticate_or_request_with_http_basic <span class="Keyword">do </span>|<span class="Variable">user_name</span>, <span class="Variable">password</span>|
1284
+ &#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;user_name <span class="Keyword">==</span> <span class="String"><span class="String">'</span>admin<span class="String">'</span></span> <span class="Keyword">&amp;&amp;</span> password <span class="Keyword">==</span> <span class="String"><span class="String">'</span>password<span class="String">'</span></span>
1285
+ &#xA0;&#xA0;&#xA0;&#xA0;<span class="Keyword">end</span>
1286
+ &#xA0;&#xA0;<span class="Keyword">end</span>
1287
+ <span class="Keyword">end</span>
1288
+ </pre>
1289
+
1290
+
1291
+ <p>You can obviously change the username and password to whatever you want. We put this method inside of <code>ApplicationController</code> so that it is available to all of our controllers.</p>
1292
+
1293
+ <p>Then in the <code>PostsController</code> we need to have a way to block access to the various actions if the person is not authenticated, here we can use the Rails <code>before_filter</code> method, which allows us to specify that Rails must run a method and only then allow access to the requested action if that method allows it.</p>
1294
+
1295
+ <p>To use the before filter, we specify it at the top of our <code>PostsController</code>, in this case, we want the user to be authenticated on every action, except for <code>index</code> and <code>show</code>, so we write that:</p>
1296
+
1297
+ <pre class="idle"><span class="Keyword">class</span> <span class="TypeName">PostsController<span class="InheritedClass"> <span class="InheritedClass">&lt;</span> ApplicationController</span></span>
1298
+
1299
+ &#xA0;&#xA0;before_filter <span class="UserDefinedConstant"><span class="UserDefinedConstant">:</span>authenticate</span>, <span class="UserDefinedConstant"><span class="UserDefinedConstant">:</span>except</span> =&gt; [<span class="UserDefinedConstant"><span class="UserDefinedConstant">:</span>index</span>, <span class="UserDefinedConstant"><span class="UserDefinedConstant">:</span>show</span>]
1300
+
1301
+ &#xA0;&#xA0;<span class="Comment"><span class="Comment">#</span> GET /posts</span>
1302
+ &#xA0;&#xA0;<span class="Comment"><span class="Comment">#</span> GET /posts.xml</span>
1303
+ &#xA0;&#xA0;def index
1304
+ &#xA0;&#xA0;&#xA0;&#xA0;<span class="Variable"><span class="Variable">@</span>posts</span> <span class="Keyword">=</span> <span class="LibraryClassType">Post</span>.<span class="FunctionName">all</span>
1305
+ &#xA0;&#xA0;&#xA0;&#xA0;respond_to <span class="Keyword">do </span>|<span class="Variable">format</span>|
1306
+ <span class="Comment"><span class="Comment">#</span> snipped for brevity</span>
1307
+ </pre>
1308
+
1309
+
1310
+
1311
+
1312
+ <p class="figure">
1313
+ <img src="../images/challenge.png" alt="Basic Auth challenge window" /></p>
1314
+
1315
+
1316
+ <p>We also only want to allow authenticated users to delete comments, so in the <code>CommentsController</code> we write:</p>
1317
+
1318
+ <pre class="idle"><span class="Keyword">class</span> <span class="TypeName">CommentsController<span class="InheritedClass"> <span class="InheritedClass">&lt;</span> ApplicationController</span></span>
1319
+
1320
+ &#xA0;&#xA0;before_filter <span class="UserDefinedConstant"><span class="UserDefinedConstant">:</span>authenticate</span>, <span class="UserDefinedConstant"><span class="UserDefinedConstant">:</span>only</span> =&gt; <span class="UserDefinedConstant"><span class="UserDefinedConstant">:</span>destroy</span>
1321
+
1322
+ &#xA0;&#xA0;def create
1323
+ &#xA0;&#xA0;&#xA0;&#xA0;<span class="Variable"><span class="Variable">@</span>post</span> <span class="Keyword">=</span> <span class="LibraryClassType">Post</span>.<span class="FunctionName">find</span>(params[<span class="UserDefinedConstant"><span class="UserDefinedConstant">:</span>post_id</span>])
1324
+ <span class="Comment"><span class="Comment">#</span> snipped for brevity</span>
1325
+ </pre>
1326
+
1327
+
1328
+ <p>Now if you try to create a new post, you will be greeted with a basic HTTP Authentication challenge.</p>
1329
+
1330
+
1331
+ </div>
1332
+ <div class="chapter"><h2 id="building-a-multi-model-form">Building a Multi-Model Form</h2>
1333
+
1334
+ <p>Another feature of your average blog is the ability to tag posts. To implement this feature your application needs to interact with more than one model on a single form. Rails offers support for nested forms.</p>
1335
+
1336
+ <p>To demonstrate this, we will add support for giving each post multiple tags, right in the form where you create the post. First, create a new model to hold the tags:</p>
1337
+
1338
+ <pre><code>$ rails generate model tag name:string post:references
1339
+ </code></pre>
1340
+
1341
+ <p>Again, run the migration to create the database table:</p>
1342
+
1343
+ <pre><code>$ rake db:migrate
1344
+ </code></pre>
1345
+
1346
+ <p>Next, edit the <code>post.rb</code> file to create the other side of the association, and to tell Rails (via the <code>accepts_nested_attributes_for</code> macro) that you intend to edit tags via posts:</p>
1347
+
1348
+ <pre class="idle"><span class="Keyword">class</span> <span class="TypeName">Post<span class="InheritedClass"> <span class="InheritedClass">&lt;</span> ActiveRecord::Base</span></span>
1349
+ &#xA0;&#xA0;validates <span class="UserDefinedConstant"><span class="UserDefinedConstant">:</span>name</span>,&#xA0; <span class="UserDefinedConstant"><span class="UserDefinedConstant">:</span>presence</span> =&gt; <span class="BuiltInConstant">true</span>
1350
+ &#xA0;&#xA0;validates <span class="UserDefinedConstant"><span class="UserDefinedConstant">:</span>title</span>, <span class="UserDefinedConstant"><span class="UserDefinedConstant">:</span>presence</span> =&gt; <span class="BuiltInConstant">true</span>,
1351
+ &#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;<span class="UserDefinedConstant"><span class="UserDefinedConstant">:</span>length</span> =&gt; { <span class="UserDefinedConstant"><span class="UserDefinedConstant">:</span>minimum</span> =&gt; <span class="Number">5</span> }
1352
+
1353
+ &#xA0;&#xA0;has_many <span class="UserDefinedConstant"><span class="UserDefinedConstant">:</span>comments</span>, <span class="UserDefinedConstant"><span class="UserDefinedConstant">:</span>dependent</span> =&gt; <span class="UserDefinedConstant"><span class="UserDefinedConstant">:</span>destroy</span>
1354
+ &#xA0;&#xA0;has_many <span class="UserDefinedConstant"><span class="UserDefinedConstant">:</span>tags</span>
1355
+
1356
+ &#xA0;&#xA0;accepts_nested_attributes_for <span class="UserDefinedConstant"><span class="UserDefinedConstant">:</span>tags</span>, <span class="UserDefinedConstant"><span class="UserDefinedConstant">:</span>allow_destroy</span> =&gt; <span class="UserDefinedConstant"><span class="UserDefinedConstant">:</span>true</span>,
1357
+ &#xA0;&#xA0;&#xA0;&#xA0;<span class="UserDefinedConstant"><span class="UserDefinedConstant">:</span>reject_if</span> =&gt; proc { |<span class="Variable">attrs</span>| attrs.<span class="FunctionName">all?</span> { |<span class="Variable">k</span>, <span class="Variable">v</span>| v.<span class="FunctionName">blank?</span> } }
1358
+ <span class="Keyword">end</span>
1359
+ </pre>
1360
+
1361
+
1362
+ <p>The <code>:allow_destroy</code> option on the nested attribute declaration tells Rails to display a &#x201C;remove&#x201D; checkbox on the view that you&#x2019;ll build shortly. The <code>:reject_if</code> option prevents saving new tags that do not have any attributes filled in.</p>
1363
+
1364
+ <p>We will modify <code>views/posts/_form.html.erb</code> to render a partial to make a tag:</p>
1365
+
1366
+ <pre class="idle">&lt;% <span class="Variable"><span class="Variable">@</span>post</span>.<span class="FunctionName">tags</span>.<span class="FunctionName">build</span> %&gt;
1367
+ &lt;%= <span class="FunctionName">form_for</span>(<span class="Variable"><span class="Variable">@</span>post</span>) <span class="Keyword">do </span>|<span class="Variable">post_form</span>| %&gt;
1368
+ &#xA0;&#xA0;&lt;% <span class="Keyword">if</span> <span class="Variable"><span class="Variable">@</span>post</span>.<span class="FunctionName">errors</span>.<span class="FunctionName">any?</span> %&gt;
1369
+ &#xA0;&#xA0;&lt;<span class="TagName">div</span> <span class="TagAttribute">id</span>=<span class="String"><span class="String">"</span><span class="String">errorExplanation</span><span class="String">"</span></span>&gt;
1370
+ &#xA0;&#xA0;&#xA0;&#xA0;&lt;<span class="TagName">h2</span>&gt;&lt;%= <span class="FunctionName">pluralize</span>(<span class="Variable"><span class="Variable">@</span>post</span>.<span class="FunctionName">errors</span>.<span class="FunctionName">count</span>, <span class="String"><span class="String">"</span>error<span class="String">"</span></span>) %&gt; prohibited this post from being saved:&lt;/<span class="TagName">h2</span>&gt;
1371
+ &#xA0;&#xA0;&#xA0;&#xA0;&lt;<span class="TagName">ul</span>&gt;
1372
+ &#xA0;&#xA0;&#xA0;&#xA0;&lt;% <span class="Variable"><span class="Variable">@</span>post</span>.<span class="FunctionName">errors</span>.<span class="FunctionName">full_messages</span>.<span class="FunctionName">each</span> <span class="Keyword">do </span>|<span class="Variable">msg</span>| %&gt;
1373
+ &#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&lt;<span class="TagName">li</span>&gt;&lt;%= msg %&gt;&lt;/<span class="TagName">li</span>&gt;
1374
+ &#xA0;&#xA0;&#xA0;&#xA0;&lt;% <span class="Keyword">end</span> %&gt;
1375
+ &#xA0;&#xA0;&#xA0;&#xA0;&lt;/<span class="TagName">ul</span>&gt;
1376
+ &#xA0;&#xA0;&lt;/<span class="TagName">div</span>&gt;
1377
+ &#xA0;&#xA0;&lt;% <span class="Keyword">end</span> %&gt;
1378
+
1379
+ &#xA0;&#xA0;&lt;<span class="TagName">div</span> <span class="TagAttribute">class</span>=<span class="String"><span class="String">"</span>field<span class="String">"</span></span>&gt;
1380
+ &#xA0;&#xA0;&#xA0;&#xA0;&lt;%= post_form.<span class="FunctionName">label</span> <span class="UserDefinedConstant"><span class="UserDefinedConstant">:</span>name</span> %&gt;&lt;<span class="TagName">br</span> /&gt;
1381
+ &#xA0;&#xA0;&#xA0;&#xA0;&lt;%= post_form.<span class="FunctionName">text_field</span> <span class="UserDefinedConstant"><span class="UserDefinedConstant">:</span>name</span> %&gt;
1382
+ &#xA0;&#xA0;&lt;/<span class="TagName">div</span>&gt;
1383
+ &#xA0;&#xA0;&lt;<span class="TagName">div</span> <span class="TagAttribute">class</span>=<span class="String"><span class="String">"</span>field<span class="String">"</span></span>&gt;
1384
+ &#xA0;&#xA0;&#xA0;&#xA0;&lt;%= post_form.<span class="FunctionName">label</span> <span class="UserDefinedConstant"><span class="UserDefinedConstant">:</span>title</span> %&gt;&lt;<span class="TagName">br</span> /&gt;
1385
+ &#xA0;&#xA0;&#xA0;&#xA0;&lt;%= post_form.<span class="FunctionName">text_field</span> <span class="UserDefinedConstant"><span class="UserDefinedConstant">:</span>title</span> %&gt;
1386
+ &#xA0;&#xA0;&lt;/<span class="TagName">div</span>&gt;
1387
+ &#xA0;&#xA0;&lt;<span class="TagName">div</span> <span class="TagAttribute">class</span>=<span class="String"><span class="String">"</span>field<span class="String">"</span></span>&gt;
1388
+ &#xA0;&#xA0;&#xA0;&#xA0;&lt;%= post_form.<span class="FunctionName">label</span> <span class="UserDefinedConstant"><span class="UserDefinedConstant">:</span>content</span> %&gt;&lt;<span class="TagName">br</span> /&gt;
1389
+ &#xA0;&#xA0;&#xA0;&#xA0;&lt;%= post_form.<span class="FunctionName">text_area</span> <span class="UserDefinedConstant"><span class="UserDefinedConstant">:</span>content</span> %&gt;
1390
+ &#xA0;&#xA0;&lt;/<span class="TagName">div</span>&gt;
1391
+ &#xA0;&#xA0;&lt;<span class="TagName">h2</span>&gt;Tags&lt;/<span class="TagName">h2</span>&gt;
1392
+ &#xA0;&#xA0;&lt;%= render <span class="UserDefinedConstant"><span class="UserDefinedConstant">:</span>partial</span> =&gt; <span class="String"><span class="String">'</span>tags/form<span class="String">'</span></span>,
1393
+ &#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;<span class="UserDefinedConstant"><span class="UserDefinedConstant">:</span>locals</span> =&gt; {<span class="UserDefinedConstant"><span class="UserDefinedConstant">:</span>form</span> =&gt; post_form} %&gt;
1394
+ &#xA0;&#xA0;&lt;<span class="TagName">div</span> <span class="TagAttribute">class</span>=<span class="String"><span class="String">"</span>actions<span class="String">"</span></span>&gt;
1395
+ &#xA0;&#xA0;&#xA0;&#xA0;&lt;%= post_form.<span class="FunctionName">submit</span> %&gt;
1396
+ &#xA0;&#xA0;&lt;/<span class="TagName">div</span>&gt;
1397
+ &lt;% <span class="Keyword">end</span> %&gt;
1398
+ </pre>
1399
+
1400
+
1401
+ <p>Note that we have changed the <code>f in form_for(@post) do |f|</code> to <code>post_form</code> to make it easier to understand what is going on.</p>
1402
+
1403
+ <p>This example shows another option of the render helper, being able to pass in local variables, in this case, we want the local variable <code>form</code> in the partial to refer to the <code>post_form</code> object.</p>
1404
+
1405
+ <p>We also add a <code>@post.tags.build</code> at the top of this form, this is to make sure there is a new tag ready to have it&#x2019;s name filled in by the user. If you do not build the new tag, then the form will not appear as there is no new Tag object ready to create.</p>
1406
+
1407
+ <p>Now create the folder <code>app/views/tags</code> and make a file in there called <code>_form.html.erb</code> which contains the form for the tag:</p>
1408
+
1409
+ <pre class="idle">&lt;%= form.<span class="FunctionName">fields_for</span> <span class="UserDefinedConstant"><span class="UserDefinedConstant">:</span>tags</span> <span class="Keyword">do </span>|<span class="Variable">tag_form</span>| %&gt;
1410
+ &#xA0;&#xA0;&lt;<span class="TagName">div</span> <span class="TagAttribute">class</span>=<span class="String"><span class="String">"</span>field<span class="String">"</span></span>&gt;
1411
+ &#xA0;&#xA0;&#xA0;&#xA0;&lt;%= tag_form.<span class="FunctionName">label</span> <span class="UserDefinedConstant"><span class="UserDefinedConstant">:</span>name</span>, <span class="String"><span class="String">'</span>Tag:<span class="String">'</span></span> %&gt;
1412
+ &#xA0;&#xA0;&#xA0;&#xA0;&lt;%= tag_form.<span class="FunctionName">text_field</span> <span class="UserDefinedConstant"><span class="UserDefinedConstant">:</span>name</span> %&gt;
1413
+ &#xA0;&#xA0;&lt;/<span class="TagName">div</span>&gt;
1414
+ &#xA0;&#xA0;&lt;% <span class="Keyword">unless</span> tag_form.<span class="FunctionName">object</span>.<span class="FunctionName">nil?</span> <span class="Keyword">||</span> tag_form.<span class="FunctionName">object</span>.<span class="FunctionName">new_record?</span> %&gt;
1415
+ &#xA0;&#xA0;&#xA0;&#xA0;&lt;<span class="TagName">div</span> <span class="TagAttribute">class</span>=<span class="String"><span class="String">"</span>field<span class="String">"</span></span>&gt;
1416
+ &#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&lt;%= tag_form.<span class="FunctionName">label</span> <span class="UserDefinedConstant"><span class="UserDefinedConstant">:</span>_destroy</span>, <span class="String"><span class="String">'</span>Remove:<span class="String">'</span></span> %&gt;
1417
+ &#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&lt;%= tag_form.<span class="FunctionName">check_box</span> <span class="UserDefinedConstant"><span class="UserDefinedConstant">:</span>_destroy</span> %&gt;
1418
+ &#xA0;&#xA0;&#xA0;&#xA0;&lt;/<span class="TagName">div</span>&gt;
1419
+ &#xA0;&#xA0;&lt;% <span class="Keyword">end</span> %&gt;
1420
+ &lt;% <span class="Keyword">end</span> %&gt;
1421
+ </pre>
1422
+
1423
+
1424
+ <p>Finally, we will edit the <code>app/views/posts/show.html.erb</code> template to show our tags.</p>
1425
+
1426
+ <pre class="idle">&lt;<span class="TagName">p</span> <span class="TagAttribute">class</span>=<span class="String"><span class="String">"</span>notice<span class="String">"</span></span>&gt;&lt;%= notice %&gt;&lt;/<span class="TagName">p</span>&gt;
1427
+
1428
+ &lt;<span class="TagName">p</span>&gt;
1429
+ &#xA0;&#xA0;&lt;<span class="TagName">b</span>&gt;Name:&lt;/<span class="TagName">b</span>&gt;
1430
+ &#xA0;&#xA0;&lt;%= <span class="Variable"><span class="Variable">@</span>post</span>.<span class="FunctionName">name</span> %&gt;
1431
+ &lt;/<span class="TagName">p</span>&gt;
1432
+
1433
+ &lt;<span class="TagName">p</span>&gt;
1434
+ &#xA0;&#xA0;&lt;<span class="TagName">b</span>&gt;Title:&lt;/<span class="TagName">b</span>&gt;
1435
+ &#xA0;&#xA0;&lt;%= <span class="Variable"><span class="Variable">@</span>post</span>.<span class="FunctionName">title</span> %&gt;
1436
+ &lt;/<span class="TagName">p</span>&gt;
1437
+
1438
+ &lt;<span class="TagName">p</span>&gt;
1439
+ &#xA0;&#xA0;&lt;<span class="TagName">b</span>&gt;Content:&lt;/<span class="TagName">b</span>&gt;
1440
+ &#xA0;&#xA0;&lt;%= <span class="Variable"><span class="Variable">@</span>post</span>.<span class="FunctionName">content</span> %&gt;
1441
+ &lt;/<span class="TagName">p</span>&gt;
1442
+
1443
+ &lt;<span class="TagName">p</span>&gt;
1444
+ &#xA0;&#xA0;&lt;<span class="TagName">b</span>&gt;Tags:&lt;/<span class="TagName">b</span>&gt;
1445
+ &#xA0;&#xA0;&lt;%= <span class="Variable"><span class="Variable">@</span>post</span>.<span class="FunctionName">tags</span>.<span class="FunctionName">map</span> { |<span class="Variable">t</span>| t.<span class="FunctionName">name</span> }.<span class="FunctionName">join</span>(<span class="String"><span class="String">"</span>, <span class="String">"</span></span>) %&gt;
1446
+ &lt;/<span class="TagName">p</span>&gt;
1447
+
1448
+ &lt;<span class="TagName">h2</span>&gt;Comments&lt;/<span class="TagName">h2</span>&gt;
1449
+ &lt;%= render <span class="UserDefinedConstant"><span class="UserDefinedConstant">:</span>partial</span> =&gt; <span class="String"><span class="String">"</span>comments/comment<span class="String">"</span></span>,
1450
+ &#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;<span class="UserDefinedConstant"><span class="UserDefinedConstant">:</span>collection</span> =&gt; <span class="Variable"><span class="Variable">@</span>post</span>.<span class="FunctionName">comments</span> %&gt;
1451
+
1452
+ &lt;<span class="TagName">h2</span>&gt;Add a comment:&lt;/<span class="TagName">h2</span>&gt;
1453
+ &lt;%= render <span class="String"><span class="String">"</span>comments/form<span class="String">"</span></span> %&gt;
1454
+
1455
+
1456
+ &lt;%= link_to <span class="String"><span class="String">'</span>Edit Post<span class="String">'</span></span>, <span class="FunctionName">edit_post_path</span>(<span class="Variable"><span class="Variable">@</span>post</span>) %&gt; |
1457
+ &lt;%= link_to <span class="String"><span class="String">'</span>Back to Posts<span class="String">'</span></span>, posts_path %&gt; |
1458
+ </pre>
1459
+
1460
+
1461
+ <p>With these changes in place, you&#x2019;ll find that you can edit a post and its tags directly on the same view.</p>
1462
+
1463
+ <p>However, that method call <code>@post.tags.map { |t| t.name }.join(", ")</code> is awkward, we could handle this by making a helper method.</p>
1464
+
1465
+
1466
+ </div>
1467
+ <div class="chapter"><h2 id="view-helpers">View Helpers</h2>
1468
+
1469
+ <p>View Helpers live in <code>app/helpers</code> and provide small snippets of reusable code for views. In our case, we want a method that strings a bunch of objects together using their name attribute and joining them with a comma. As this is for the Post show template, we put it in the <code>PostsHelper</code>.</p>
1470
+
1471
+ <p>Open up <code>app/helpers/posts_helper.rb</code> and add the following:</p>
1472
+
1473
+ <pre class="idle"><span class="Keyword">module</span> <span class="TypeName">PostsHelper</span>
1474
+ &#xA0;&#xA0;def <span class="FunctionName">join_tags</span>(post)
1475
+ &#xA0;&#xA0;&#xA0;&#xA0;post.<span class="FunctionName">tags</span>.<span class="FunctionName">map</span> { |<span class="Variable">t</span>| t.<span class="FunctionName">name</span> }.<span class="FunctionName">join</span>(<span class="String"><span class="String">"</span>, <span class="String">"</span></span>)
1476
+ &#xA0;&#xA0;<span class="Keyword">end</span>
1477
+ <span class="Keyword">end</span>
1478
+ </pre>
1479
+
1480
+
1481
+ <p>Now you can edit the view in <code>app/views/posts/show.html.erb</code> to look like this:</p>
1482
+
1483
+ <pre class="idle">&lt;<span class="TagName">p</span> <span class="TagAttribute">class</span>=<span class="String"><span class="String">"</span>notice<span class="String">"</span></span>&gt;&lt;%= notice %&gt;&lt;/<span class="TagName">p</span>&gt;
1484
+
1485
+ &lt;<span class="TagName">p</span>&gt;
1486
+ &#xA0;&#xA0;&lt;<span class="TagName">b</span>&gt;Name:&lt;/<span class="TagName">b</span>&gt;
1487
+ &#xA0;&#xA0;&lt;%= <span class="Variable"><span class="Variable">@</span>post</span>.<span class="FunctionName">name</span> %&gt;
1488
+ &lt;/<span class="TagName">p</span>&gt;
1489
+
1490
+ &lt;<span class="TagName">p</span>&gt;
1491
+ &#xA0;&#xA0;&lt;<span class="TagName">b</span>&gt;Title:&lt;/<span class="TagName">b</span>&gt;
1492
+ &#xA0;&#xA0;&lt;%= <span class="Variable"><span class="Variable">@</span>post</span>.<span class="FunctionName">title</span> %&gt;
1493
+ &lt;/<span class="TagName">p</span>&gt;
1494
+
1495
+ &lt;<span class="TagName">p</span>&gt;
1496
+ &#xA0;&#xA0;&lt;<span class="TagName">b</span>&gt;Content:&lt;/<span class="TagName">b</span>&gt;
1497
+ &#xA0;&#xA0;&lt;%= <span class="Variable"><span class="Variable">@</span>post</span>.<span class="FunctionName">content</span> %&gt;
1498
+ &lt;/<span class="TagName">p</span>&gt;
1499
+
1500
+ &lt;<span class="TagName">p</span>&gt;
1501
+ &#xA0;&#xA0;&lt;<span class="TagName">b</span>&gt;Tags:&lt;/<span class="TagName">b</span>&gt;
1502
+ &#xA0;&#xA0;&lt;%= <span class="FunctionName">join_tags</span>(<span class="Variable"><span class="Variable">@</span>post</span>) %&gt;
1503
+ &lt;/<span class="TagName">p</span>&gt;
1504
+
1505
+ &lt;<span class="TagName">h2</span>&gt;Comments&lt;/<span class="TagName">h2</span>&gt;
1506
+ &lt;%= render <span class="UserDefinedConstant"><span class="UserDefinedConstant">:</span>partial</span> =&gt; <span class="String"><span class="String">"</span>comments/comment<span class="String">"</span></span>,
1507
+ &#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;<span class="UserDefinedConstant"><span class="UserDefinedConstant">:</span>collection</span> =&gt; <span class="Variable"><span class="Variable">@</span>post</span>.<span class="FunctionName">comments</span> %&gt;
1508
+
1509
+ &lt;<span class="TagName">h2</span>&gt;Add a comment:&lt;/<span class="TagName">h2</span>&gt;
1510
+ &lt;%= render <span class="String"><span class="String">"</span>comments/form<span class="String">"</span></span> %&gt;
1511
+
1512
+
1513
+ &lt;%= link_to <span class="String"><span class="String">'</span>Edit Post<span class="String">'</span></span>, <span class="FunctionName">edit_post_path</span>(<span class="Variable"><span class="Variable">@</span>post</span>) %&gt; |
1514
+ &lt;%= link_to <span class="String"><span class="String">'</span>Back to Posts<span class="String">'</span></span>, posts_path %&gt; |
1515
+ </pre>
1516
+
1517
+
1518
+
1519
+ </div>
1520
+ <div class="chapter"><h2 id="whats-next">What&#x2019;s Next?</h2>
1521
+
1522
+ <p>Now that you&#x2019;ve seen your first Rails application, you should feel free to update it and experiment on your own. But you don&#x2019;t have to do everything without help. As you need assistance getting up and running with Rails, feel free to consult these support resources:</p>
1523
+
1524
+ <ul><li>The <a href="http://guides.rubyonrails.org/index.html">Ruby on Rails guides</a></li>
1525
+ <li>The <a href="http://railstutorial.org/book">Ruby on Rails Tutorial</a></li>
1526
+ <li>The <a href="http://groups.google.com/group/rubyonrails-talk">Ruby on Rails mailing list</a></li>
1527
+ <li>The <a href="irc://irc.freenode.net/#rubyonrails">#rubyonrails</a> channel on irc.freenode.net</li>
1528
+ <li>The <a href="http://wiki.rubyonrails.org/">Rails Wiki</a></li>
1529
+ </ul><p>Rails also comes with built-in help that you can generate using the rake command-line utility:</p>
1530
+
1531
+ <ul><li>Running <code>rake doc:guides</code> will put a full copy of the Rails Guides in the <code>doc/guides</code> folder of your application. Open <code>doc/guides/index.html</code> in your web browser to explore the Guides.</li>
1532
+ <li>Running <code>rake doc:rails</code> will put a full copy of the API documentation for Rails in the <code>doc/api</code> folder of your application. Open <code>doc/api/index.html</code> in your web browser to explore the API documentation.</li>
1533
+ </ul></div>
1534
+ <div class="chapter"><h2 id="configuration-gotchas">Configuration Gotchas</h2>
1535
+
1536
+ <p>The easiest way to work with Rails is to store all external data as UTF-8. If you don&#x2019;t, Ruby libraries and Rails will often be able to convert your native data into UTF-8, but this doesn&#x2019;t always work reliably, so you&#x2019;re better off ensuring that all external data is UTF-8.</p>
1537
+
1538
+ <p>If you have made a mistake in this area, the most common symptom is a black diamond with a question mark inside appearing in the browser. Another common symptom is characters like &#x201C;&#xC3;&#xBC;&#x201D; appearing instead of &#x201C;&#xFC;&#x201D;. Rails takes a number of internal steps to mitigate common causes of these problems that can be automatically detected and corrected. However, if you have external data that is not stored as UTF-8, it can occasionally result in these kinds of issues that cannot be automatically detected by Rails and corrected.</p>
1539
+
1540
+ <p>Two very common sources of data that are not UTF-8:</p>
1541
+
1542
+ <ul><li>Your text editor: Most text editors (such as Textmate), default to saving files as UTF-8. If your text editor does not, this can result in special characters that you enter in your templates (such as &#xE9;) to appear as a diamond with a question mark inside in the browser. This also applies to your I18N translation files. Most editors that do not already default to UTF-8 (such as some versions of Dreamweaver) offer a way to change the default to UTF-8. Do so.</li>
1543
+ <li>Your database. Rails defaults to converting data from your database into UTF-8 at the boundary. However, if your database is not using UTF-8 internally, it may not be able to store all characters that your users enter. For instance, if your database is using Latin-1 internally, and your user enters a Russian, Hebrew, or Japanese character, the data will be lost forever once it enters the database. If possible, use UTF-8 as the internal storage of your database.</li>
1544
+ </ul></div>
1545
+
1546
+ </div>
1547
+
1548
+ <div class="imprint container">
1549
+ <div>
1550
+ <h2>Rails Guides: Getting Started</h2>
1551
+ <p>Rails Documentation Team</p>
1552
+ <p>Copyright (C) 2010 Rails Documentation Team.</p>
1553
+ </div>
1554
+ </div>
1555
+ </body>
1556
+ </html>