openskies 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (494) hide show
  1. data/CHANGELOG.md +87 -0
  2. data/README.rdoc +35 -0
  3. data/bin/openskies +9 -0
  4. data/guides/assets/images/belongs_to.png +0 -0
  5. data/guides/assets/images/book_icon.gif +0 -0
  6. data/guides/assets/images/bullet.gif +0 -0
  7. data/guides/assets/images/challenge.png +0 -0
  8. data/guides/assets/images/chapters_icon.gif +0 -0
  9. data/guides/assets/images/check_bullet.gif +0 -0
  10. data/guides/assets/images/credits_pic_blank.gif +0 -0
  11. data/guides/assets/images/csrf.png +0 -0
  12. data/guides/assets/images/customized_error_messages.png +0 -0
  13. data/guides/assets/images/edge_badge.png +0 -0
  14. data/guides/assets/images/error_messages.png +0 -0
  15. data/guides/assets/images/feature_tile.gif +0 -0
  16. data/guides/assets/images/footer_tile.gif +0 -0
  17. data/guides/assets/images/fxn.png +0 -0
  18. data/guides/assets/images/grey_bullet.gif +0 -0
  19. data/guides/assets/images/habtm.png +0 -0
  20. data/guides/assets/images/has_many.png +0 -0
  21. data/guides/assets/images/has_many_through.png +0 -0
  22. data/guides/assets/images/has_one.png +0 -0
  23. data/guides/assets/images/has_one_through.png +0 -0
  24. data/guides/assets/images/header_backdrop.png +0 -0
  25. data/guides/assets/images/header_tile.gif +0 -0
  26. data/guides/assets/images/i18n/demo_html_safe.png +0 -0
  27. data/guides/assets/images/i18n/demo_localized_pirate.png +0 -0
  28. data/guides/assets/images/i18n/demo_translated_en.png +0 -0
  29. data/guides/assets/images/i18n/demo_translated_pirate.png +0 -0
  30. data/guides/assets/images/i18n/demo_translation_missing.png +0 -0
  31. data/guides/assets/images/i18n/demo_untranslated.png +0 -0
  32. data/guides/assets/images/icons/README +5 -0
  33. data/guides/assets/images/icons/callouts/1.png +0 -0
  34. data/guides/assets/images/icons/callouts/10.png +0 -0
  35. data/guides/assets/images/icons/callouts/11.png +0 -0
  36. data/guides/assets/images/icons/callouts/12.png +0 -0
  37. data/guides/assets/images/icons/callouts/13.png +0 -0
  38. data/guides/assets/images/icons/callouts/14.png +0 -0
  39. data/guides/assets/images/icons/callouts/15.png +0 -0
  40. data/guides/assets/images/icons/callouts/2.png +0 -0
  41. data/guides/assets/images/icons/callouts/3.png +0 -0
  42. data/guides/assets/images/icons/callouts/4.png +0 -0
  43. data/guides/assets/images/icons/callouts/5.png +0 -0
  44. data/guides/assets/images/icons/callouts/6.png +0 -0
  45. data/guides/assets/images/icons/callouts/7.png +0 -0
  46. data/guides/assets/images/icons/callouts/8.png +0 -0
  47. data/guides/assets/images/icons/callouts/9.png +0 -0
  48. data/guides/assets/images/icons/caution.png +0 -0
  49. data/guides/assets/images/icons/example.png +0 -0
  50. data/guides/assets/images/icons/home.png +0 -0
  51. data/guides/assets/images/icons/important.png +0 -0
  52. data/guides/assets/images/icons/next.png +0 -0
  53. data/guides/assets/images/icons/note.png +0 -0
  54. data/guides/assets/images/icons/prev.png +0 -0
  55. data/guides/assets/images/icons/tip.png +0 -0
  56. data/guides/assets/images/icons/up.png +0 -0
  57. data/guides/assets/images/icons/warning.png +0 -0
  58. data/guides/assets/images/jaimeiniesta.jpg +0 -0
  59. data/guides/assets/images/nav_arrow.gif +0 -0
  60. data/guides/assets/images/polymorphic.png +0 -0
  61. data/guides/assets/images/posts_index.png +0 -0
  62. data/guides/assets/images/radar.png +0 -0
  63. data/guides/assets/images/rails_guides_kindle_cover.jpg +0 -0
  64. data/guides/assets/images/rails_guides_logo.gif +0 -0
  65. data/guides/assets/images/rails_logo_remix.gif +0 -0
  66. data/guides/assets/images/rails_welcome.png +0 -0
  67. data/guides/assets/images/session_fixation.png +0 -0
  68. data/guides/assets/images/tab_grey.gif +0 -0
  69. data/guides/assets/images/tab_info.gif +0 -0
  70. data/guides/assets/images/tab_note.gif +0 -0
  71. data/guides/assets/images/tab_red.gif +0 -0
  72. data/guides/assets/images/tab_yellow.gif +0 -0
  73. data/guides/assets/images/tab_yellow.png +0 -0
  74. data/guides/assets/images/validation_error_messages.png +0 -0
  75. data/guides/assets/images/vijaydev.jpg +0 -0
  76. data/guides/assets/javascripts/guides.js +7 -0
  77. data/guides/assets/javascripts/syntaxhighlighter/shBrushAS3.js +59 -0
  78. data/guides/assets/javascripts/syntaxhighlighter/shBrushAppleScript.js +75 -0
  79. data/guides/assets/javascripts/syntaxhighlighter/shBrushBash.js +59 -0
  80. data/guides/assets/javascripts/syntaxhighlighter/shBrushCSharp.js +65 -0
  81. data/guides/assets/javascripts/syntaxhighlighter/shBrushColdFusion.js +100 -0
  82. data/guides/assets/javascripts/syntaxhighlighter/shBrushCpp.js +97 -0
  83. data/guides/assets/javascripts/syntaxhighlighter/shBrushCss.js +91 -0
  84. data/guides/assets/javascripts/syntaxhighlighter/shBrushDelphi.js +55 -0
  85. data/guides/assets/javascripts/syntaxhighlighter/shBrushDiff.js +41 -0
  86. data/guides/assets/javascripts/syntaxhighlighter/shBrushErlang.js +52 -0
  87. data/guides/assets/javascripts/syntaxhighlighter/shBrushGroovy.js +67 -0
  88. data/guides/assets/javascripts/syntaxhighlighter/shBrushJScript.js +52 -0
  89. data/guides/assets/javascripts/syntaxhighlighter/shBrushJava.js +57 -0
  90. data/guides/assets/javascripts/syntaxhighlighter/shBrushJavaFX.js +58 -0
  91. data/guides/assets/javascripts/syntaxhighlighter/shBrushPerl.js +72 -0
  92. data/guides/assets/javascripts/syntaxhighlighter/shBrushPhp.js +88 -0
  93. data/guides/assets/javascripts/syntaxhighlighter/shBrushPlain.js +33 -0
  94. data/guides/assets/javascripts/syntaxhighlighter/shBrushPowerShell.js +74 -0
  95. data/guides/assets/javascripts/syntaxhighlighter/shBrushPython.js +64 -0
  96. data/guides/assets/javascripts/syntaxhighlighter/shBrushRuby.js +55 -0
  97. data/guides/assets/javascripts/syntaxhighlighter/shBrushSass.js +94 -0
  98. data/guides/assets/javascripts/syntaxhighlighter/shBrushScala.js +51 -0
  99. data/guides/assets/javascripts/syntaxhighlighter/shBrushSql.js +66 -0
  100. data/guides/assets/javascripts/syntaxhighlighter/shBrushVb.js +56 -0
  101. data/guides/assets/javascripts/syntaxhighlighter/shBrushXml.js +69 -0
  102. data/guides/assets/javascripts/syntaxhighlighter/shCore.js +17 -0
  103. data/guides/assets/stylesheets/fixes.css +16 -0
  104. data/guides/assets/stylesheets/kindle.css +11 -0
  105. data/guides/assets/stylesheets/main.css +453 -0
  106. data/guides/assets/stylesheets/print.css +52 -0
  107. data/guides/assets/stylesheets/reset.css +43 -0
  108. data/guides/assets/stylesheets/style.css +13 -0
  109. data/guides/assets/stylesheets/syntaxhighlighter/shCore.css +226 -0
  110. data/guides/assets/stylesheets/syntaxhighlighter/shCoreDefault.css +328 -0
  111. data/guides/assets/stylesheets/syntaxhighlighter/shCoreDjango.css +331 -0
  112. data/guides/assets/stylesheets/syntaxhighlighter/shCoreEclipse.css +339 -0
  113. data/guides/assets/stylesheets/syntaxhighlighter/shCoreEmacs.css +324 -0
  114. data/guides/assets/stylesheets/syntaxhighlighter/shCoreFadeToGrey.css +328 -0
  115. data/guides/assets/stylesheets/syntaxhighlighter/shCoreMDUltra.css +324 -0
  116. data/guides/assets/stylesheets/syntaxhighlighter/shCoreMidnight.css +324 -0
  117. data/guides/assets/stylesheets/syntaxhighlighter/shCoreRDark.css +324 -0
  118. data/guides/assets/stylesheets/syntaxhighlighter/shThemeDefault.css +117 -0
  119. data/guides/assets/stylesheets/syntaxhighlighter/shThemeDjango.css +120 -0
  120. data/guides/assets/stylesheets/syntaxhighlighter/shThemeEclipse.css +128 -0
  121. data/guides/assets/stylesheets/syntaxhighlighter/shThemeEmacs.css +113 -0
  122. data/guides/assets/stylesheets/syntaxhighlighter/shThemeFadeToGrey.css +117 -0
  123. data/guides/assets/stylesheets/syntaxhighlighter/shThemeMDUltra.css +113 -0
  124. data/guides/assets/stylesheets/syntaxhighlighter/shThemeMidnight.css +113 -0
  125. data/guides/assets/stylesheets/syntaxhighlighter/shThemeRDark.css +113 -0
  126. data/guides/assets/stylesheets/syntaxhighlighter/shThemeRailsGuides.css +116 -0
  127. data/guides/code/getting_started/Gemfile +38 -0
  128. data/guides/code/getting_started/README.rdoc +261 -0
  129. data/guides/code/getting_started/Rakefile +7 -0
  130. data/guides/code/getting_started/app/assets/images/rails.png +0 -0
  131. data/guides/code/getting_started/app/assets/javascripts/application.js +15 -0
  132. data/guides/code/getting_started/app/assets/javascripts/comments.js.coffee +3 -0
  133. data/guides/code/getting_started/app/assets/javascripts/home.js.coffee +3 -0
  134. data/guides/code/getting_started/app/assets/javascripts/posts.js.coffee +3 -0
  135. data/guides/code/getting_started/app/assets/stylesheets/application.css +13 -0
  136. data/guides/code/getting_started/app/assets/stylesheets/comments.css.scss +3 -0
  137. data/guides/code/getting_started/app/assets/stylesheets/home.css.scss +3 -0
  138. data/guides/code/getting_started/app/assets/stylesheets/posts.css.scss +3 -0
  139. data/guides/code/getting_started/app/assets/stylesheets/scaffolds.css.scss +56 -0
  140. data/guides/code/getting_started/app/controllers/application_controller.rb +3 -0
  141. data/guides/code/getting_started/app/controllers/comments_controller.rb +16 -0
  142. data/guides/code/getting_started/app/controllers/home_controller.rb +5 -0
  143. data/guides/code/getting_started/app/controllers/posts_controller.rb +84 -0
  144. data/guides/code/getting_started/app/helpers/application_helper.rb +2 -0
  145. data/guides/code/getting_started/app/helpers/comments_helper.rb +2 -0
  146. data/guides/code/getting_started/app/helpers/home_helper.rb +2 -0
  147. data/guides/code/getting_started/app/helpers/posts_helper.rb +5 -0
  148. data/guides/code/getting_started/app/models/comment.rb +3 -0
  149. data/guides/code/getting_started/app/models/post.rb +11 -0
  150. data/guides/code/getting_started/app/models/tag.rb +3 -0
  151. data/guides/code/getting_started/app/views/comments/_comment.html.erb +15 -0
  152. data/guides/code/getting_started/app/views/comments/_form.html.erb +13 -0
  153. data/guides/code/getting_started/app/views/home/index.html.erb +2 -0
  154. data/guides/code/getting_started/app/views/layouts/application.html.erb +14 -0
  155. data/guides/code/getting_started/app/views/posts/_form.html.erb +32 -0
  156. data/guides/code/getting_started/app/views/posts/edit.html.erb +6 -0
  157. data/guides/code/getting_started/app/views/posts/index.html.erb +27 -0
  158. data/guides/code/getting_started/app/views/posts/new.html.erb +5 -0
  159. data/guides/code/getting_started/app/views/posts/show.html.erb +31 -0
  160. data/guides/code/getting_started/app/views/tags/_form.html.erb +12 -0
  161. data/guides/code/getting_started/config/application.rb +59 -0
  162. data/guides/code/getting_started/config/boot.rb +6 -0
  163. data/guides/code/getting_started/config/database.yml +25 -0
  164. data/guides/code/getting_started/config/environment.rb +5 -0
  165. data/guides/code/getting_started/config/environments/development.rb +37 -0
  166. data/guides/code/getting_started/config/environments/production.rb +67 -0
  167. data/guides/code/getting_started/config/environments/test.rb +37 -0
  168. data/guides/code/getting_started/config/initializers/backtrace_silencers.rb +7 -0
  169. data/guides/code/getting_started/config/initializers/inflections.rb +15 -0
  170. data/guides/code/getting_started/config/initializers/mime_types.rb +5 -0
  171. data/guides/code/getting_started/config/initializers/secret_token.rb +7 -0
  172. data/guides/code/getting_started/config/initializers/session_store.rb +8 -0
  173. data/guides/code/getting_started/config/initializers/wrap_parameters.rb +14 -0
  174. data/guides/code/getting_started/config/locales/en.yml +5 -0
  175. data/guides/code/getting_started/config/routes.rb +64 -0
  176. data/guides/code/getting_started/config.ru +4 -0
  177. data/guides/code/getting_started/db/migrate/20110901012504_create_posts.rb +11 -0
  178. data/guides/code/getting_started/db/migrate/20110901012815_create_comments.rb +12 -0
  179. data/guides/code/getting_started/db/migrate/20110901013701_create_tags.rb +11 -0
  180. data/guides/code/getting_started/db/schema.rb +43 -0
  181. data/guides/code/getting_started/db/seeds.rb +7 -0
  182. data/guides/code/getting_started/doc/README_FOR_APP +2 -0
  183. data/guides/code/getting_started/public/404.html +26 -0
  184. data/guides/code/getting_started/public/422.html +26 -0
  185. data/guides/code/getting_started/public/500.html +25 -0
  186. data/guides/code/getting_started/public/favicon.ico +0 -0
  187. data/guides/code/getting_started/public/robots.txt +5 -0
  188. data/guides/code/getting_started/script/rails +6 -0
  189. data/guides/code/getting_started/test/fixtures/comments.yml +11 -0
  190. data/guides/code/getting_started/test/fixtures/posts.yml +11 -0
  191. data/guides/code/getting_started/test/fixtures/tags.yml +9 -0
  192. data/guides/code/getting_started/test/functional/comments_controller_test.rb +7 -0
  193. data/guides/code/getting_started/test/functional/home_controller_test.rb +9 -0
  194. data/guides/code/getting_started/test/functional/posts_controller_test.rb +49 -0
  195. data/guides/code/getting_started/test/performance/browsing_test.rb +12 -0
  196. data/guides/code/getting_started/test/test_helper.rb +13 -0
  197. data/guides/code/getting_started/test/unit/comment_test.rb +7 -0
  198. data/guides/code/getting_started/test/unit/helpers/comments_helper_test.rb +4 -0
  199. data/guides/code/getting_started/test/unit/helpers/home_helper_test.rb +4 -0
  200. data/guides/code/getting_started/test/unit/helpers/posts_helper_test.rb +4 -0
  201. data/guides/code/getting_started/test/unit/post_test.rb +7 -0
  202. data/guides/code/getting_started/test/unit/tag_test.rb +7 -0
  203. data/guides/rails_guides/generator.rb +310 -0
  204. data/guides/rails_guides/helpers.rb +45 -0
  205. data/guides/rails_guides/indexer.rb +69 -0
  206. data/guides/rails_guides/levenshtein.rb +31 -0
  207. data/guides/rails_guides/textile_extensions.rb +63 -0
  208. data/guides/rails_guides.rb +50 -0
  209. data/guides/source/2_2_release_notes.textile +422 -0
  210. data/guides/source/2_3_release_notes.textile +610 -0
  211. data/guides/source/3_0_release_notes.textile +595 -0
  212. data/guides/source/3_1_release_notes.textile +553 -0
  213. data/guides/source/3_2_release_notes.textile +540 -0
  214. data/guides/source/_license.html.erb +2 -0
  215. data/guides/source/_welcome.html.erb +19 -0
  216. data/guides/source/action_controller_overview.textile +820 -0
  217. data/guides/source/action_mailer_basics.textile +516 -0
  218. data/guides/source/action_view_overview.textile +1497 -0
  219. data/guides/source/active_model_basics.textile +205 -0
  220. data/guides/source/active_record_basics.textile +218 -0
  221. data/guides/source/active_record_querying.textile +1433 -0
  222. data/guides/source/active_record_validations_callbacks.textile +1283 -0
  223. data/guides/source/active_resource_basics.textile +120 -0
  224. data/guides/source/active_support_core_extensions.textile +3713 -0
  225. data/guides/source/ajax_on_rails.textile +267 -0
  226. data/guides/source/api_documentation_guidelines.textile +185 -0
  227. data/guides/source/asset_pipeline.textile +707 -0
  228. data/guides/source/association_basics.textile +1959 -0
  229. data/guides/source/caching_with_rails.textile +437 -0
  230. data/guides/source/command_line.textile +574 -0
  231. data/guides/source/configuring.textile +641 -0
  232. data/guides/source/contributing_to_ruby_on_rails.textile +448 -0
  233. data/guides/source/credits.html.erb +72 -0
  234. data/guides/source/debugging_rails_applications.textile +714 -0
  235. data/guides/source/documents.yaml +157 -0
  236. data/guides/source/engines.textile +618 -0
  237. data/guides/source/form_helpers.textile +798 -0
  238. data/guides/source/generators.textile +621 -0
  239. data/guides/source/getting_started.textile +1929 -0
  240. data/guides/source/i18n.textile +931 -0
  241. data/guides/source/index.html.erb +30 -0
  242. data/guides/source/initialization.textile +1116 -0
  243. data/guides/source/kindle/KINDLE.md +26 -0
  244. data/guides/source/kindle/copyright.html.erb +1 -0
  245. data/guides/source/kindle/layout.html.erb +27 -0
  246. data/guides/source/kindle/rails_guides.opf.erb +52 -0
  247. data/guides/source/kindle/toc.html.erb +24 -0
  248. data/guides/source/kindle/toc.ncx.erb +64 -0
  249. data/guides/source/kindle/welcome.html.erb +5 -0
  250. data/guides/source/layout.html.erb +124 -0
  251. data/guides/source/layouts_and_rendering.textile +1237 -0
  252. data/guides/source/migrations.textile +900 -0
  253. data/guides/source/nested_model_forms.textile +222 -0
  254. data/guides/source/performance_testing.textile +597 -0
  255. data/guides/source/plugins.textile +464 -0
  256. data/guides/source/rails_application_templates.textile +240 -0
  257. data/guides/source/rails_on_rack.textile +236 -0
  258. data/guides/source/routing.textile +885 -0
  259. data/guides/source/ruby_on_rails_guides_guidelines.textile +79 -0
  260. data/guides/source/security.textile +1004 -0
  261. data/guides/source/testing.textile +947 -0
  262. data/guides/w3c_validator.rb +91 -0
  263. data/lib/rails/all.rb +15 -0
  264. data/lib/rails/application/bootstrap.rb +73 -0
  265. data/lib/rails/application/configuration.rb +151 -0
  266. data/lib/rails/application/finisher.rb +98 -0
  267. data/lib/rails/application/railties.rb +13 -0
  268. data/lib/rails/application/route_inspector.rb +84 -0
  269. data/lib/rails/application/routes_reloader.rb +56 -0
  270. data/lib/rails/application.rb +320 -0
  271. data/lib/rails/backtrace_cleaner.rb +42 -0
  272. data/lib/rails/cli.rb +16 -0
  273. data/lib/rails/code_statistics.rb +118 -0
  274. data/lib/rails/commands/application.rb +38 -0
  275. data/lib/rails/commands/benchmarker.rb +34 -0
  276. data/lib/rails/commands/console.rb +55 -0
  277. data/lib/rails/commands/dbconsole.rb +123 -0
  278. data/lib/rails/commands/destroy.rb +10 -0
  279. data/lib/rails/commands/generate.rb +12 -0
  280. data/lib/rails/commands/plugin.rb +544 -0
  281. data/lib/rails/commands/plugin_new.rb +11 -0
  282. data/lib/rails/commands/profiler.rb +32 -0
  283. data/lib/rails/commands/runner.rb +54 -0
  284. data/lib/rails/commands/server.rb +100 -0
  285. data/lib/rails/commands/update.rb +9 -0
  286. data/lib/rails/commands.rb +104 -0
  287. data/lib/rails/configuration.rb +89 -0
  288. data/lib/rails/console/app.rb +36 -0
  289. data/lib/rails/console/helpers.rb +11 -0
  290. data/lib/rails/engine/commands.rb +43 -0
  291. data/lib/rails/engine/configuration.rb +84 -0
  292. data/lib/rails/engine/railties.rb +33 -0
  293. data/lib/rails/engine.rb +657 -0
  294. data/lib/rails/generators/actions.rb +325 -0
  295. data/lib/rails/generators/active_model.rb +78 -0
  296. data/lib/rails/generators/app_base.rb +281 -0
  297. data/lib/rails/generators/base.rb +390 -0
  298. data/lib/rails/generators/css/assets/assets_generator.rb +13 -0
  299. data/lib/rails/generators/css/assets/templates/stylesheet.css +4 -0
  300. data/lib/rails/generators/css/scaffold/scaffold_generator.rb +16 -0
  301. data/lib/rails/generators/erb/controller/controller_generator.rb +20 -0
  302. data/lib/rails/generators/erb/controller/templates/view.html.erb +2 -0
  303. data/lib/rails/generators/erb/mailer/mailer_generator.rb +13 -0
  304. data/lib/rails/generators/erb/mailer/templates/view.text.erb +3 -0
  305. data/lib/rails/generators/erb/scaffold/scaffold_generator.rb +29 -0
  306. data/lib/rails/generators/erb/scaffold/templates/_form.html.erb +23 -0
  307. data/lib/rails/generators/erb/scaffold/templates/edit.html.erb +6 -0
  308. data/lib/rails/generators/erb/scaffold/templates/index.html.erb +27 -0
  309. data/lib/rails/generators/erb/scaffold/templates/new.html.erb +5 -0
  310. data/lib/rails/generators/erb/scaffold/templates/show.html.erb +12 -0
  311. data/lib/rails/generators/erb.rb +21 -0
  312. data/lib/rails/generators/generated_attribute.rb +107 -0
  313. data/lib/rails/generators/js/assets/assets_generator.rb +13 -0
  314. data/lib/rails/generators/js/assets/templates/javascript.js +2 -0
  315. data/lib/rails/generators/migration.rb +68 -0
  316. data/lib/rails/generators/named_base.rb +202 -0
  317. data/lib/rails/generators/rails/app/USAGE +15 -0
  318. data/lib/rails/generators/rails/app/app_generator.rb +303 -0
  319. data/lib/rails/generators/rails/app/templates/Gemfile +26 -0
  320. data/lib/rails/generators/rails/app/templates/README +261 -0
  321. data/lib/rails/generators/rails/app/templates/Rakefile +7 -0
  322. data/lib/rails/generators/rails/app/templates/app/assets/images/rails.png +0 -0
  323. data/lib/rails/generators/rails/app/templates/app/assets/javascripts/application.js.tt +17 -0
  324. data/lib/rails/generators/rails/app/templates/app/assets/stylesheets/application.css +13 -0
  325. data/lib/rails/generators/rails/app/templates/app/controllers/application_controller.rb +3 -0
  326. data/lib/rails/generators/rails/app/templates/app/helpers/application_helper.rb +2 -0
  327. data/lib/rails/generators/rails/app/templates/app/mailers/.empty_directory +0 -0
  328. data/lib/rails/generators/rails/app/templates/app/models/.empty_directory +0 -0
  329. data/lib/rails/generators/rails/app/templates/app/views/layouts/application.html.erb.tt +14 -0
  330. data/lib/rails/generators/rails/app/templates/config/application.rb +74 -0
  331. data/lib/rails/generators/rails/app/templates/config/boot.rb +6 -0
  332. data/lib/rails/generators/rails/app/templates/config/databases/frontbase.yml +31 -0
  333. data/lib/rails/generators/rails/app/templates/config/databases/ibm_db.yml +86 -0
  334. data/lib/rails/generators/rails/app/templates/config/databases/jdbc.yml +62 -0
  335. data/lib/rails/generators/rails/app/templates/config/databases/jdbcmysql.yml +33 -0
  336. data/lib/rails/generators/rails/app/templates/config/databases/jdbcpostgresql.yml +43 -0
  337. data/lib/rails/generators/rails/app/templates/config/databases/jdbcsqlite3.yml +20 -0
  338. data/lib/rails/generators/rails/app/templates/config/databases/mysql.yml +54 -0
  339. data/lib/rails/generators/rails/app/templates/config/databases/oracle.yml +39 -0
  340. data/lib/rails/generators/rails/app/templates/config/databases/postgresql.yml +55 -0
  341. data/lib/rails/generators/rails/app/templates/config/databases/sqlite3.yml +25 -0
  342. data/lib/rails/generators/rails/app/templates/config/environment.rb +5 -0
  343. data/lib/rails/generators/rails/app/templates/config/environments/development.rb.tt +41 -0
  344. data/lib/rails/generators/rails/app/templates/config/environments/production.rb.tt +73 -0
  345. data/lib/rails/generators/rails/app/templates/config/environments/test.rb.tt +39 -0
  346. data/lib/rails/generators/rails/app/templates/config/initializers/backtrace_silencers.rb +7 -0
  347. data/lib/rails/generators/rails/app/templates/config/initializers/inflections.rb +15 -0
  348. data/lib/rails/generators/rails/app/templates/config/initializers/mime_types.rb +5 -0
  349. data/lib/rails/generators/rails/app/templates/config/initializers/secret_token.rb.tt +7 -0
  350. data/lib/rails/generators/rails/app/templates/config/initializers/session_store.rb.tt +8 -0
  351. data/lib/rails/generators/rails/app/templates/config/initializers/wrap_parameters.rb.tt +16 -0
  352. data/lib/rails/generators/rails/app/templates/config/locales/en.yml +5 -0
  353. data/lib/rails/generators/rails/app/templates/config/routes.rb +58 -0
  354. data/lib/rails/generators/rails/app/templates/config.ru +4 -0
  355. data/lib/rails/generators/rails/app/templates/db/seeds.rb.tt +7 -0
  356. data/lib/rails/generators/rails/app/templates/doc/README_FOR_APP +2 -0
  357. data/lib/rails/generators/rails/app/templates/gitignore +15 -0
  358. data/lib/rails/generators/rails/app/templates/public/404.html +26 -0
  359. data/lib/rails/generators/rails/app/templates/public/422.html +26 -0
  360. data/lib/rails/generators/rails/app/templates/public/500.html +25 -0
  361. data/lib/rails/generators/rails/app/templates/public/favicon.ico +0 -0
  362. data/lib/rails/generators/rails/app/templates/public/index.html +241 -0
  363. data/lib/rails/generators/rails/app/templates/public/robots.txt +5 -0
  364. data/lib/rails/generators/rails/app/templates/public/stylesheets/.empty_directory +0 -0
  365. data/lib/rails/generators/rails/app/templates/script/rails +5 -0
  366. data/lib/rails/generators/rails/app/templates/test/fixtures/.empty_directory +0 -0
  367. data/lib/rails/generators/rails/app/templates/test/functional/.empty_directory +0 -0
  368. data/lib/rails/generators/rails/app/templates/test/integration/.empty_directory +0 -0
  369. data/lib/rails/generators/rails/app/templates/test/performance/browsing_test.rb +12 -0
  370. data/lib/rails/generators/rails/app/templates/test/test_helper.rb +15 -0
  371. data/lib/rails/generators/rails/app/templates/test/unit/.empty_directory +0 -0
  372. data/lib/rails/generators/rails/assets/USAGE +20 -0
  373. data/lib/rails/generators/rails/assets/assets_generator.rb +25 -0
  374. data/lib/rails/generators/rails/assets/templates/javascript.js +2 -0
  375. data/lib/rails/generators/rails/assets/templates/stylesheet.css +4 -0
  376. data/lib/rails/generators/rails/controller/USAGE +18 -0
  377. data/lib/rails/generators/rails/controller/controller_generator.rb +20 -0
  378. data/lib/rails/generators/rails/controller/templates/controller.rb +13 -0
  379. data/lib/rails/generators/rails/generator/USAGE +12 -0
  380. data/lib/rails/generators/rails/generator/generator_generator.rb +25 -0
  381. data/lib/rails/generators/rails/generator/templates/%file_name%_generator.rb.tt +3 -0
  382. data/lib/rails/generators/rails/generator/templates/USAGE.tt +8 -0
  383. data/lib/rails/generators/rails/generator/templates/templates/.empty_directory +0 -0
  384. data/lib/rails/generators/rails/helper/USAGE +17 -0
  385. data/lib/rails/generators/rails/helper/helper_generator.rb +13 -0
  386. data/lib/rails/generators/rails/helper/templates/helper.rb +4 -0
  387. data/lib/rails/generators/rails/integration_test/USAGE +10 -0
  388. data/lib/rails/generators/rails/integration_test/integration_test_generator.rb +7 -0
  389. data/lib/rails/generators/rails/migration/USAGE +29 -0
  390. data/lib/rails/generators/rails/migration/migration_generator.rb +8 -0
  391. data/lib/rails/generators/rails/model/USAGE +45 -0
  392. data/lib/rails/generators/rails/model/model_generator.rb +8 -0
  393. data/lib/rails/generators/rails/observer/USAGE +12 -0
  394. data/lib/rails/generators/rails/observer/observer_generator.rb +7 -0
  395. data/lib/rails/generators/rails/performance_test/USAGE +10 -0
  396. data/lib/rails/generators/rails/performance_test/performance_test_generator.rb +7 -0
  397. data/lib/rails/generators/rails/plugin_new/USAGE +10 -0
  398. data/lib/rails/generators/rails/plugin_new/plugin_new_generator.rb +318 -0
  399. data/lib/rails/generators/rails/plugin_new/templates/%name%.gemspec +29 -0
  400. data/lib/rails/generators/rails/plugin_new/templates/Gemfile +23 -0
  401. data/lib/rails/generators/rails/plugin_new/templates/MIT-LICENSE +20 -0
  402. data/lib/rails/generators/rails/plugin_new/templates/README.rdoc +3 -0
  403. data/lib/rails/generators/rails/plugin_new/templates/Rakefile +31 -0
  404. data/lib/rails/generators/rails/plugin_new/templates/app/controllers/%name%/application_controller.rb.tt +4 -0
  405. data/lib/rails/generators/rails/plugin_new/templates/app/helpers/%name%/application_helper.rb.tt +4 -0
  406. data/lib/rails/generators/rails/plugin_new/templates/app/mailers/.empty_directory +0 -0
  407. data/lib/rails/generators/rails/plugin_new/templates/app/models/.empty_directory +0 -0
  408. data/lib/rails/generators/rails/plugin_new/templates/app/views/layouts/%name%/application.html.erb.tt +14 -0
  409. data/lib/rails/generators/rails/plugin_new/templates/config/routes.rb +6 -0
  410. data/lib/rails/generators/rails/plugin_new/templates/gitignore +7 -0
  411. data/lib/rails/generators/rails/plugin_new/templates/lib/%name%/engine.rb +7 -0
  412. data/lib/rails/generators/rails/plugin_new/templates/lib/%name%/version.rb +3 -0
  413. data/lib/rails/generators/rails/plugin_new/templates/lib/%name%.rb +6 -0
  414. data/lib/rails/generators/rails/plugin_new/templates/lib/tasks/%name%_tasks.rake +4 -0
  415. data/lib/rails/generators/rails/plugin_new/templates/rails/application.rb +18 -0
  416. data/lib/rails/generators/rails/plugin_new/templates/rails/boot.rb +10 -0
  417. data/lib/rails/generators/rails/plugin_new/templates/rails/routes.rb +4 -0
  418. data/lib/rails/generators/rails/plugin_new/templates/script/rails.tt +7 -0
  419. data/lib/rails/generators/rails/plugin_new/templates/test/%name%_test.rb +7 -0
  420. data/lib/rails/generators/rails/plugin_new/templates/test/integration/navigation_test.rb +12 -0
  421. data/lib/rails/generators/rails/plugin_new/templates/test/test_helper.rb +15 -0
  422. data/lib/rails/generators/rails/resource/USAGE +23 -0
  423. data/lib/rails/generators/rails/resource/resource_generator.rb +20 -0
  424. data/lib/rails/generators/rails/resource_route/resource_route_generator.rb +50 -0
  425. data/lib/rails/generators/rails/scaffold/USAGE +35 -0
  426. data/lib/rails/generators/rails/scaffold/scaffold_generator.rb +23 -0
  427. data/lib/rails/generators/rails/scaffold/templates/scaffold.css +56 -0
  428. data/lib/rails/generators/rails/scaffold_controller/USAGE +19 -0
  429. data/lib/rails/generators/rails/scaffold_controller/scaffold_controller_generator.rb +25 -0
  430. data/lib/rails/generators/rails/scaffold_controller/templates/controller.rb +89 -0
  431. data/lib/rails/generators/rails/session_migration/USAGE +8 -0
  432. data/lib/rails/generators/rails/session_migration/session_migration_generator.rb +8 -0
  433. data/lib/rails/generators/rails/task/USAGE +9 -0
  434. data/lib/rails/generators/rails/task/task_generator.rb +12 -0
  435. data/lib/rails/generators/rails/task/templates/task.rb +8 -0
  436. data/lib/rails/generators/resource_helpers.rb +80 -0
  437. data/lib/rails/generators/test_case.rb +247 -0
  438. data/lib/rails/generators/test_unit/controller/controller_generator.rb +15 -0
  439. data/lib/rails/generators/test_unit/controller/templates/functional_test.rb +19 -0
  440. data/lib/rails/generators/test_unit/helper/helper_generator.rb +13 -0
  441. data/lib/rails/generators/test_unit/helper/templates/helper_test.rb +6 -0
  442. data/lib/rails/generators/test_unit/integration/integration_generator.rb +13 -0
  443. data/lib/rails/generators/test_unit/integration/templates/integration_test.rb +7 -0
  444. data/lib/rails/generators/test_unit/mailer/mailer_generator.rb +14 -0
  445. data/lib/rails/generators/test_unit/mailer/templates/functional_test.rb +21 -0
  446. data/lib/rails/generators/test_unit/model/model_generator.rb +24 -0
  447. data/lib/rails/generators/test_unit/model/templates/fixtures.yml +23 -0
  448. data/lib/rails/generators/test_unit/model/templates/unit_test.rb +9 -0
  449. data/lib/rails/generators/test_unit/observer/observer_generator.rb +13 -0
  450. data/lib/rails/generators/test_unit/observer/templates/unit_test.rb +9 -0
  451. data/lib/rails/generators/test_unit/performance/performance_generator.rb +13 -0
  452. data/lib/rails/generators/test_unit/performance/templates/performance_test.rb +12 -0
  453. data/lib/rails/generators/test_unit/plugin/plugin_generator.rb +13 -0
  454. data/lib/rails/generators/test_unit/plugin/templates/%file_name%_test.rb.tt +7 -0
  455. data/lib/rails/generators/test_unit/plugin/templates/test_helper.rb +3 -0
  456. data/lib/rails/generators/test_unit/scaffold/scaffold_generator.rb +38 -0
  457. data/lib/rails/generators/test_unit/scaffold/templates/functional_test.rb +51 -0
  458. data/lib/rails/generators/test_unit.rb +8 -0
  459. data/lib/rails/generators.rb +339 -0
  460. data/lib/rails/info.rb +116 -0
  461. data/lib/rails/info_controller.rb +15 -0
  462. data/lib/rails/initializable.rb +89 -0
  463. data/lib/rails/paths.rb +211 -0
  464. data/lib/rails/performance_test_help.rb +3 -0
  465. data/lib/rails/plugin.rb +92 -0
  466. data/lib/rails/rack/debugger.rb +24 -0
  467. data/lib/rails/rack/log_tailer.rb +34 -0
  468. data/lib/rails/rack/logger.rb +60 -0
  469. data/lib/rails/rack.rb +7 -0
  470. data/lib/rails/railtie/configurable.rb +35 -0
  471. data/lib/rails/railtie/configuration.rb +90 -0
  472. data/lib/rails/railtie.rb +202 -0
  473. data/lib/rails/ruby_version_check.rb +23 -0
  474. data/lib/rails/rubyprof_ext.rb +35 -0
  475. data/lib/rails/script_rails_loader.rb +29 -0
  476. data/lib/rails/source_annotation_extractor.rb +104 -0
  477. data/lib/rails/tasks/annotations.rake +20 -0
  478. data/lib/rails/tasks/documentation.rake +155 -0
  479. data/lib/rails/tasks/engine.rake +72 -0
  480. data/lib/rails/tasks/framework.rake +76 -0
  481. data/lib/rails/tasks/log.rake +9 -0
  482. data/lib/rails/tasks/middleware.rake +7 -0
  483. data/lib/rails/tasks/misc.rake +67 -0
  484. data/lib/rails/tasks/routes.rake +9 -0
  485. data/lib/rails/tasks/statistics.rake +16 -0
  486. data/lib/rails/tasks/tmp.rake +37 -0
  487. data/lib/rails/tasks.rb +16 -0
  488. data/lib/rails/test_help.rb +56 -0
  489. data/lib/rails/test_unit/railtie.rb +15 -0
  490. data/lib/rails/test_unit/sub_test_task.rb +8 -0
  491. data/lib/rails/test_unit/testing.rake +146 -0
  492. data/lib/rails/version.rb +10 -0
  493. data/lib/rails.rb +126 -0
  494. metadata +609 -0
@@ -0,0 +1,1237 @@
1
+ h2. Layouts and Rendering in Rails
2
+
3
+ This guide covers the basic layout features of Action Controller and Action View. By referring to this guide, you will be able to:
4
+
5
+ * Use the various rendering methods built into Rails
6
+ * Create layouts with multiple content sections
7
+ * Use partials to DRY up your views
8
+ * Use nested layouts (sub-templates)
9
+
10
+ endprologue.
11
+
12
+ h3. Overview: How the Pieces Fit Together
13
+
14
+ This guide focuses on the interaction between Controller and View in the Model-View-Controller triangle. As you know, the Controller is responsible for orchestrating the whole process of handling a request in Rails, though it normally hands off any heavy code to the Model. But then, when it's time to send a response back to the user, the Controller hands things off to the View. It's that handoff that is the subject of this guide.
15
+
16
+ In broad strokes, this involves deciding what should be sent as the response and calling an appropriate method to create that response. If the response is a full-blown view, Rails also does some extra work to wrap the view in a layout and possibly to pull in partial views. You'll see all of those paths later in this guide.
17
+
18
+ h3. Creating Responses
19
+
20
+ From the controller's point of view, there are three ways to create an HTTP response:
21
+
22
+ * Call +render+ to create a full response to send back to the browser
23
+ * Call +redirect_to+ to send an HTTP redirect status code to the browser
24
+ * Call +head+ to create a response consisting solely of HTTP headers to send back to the browser
25
+
26
+ I'll cover each of these methods in turn. But first, a few words about the very easiest thing that the controller can do to create a response: nothing at all.
27
+
28
+ h4. Rendering by Default: Convention Over Configuration in Action
29
+
30
+ You've heard that Rails promotes "convention over configuration". Default rendering is an excellent example of this. By default, controllers in Rails automatically render views with names that correspond to valid routes. For example, if you have this code in your +BooksController+ class:
31
+
32
+ <ruby>
33
+ class BooksController < ApplicationController
34
+ end
35
+ </ruby>
36
+
37
+ And the following in your routes file:
38
+
39
+ <ruby>
40
+ resources :books
41
+ </ruby>
42
+
43
+ And you have a view file +app/views/books/index.html.erb+:
44
+
45
+ <ruby>
46
+ <h1>Books are coming soon!</h1>
47
+ </ruby>
48
+
49
+ Rails will automatically render +app/views/books/index.html.erb+ when you navigate to +/books+ and you will see "Books are coming soon!" on your screen.
50
+
51
+ However a coming soon screen is only minimally useful, so you will soon create your +Book+ model and add the index action to +BooksController+:
52
+
53
+ <ruby>
54
+ class BooksController < ApplicationController
55
+ def index
56
+ @books = Book.all
57
+ end
58
+ end
59
+ </ruby>
60
+
61
+ Note that we don't have explicit render at the end of the index action in accordance with "convention over configuration" principle. The rule is that if you do not explicitly render something at the end of a controller action, Rails will automatically look for the +action_name.html.erb+ template in the controller's view path and render it. So in this case, Rails will render the +app/views/books/index.html.erb+ file.
62
+
63
+ If we want to display the properties of all the books in our view, we can do so with an ERB template like this:
64
+
65
+ <ruby>
66
+ <h1>Listing Books</h1>
67
+
68
+ <table>
69
+ <tr>
70
+ <th>Title</th>
71
+ <th>Summary</th>
72
+ <th></th>
73
+ <th></th>
74
+ <th></th>
75
+ </tr>
76
+
77
+ <% @books.each do |book| %>
78
+ <tr>
79
+ <td><%= book.title %></td>
80
+ <td><%= book.content %></td>
81
+ <td><%= link_to 'Show', book %></td>
82
+ <td><%= link_to 'Edit', edit_book_path(book) %></td>
83
+ <td><%= link_to 'Remove', book, :confirm => 'Are you sure?', :method => :delete %></td>
84
+ </tr>
85
+ <% end %>
86
+ </table>
87
+
88
+ <br />
89
+
90
+ <%= link_to 'New book', new_book_path %>
91
+ </ruby>
92
+
93
+ NOTE: The actual rendering is done by subclasses of +ActionView::TemplateHandlers+. This guide does not dig into that process, but it's important to know that the file extension on your view controls the choice of template handler. Beginning with Rails 2, the standard extensions are +.erb+ for ERB (HTML with embedded Ruby), and +.builder+ for Builder (XML generator).
94
+
95
+ h4. Using +render+
96
+
97
+ In most cases, the +ActionController::Base#render+ method does the heavy lifting of rendering your application's content for use by a browser. There are a variety of ways to customize the behaviour of +render+. You can render the default view for a Rails template, or a specific template, or a file, or inline code, or nothing at all. You can render text, JSON, or XML. You can specify the content type or HTTP status of the rendered response as well.
98
+
99
+ TIP: If you want to see the exact results of a call to +render+ without needing to inspect it in a browser, you can call +render_to_string+. This method takes exactly the same options as +render+, but it returns a string instead of sending a response back to the browser.
100
+
101
+ h5. Rendering Nothing
102
+
103
+ Perhaps the simplest thing you can do with +render+ is to render nothing at all:
104
+
105
+ <ruby>
106
+ render :nothing => true
107
+ </ruby>
108
+
109
+ If you look at the response for this using cURL, you will see the following:
110
+
111
+ <shell>
112
+ $ curl -i 127.0.0.1:3000/books
113
+ HTTP/1.1 200 OK
114
+ Connection: close
115
+ Date: Sun, 24 Jan 2010 09:25:18 GMT
116
+ Transfer-Encoding: chunked
117
+ Content-Type: */*; charset=utf-8
118
+ X-Runtime: 0.014297
119
+ Set-Cookie: _blog_session=...snip...; path=/; HttpOnly
120
+ Cache-Control: no-cache
121
+
122
+
123
+ $
124
+ </shell>
125
+
126
+ We see there is an empty response (no data after the +Cache-Control+ line), but the request was successful because Rails has set the response to 200 OK. You can set the +:status+ option on render to change this response. Rendering nothing can be useful for AJAX requests where all you want to send back to the browser is an acknowledgment that the request was completed.
127
+
128
+ TIP: You should probably be using the +head+ method, discussed later in this guide, instead of +render :nothing+. This provides additional flexibility and makes it explicit that you're only generating HTTP headers.
129
+
130
+ h5. Rendering an Action's View
131
+
132
+ If you want to render the view that corresponds to a different action within the same template, you can use +render+ with the name of the view:
133
+
134
+ <ruby>
135
+ def update
136
+ @book = Book.find(params[:id])
137
+ if @book.update_attributes(params[:book])
138
+ redirect_to(@book)
139
+ else
140
+ render "edit"
141
+ end
142
+ end
143
+ </ruby>
144
+
145
+ If the call to +update_attributes+ fails, calling the +update+ action in this controller will render the +edit.html.erb+ template belonging to the same controller.
146
+
147
+ If you prefer, you can use a symbol instead of a string to specify the action to render:
148
+
149
+ <ruby>
150
+ def update
151
+ @book = Book.find(params[:id])
152
+ if @book.update_attributes(params[:book])
153
+ redirect_to(@book)
154
+ else
155
+ render :edit
156
+ end
157
+ end
158
+ </ruby>
159
+
160
+ To be explicit, you can use +render+ with the +:action+ option (though this is no longer necessary in Rails 3.0):
161
+
162
+ <ruby>
163
+ def update
164
+ @book = Book.find(params[:id])
165
+ if @book.update_attributes(params[:book])
166
+ redirect_to(@book)
167
+ else
168
+ render :action => "edit"
169
+ end
170
+ end
171
+ </ruby>
172
+
173
+ WARNING: Using +render+ with +:action+ is a frequent source of confusion for Rails newcomers. The specified action is used to determine which view to render, but Rails does _not_ run any of the code for that action in the controller. Any instance variables that you require in the view must be set up in the current action before calling +render+.
174
+
175
+ h5. Rendering an Action's Template from Another Controller
176
+
177
+ What if you want to render a template from an entirely different controller from the one that contains the action code? You can also do that with +render+, which accepts the full path (relative to +app/views+) of the template to render. For example, if you're running code in an +AdminProductsController+ that lives in +app/controllers/admin+, you can render the results of an action to a template in +app/views/products+ this way:
178
+
179
+ <ruby>
180
+ render 'products/show'
181
+ </ruby>
182
+
183
+ Rails knows that this view belongs to a different controller because of the embedded slash character in the string. If you want to be explicit, you can use the +:template+ option (which was required on Rails 2.2 and earlier):
184
+
185
+ <ruby>
186
+ render :template => 'products/show'
187
+ </ruby>
188
+
189
+ h5. Rendering an Arbitrary File
190
+
191
+ The +render+ method can also use a view that's entirely outside of your application (perhaps you're sharing views between two Rails applications):
192
+
193
+ <ruby>
194
+ render "/u/apps/warehouse_app/current/app/views/products/show"
195
+ </ruby>
196
+
197
+ Rails determines that this is a file render because of the leading slash character. To be explicit, you can use the +:file+ option (which was required on Rails 2.2 and earlier):
198
+
199
+ <ruby>
200
+ render :file =>
201
+ "/u/apps/warehouse_app/current/app/views/products/show"
202
+ </ruby>
203
+
204
+ The +:file+ option takes an absolute file-system path. Of course, you need to have rights to the view that you're using to render the content.
205
+
206
+ NOTE: By default, the file is rendered without using the current layout. If you want Rails to put the file into the current layout, you need to add the +:layout => true+ option.
207
+
208
+ TIP: If you're running Rails on Microsoft Windows, you should use the +:file+ option to render a file, because Windows filenames do not have the same format as Unix filenames.
209
+
210
+ h5. Wrapping it up
211
+
212
+ The above three ways of rendering (rendering another template within the controller, rendering a template within another controller and rendering an arbitrary file on the file system) are actually variants of the same action.
213
+
214
+ In fact, in the BooksController class, inside of the update action where we want to render the edit template if the book does not update successfully, all of the following render calls would all render the +edit.html.erb+ template in the +views/books+ directory:
215
+
216
+ <ruby>
217
+ render :edit
218
+ render :action => :edit
219
+ render 'edit'
220
+ render 'edit.html.erb'
221
+ render :action => 'edit'
222
+ render :action => 'edit.html.erb'
223
+ render 'books/edit'
224
+ render 'books/edit.html.erb'
225
+ render :template => 'books/edit'
226
+ render :template => 'books/edit.html.erb'
227
+ render '/path/to/rails/app/views/books/edit'
228
+ render '/path/to/rails/app/views/books/edit.html.erb'
229
+ render :file => '/path/to/rails/app/views/books/edit'
230
+ render :file => '/path/to/rails/app/views/books/edit.html.erb'
231
+ </ruby>
232
+
233
+ Which one you use is really a matter of style and convention, but the rule of thumb is to use the simplest one that makes sense for the code you are writing.
234
+
235
+ h5. Using +render+ with +:inline+
236
+
237
+ The +render+ method can do without a view completely, if you're willing to use the +:inline+ option to supply ERB as part of the method call. This is perfectly valid:
238
+
239
+ <ruby>
240
+ render :inline =>
241
+ "<% products.each do |p| %><p><%= p.name %></p><% end %>"
242
+ </ruby>
243
+
244
+ WARNING: There is seldom any good reason to use this option. Mixing ERB into your controllers defeats the MVC orientation of Rails and will make it harder for other developers to follow the logic of your project. Use a separate erb view instead.
245
+
246
+ By default, inline rendering uses ERB. You can force it to use Builder instead with the +:type+ option:
247
+
248
+ <ruby>
249
+ render :inline =>
250
+ "xml.p {'Horrid coding practice!'}", :type => :builder
251
+ </ruby>
252
+
253
+ h5. Rendering Text
254
+
255
+ You can send plain text - with no markup at all - back to the browser by using the +:text+ option to +render+:
256
+
257
+ <ruby>
258
+ render :text => "OK"
259
+ </ruby>
260
+
261
+ TIP: Rendering pure text is most useful when you're responding to AJAX or web service requests that are expecting something other than proper HTML.
262
+
263
+ NOTE: By default, if you use the +:text+ option, the text is rendered without using the current layout. If you want Rails to put the text into the current layout, you need to add the +:layout => true+ option.
264
+
265
+ h5. Rendering JSON
266
+
267
+ JSON is a JavaScript data format used by many AJAX libraries. Rails has built-in support for converting objects to JSON and rendering that JSON back to the browser:
268
+
269
+ <ruby>
270
+ render :json => @product
271
+ </ruby>
272
+
273
+ TIP: You don't need to call +to_json+ on the object that you want to render. If you use the +:json+ option, +render+ will automatically call +to_json+ for you.
274
+
275
+ h5. Rendering XML
276
+
277
+ Rails also has built-in support for converting objects to XML and rendering that XML back to the caller:
278
+
279
+ <ruby>
280
+ render :xml => @product
281
+ </ruby>
282
+
283
+ TIP: You don't need to call +to_xml+ on the object that you want to render. If you use the +:xml+ option, +render+ will automatically call +to_xml+ for you.
284
+
285
+ h5. Rendering Vanilla JavaScript
286
+
287
+ Rails can render vanilla JavaScript:
288
+
289
+ <ruby>
290
+ render :js => "alert('Hello Rails');"
291
+ </ruby>
292
+
293
+ This will send the supplied string to the browser with a MIME type of +text/javascript+.
294
+
295
+ h5. Options for +render+
296
+
297
+ Calls to the +render+ method generally accept four options:
298
+
299
+ * +:content_type+
300
+ * +:layout+
301
+ * +:status+
302
+ * +:location+
303
+
304
+ h6. The +:content_type+ Option
305
+
306
+ By default, Rails will serve the results of a rendering operation with the MIME content-type of +text/html+ (or +application/json+ if you use the +:json+ option, or +application/xml+ for the +:xml+ option.). There are times when you might like to change this, and you can do so by setting the +:content_type+ option:
307
+
308
+ <ruby>
309
+ render :file => filename, :content_type => 'application/rss'
310
+ </ruby>
311
+
312
+ h6. The +:layout+ Option
313
+
314
+ With most of the options to +render+, the rendered content is displayed as part of the current layout. You'll learn more about layouts and how to use them later in this guide.
315
+
316
+ You can use the +:layout+ option to tell Rails to use a specific file as the layout for the current action:
317
+
318
+ <ruby>
319
+ render :layout => 'special_layout'
320
+ </ruby>
321
+
322
+ You can also tell Rails to render with no layout at all:
323
+
324
+ <ruby>
325
+ render :layout => false
326
+ </ruby>
327
+
328
+ h6. The +:status+ Option
329
+
330
+ Rails will automatically generate a response with the correct HTTP status code (in most cases, this is +200 OK+). You can use the +:status+ option to change this:
331
+
332
+ <ruby>
333
+ render :status => 500
334
+ render :status => :forbidden
335
+ </ruby>
336
+
337
+ Rails understands both numeric and symbolic status codes.
338
+
339
+ h6. The +:location+ Option
340
+
341
+ You can use the +:location+ option to set the HTTP +Location+ header:
342
+
343
+ <ruby>
344
+ render :xml => photo, :location => photo_url(photo)
345
+ </ruby>
346
+
347
+ h5. Finding Layouts
348
+
349
+ To find the current layout, Rails first looks for a file in +app/views/layouts+ with the same base name as the controller. For example, rendering actions from the +PhotosController+ class will use +app/views/layouts/photos.html.erb+ (or +app/views/layouts/photos.builder+). If there is no such controller-specific layout, Rails will use +app/views/layouts/application.html.erb+ or +app/views/layouts/application.builder+. If there is no +.erb+ layout, Rails will use a +.builder+ layout if one exists. Rails also provides several ways to more precisely assign specific layouts to individual controllers and actions.
350
+
351
+ h6. Specifying Layouts for Controllers
352
+
353
+ You can override the default layout conventions in your controllers by using the +layout+ declaration. For example:
354
+
355
+ <ruby>
356
+ class ProductsController < ApplicationController
357
+ layout "inventory"
358
+ #...
359
+ end
360
+ </ruby>
361
+
362
+ With this declaration, all of the methods within +ProductsController+ will use +app/views/layouts/inventory.html.erb+ for their layout.
363
+
364
+ To assign a specific layout for the entire application, use a +layout+ declaration in your +ApplicationController+ class:
365
+
366
+ <ruby>
367
+ class ApplicationController < ActionController::Base
368
+ layout "main"
369
+ #...
370
+ end
371
+ </ruby>
372
+
373
+ With this declaration, all of the views in the entire application will use +app/views/layouts/main.html.erb+ for their layout.
374
+
375
+ h6. Choosing Layouts at Runtime
376
+
377
+ You can use a symbol to defer the choice of layout until a request is processed:
378
+
379
+ <ruby>
380
+ class ProductsController < ApplicationController
381
+ layout :products_layout
382
+
383
+ def show
384
+ @product = Product.find(params[:id])
385
+ end
386
+
387
+ private
388
+ def products_layout
389
+ @current_user.special? ? "special" : "products"
390
+ end
391
+
392
+ end
393
+ </ruby>
394
+
395
+ Now, if the current user is a special user, they'll get a special layout when viewing a product.
396
+
397
+ You can even use an inline method, such as a Proc, to determine the layout. For example, if you pass a Proc object, the block you give the Proc will be given the +controller+ instance, so the layout can be determined based on the current request:
398
+
399
+ <ruby>
400
+ class ProductsController < ApplicationController
401
+ layout Proc.new { |controller| controller.request.xhr? ? 'popup' : 'application' }
402
+ end
403
+ </ruby>
404
+
405
+ h6. Conditional Layouts
406
+
407
+ Layouts specified at the controller level support the +:only+ and +:except+ options. These options take either a method name, or an array of method names, corresponding to method names within the controller:
408
+
409
+ <ruby>
410
+ class ProductsController < ApplicationController
411
+ layout "product", :except => [:index, :rss]
412
+ end
413
+ </ruby>
414
+
415
+ With this declaration, the +product+ layout would be used for everything but the +rss+ and +index+ methods.
416
+
417
+ h6. Layout Inheritance
418
+
419
+ Layout declarations cascade downward in the hierarchy, and more specific layout declarations always override more general ones. For example:
420
+
421
+ * +application_controller.rb+
422
+
423
+ <ruby>
424
+ class ApplicationController < ActionController::Base
425
+ layout "main"
426
+ end
427
+ </ruby>
428
+
429
+ * +posts_controller.rb+
430
+
431
+ <ruby>
432
+ class PostsController < ApplicationController
433
+ end
434
+ </ruby>
435
+
436
+ * +special_posts_controller.rb+
437
+
438
+ <ruby>
439
+ class SpecialPostsController < PostsController
440
+ layout "special"
441
+ end
442
+ </ruby>
443
+
444
+ * +old_posts_controller.rb+
445
+
446
+ <ruby>
447
+ class OldPostsController < SpecialPostsController
448
+ layout false
449
+
450
+ def show
451
+ @post = Post.find(params[:id])
452
+ end
453
+
454
+ def index
455
+ @old_posts = Post.older
456
+ render :layout => "old"
457
+ end
458
+ # ...
459
+ end
460
+ </ruby>
461
+
462
+ In this application:
463
+
464
+ * In general, views will be rendered in the +main+ layout
465
+ * +PostsController#index+ will use the +main+ layout
466
+ * +SpecialPostsController#index+ will use the +special+ layout
467
+ * +OldPostsController#show+ will use no layout at all
468
+ * +OldPostsController#index+ will use the +old+ layout
469
+
470
+ h5. Avoiding Double Render Errors
471
+
472
+ Sooner or later, most Rails developers will see the error message "Can only render or redirect once per action". While this is annoying, it's relatively easy to fix. Usually it happens because of a fundamental misunderstanding of the way that +render+ works.
473
+
474
+ For example, here's some code that will trigger this error:
475
+
476
+ <ruby>
477
+ def show
478
+ @book = Book.find(params[:id])
479
+ if @book.special?
480
+ render :action => "special_show"
481
+ end
482
+ render :action => "regular_show"
483
+ end
484
+ </ruby>
485
+
486
+ If +@book.special?+ evaluates to +true+, Rails will start the rendering process to dump the +@book+ variable into the +special_show+ view. But this will _not_ stop the rest of the code in the +show+ action from running, and when Rails hits the end of the action, it will start to render the +regular_show+ view - and throw an error. The solution is simple: make sure that you have only one call to +render+ or +redirect+ in a single code path. One thing that can help is +and return+. Here's a patched version of the method:
487
+
488
+ <ruby>
489
+ def show
490
+ @book = Book.find(params[:id])
491
+ if @book.special?
492
+ render :action => "special_show" and return
493
+ end
494
+ render :action => "regular_show"
495
+ end
496
+ </ruby>
497
+
498
+ Make sure to use +and return+ instead of +&amp;&amp; return+ because +&amp;&amp; return+ will not work due to the operator precedence in the Ruby Language.
499
+
500
+ Note that the implicit render done by ActionController detects if +render+ has been called, so the following will work without errors:
501
+
502
+ <ruby>
503
+ def show
504
+ @book = Book.find(params[:id])
505
+ if @book.special?
506
+ render :action => "special_show"
507
+ end
508
+ end
509
+ </ruby>
510
+
511
+ This will render a book with +special?+ set with the +special_show+ template, while other books will render with the default +show+ template.
512
+
513
+ h4. Using +redirect_to+
514
+
515
+ Another way to handle returning responses to an HTTP request is with +redirect_to+. As you've seen, +render+ tells Rails which view (or other asset) to use in constructing a response. The +redirect_to+ method does something completely different: it tells the browser to send a new request for a different URL. For example, you could redirect from wherever you are in your code to the index of photos in your application with this call:
516
+
517
+ <ruby>
518
+ redirect_to photos_url
519
+ </ruby>
520
+
521
+ You can use +redirect_to+ with any arguments that you could use with +link_to+ or +url_for+. There's also a special redirect that sends the user back to the page they just came from:
522
+
523
+ <ruby>
524
+ redirect_to :back
525
+ </ruby>
526
+
527
+ h5. Getting a Different Redirect Status Code
528
+
529
+ Rails uses HTTP status code 302, a temporary redirect, when you call +redirect_to+. If you'd like to use a different status code, perhaps 301, a permanent redirect, you can use the +:status+ option:
530
+
531
+ <ruby>
532
+ redirect_to photos_path, :status => 301
533
+ </ruby>
534
+
535
+ Just like the +:status+ option for +render+, +:status+ for +redirect_to+ accepts both numeric and symbolic header designations.
536
+
537
+ h5. The Difference Between +render+ and +redirect_to+
538
+
539
+ Sometimes inexperienced developers think of +redirect_to+ as a sort of +goto+ command, moving execution from one place to another in your Rails code. This is _not_ correct. Your code stops running and waits for a new request for the browser. It just happens that you've told the browser what request it should make next, by sending back an HTTP 302 status code.
540
+
541
+ Consider these actions to see the difference:
542
+
543
+ <ruby>
544
+ def index
545
+ @books = Book.all
546
+ end
547
+
548
+ def show
549
+ @book = Book.find_by_id(params[:id])
550
+ if @book.nil?
551
+ render :action => "index"
552
+ end
553
+ end
554
+ </ruby>
555
+
556
+ With the code in this form, there will likely be a problem if the +@book+ variable is +nil+. Remember, a +render :action+ doesn't run any code in the target action, so nothing will set up the +@books+ variable that the +index+ view will probably require. One way to fix this is to redirect instead of rendering:
557
+
558
+ <ruby>
559
+ def index
560
+ @books = Book.all
561
+ end
562
+
563
+ def show
564
+ @book = Book.find_by_id(params[:id])
565
+ if @book.nil?
566
+ redirect_to :action => :index
567
+ end
568
+ end
569
+ </ruby>
570
+
571
+ With this code, the browser will make a fresh request for the index page, the code in the +index+ method will run, and all will be well.
572
+
573
+ The only downside to this code is that it requires a round trip to the browser: the browser requested the show action with +/books/1+ and the controller finds that there are no books, so the controller sends out a 302 redirect response to the browser telling it to go to +/books/+, the browser complies and sends a new request back to the controller asking now for the +index+ action, the controller then gets all the books in the database and renders the index template, sending it back down to the browser which then shows it on your screen.
574
+
575
+ While in a small application, this added latency might not be a problem, it is something to think about if response time is a concern. We can demonstrate one way to handle this with a contrived example:
576
+
577
+ <ruby>
578
+ def index
579
+ @books = Book.all
580
+ end
581
+
582
+ def show
583
+ @book = Book.find_by_id(params[:id])
584
+ if @book.nil?
585
+ @books = Book.all
586
+ render "index", :alert => 'Your book was not found!'
587
+ end
588
+ end
589
+ </ruby>
590
+
591
+ This would detect that there are no books with the specified ID, populate the +@books+ instance variable with all the books in the model, and then directly render the +index.html.erb+ template, returning it to the browser with a flash alert message to tell the user what happened.
592
+
593
+ h4. Using +head+ To Build Header-Only Responses
594
+
595
+ The +head+ method can be used to send responses with only headers to the browser. It provides a more obvious alternative to calling +render :nothing+. The +head+ method takes one parameter, which is interpreted as a hash of header names and values. For example, you can return only an error header:
596
+
597
+ <ruby>
598
+ head :bad_request
599
+ </ruby>
600
+
601
+ This would produce the following header:
602
+
603
+ <shell>
604
+ HTTP/1.1 400 Bad Request
605
+ Connection: close
606
+ Date: Sun, 24 Jan 2010 12:15:53 GMT
607
+ Transfer-Encoding: chunked
608
+ Content-Type: text/html; charset=utf-8
609
+ X-Runtime: 0.013483
610
+ Set-Cookie: _blog_session=...snip...; path=/; HttpOnly
611
+ Cache-Control: no-cache
612
+ </shell>
613
+
614
+ Or you can use other HTTP headers to convey other information:
615
+
616
+ <ruby>
617
+ head :created, :location => photo_path(@photo)
618
+ </ruby>
619
+
620
+ Which would produce:
621
+
622
+ <shell>
623
+ HTTP/1.1 201 Created
624
+ Connection: close
625
+ Date: Sun, 24 Jan 2010 12:16:44 GMT
626
+ Transfer-Encoding: chunked
627
+ Location: /photos/1
628
+ Content-Type: text/html; charset=utf-8
629
+ X-Runtime: 0.083496
630
+ Set-Cookie: _blog_session=...snip...; path=/; HttpOnly
631
+ Cache-Control: no-cache
632
+ </shell>
633
+
634
+ h3. Structuring Layouts
635
+
636
+ When Rails renders a view as a response, it does so by combining the view with the current layout, using the rules for finding the current layout that were covered earlier in this guide. Within a layout, you have access to three tools for combining different bits of output to form the overall response:
637
+
638
+ * Asset tags
639
+ * +yield+ and +content_for+
640
+ * Partials
641
+
642
+ h4. Asset Tag Helpers
643
+
644
+ Asset tag helpers provide methods for generating HTML that link views to feeds, JavaScript, stylesheets, images, videos and audios. There are six asset tag helpers available in Rails:
645
+
646
+ * +auto_discovery_link_tag+
647
+ * +javascript_include_tag+
648
+ * +stylesheet_link_tag+
649
+ * +image_tag+
650
+ * +video_tag+
651
+ * +audio_tag+
652
+
653
+ You can use these tags in layouts or other views, although the +auto_discovery_link_tag+, +javascript_include_tag+, and +stylesheet_link_tag+, are most commonly used in the +&lt;head&gt;+ section of a layout.
654
+
655
+ WARNING: The asset tag helpers do _not_ verify the existence of the assets at the specified locations; they simply assume that you know what you're doing and generate the link.
656
+
657
+ h5. Linking to Feeds with the +auto_discovery_link_tag+
658
+
659
+ The +auto_discovery_link_tag+ helper builds HTML that most browsers and newsreaders can use to detect the presences of RSS or ATOM feeds. It takes the type of the link (+:rss+ or +:atom+), a hash of options that are passed through to url_for, and a hash of options for the tag:
660
+
661
+ <erb>
662
+ <%= auto_discovery_link_tag(:rss, {:action => "feed"},
663
+ {:title => "RSS Feed"}) %>
664
+ </erb>
665
+
666
+ There are three tag options available for the +auto_discovery_link_tag+:
667
+
668
+ * +:rel+ specifies the +rel+ value in the link. The default value is "alternate".
669
+ * +:type+ specifies an explicit MIME type. Rails will generate an appropriate MIME type automatically.
670
+ * +:title+ specifies the title of the link. The default value is the upshifted +:type+ value, for example, "ATOM" or "RSS".
671
+
672
+ h5. Linking to JavaScript Files with the +javascript_include_tag+
673
+
674
+ The +javascript_include_tag+ helper returns an HTML +script+ tag for each source provided.
675
+
676
+ If you are using Rails with the "Asset Pipeline":asset_pipeline.html enabled, this helper will generate a link to +/assets/javascripts/+ rather than +public/javascripts+ which was used in earlier versions of Rails. This link is then served by the Sprockets gem, which was introduced in Rails 3.1.
677
+
678
+ A JavaScript file within a Rails application or Rails engine goes in one of three locations: +app/assets+, +lib/assets+ or +vendor/assets+. These locations are explained in detail in the "Asset Organization section in the Asset Pipeline Guide":asset_pipeline.html#asset-organization
679
+
680
+ You can specify a full path relative to the document root, or a URL, if you prefer. For example, to link to a JavaScript file that is inside a directory called +javascripts+ inside of one of +app/assets+, +lib/assets+ or +vendor/assets+, you would do this:
681
+
682
+ <erb>
683
+ <%= javascript_include_tag "main" %>
684
+ </erb>
685
+
686
+ Rails will then output a +script+ tag such as this:
687
+
688
+ <html>
689
+ <script src='/assets/main.js' type="text/javascript"></script>
690
+ </html>
691
+
692
+ The request to this asset is then served by the Sprockets gem.
693
+
694
+ To include multiple files such as +app/assets/javascripts/main.js+ and +app/assets/javascripts/columns.js+ at the same time:
695
+
696
+ <erb>
697
+ <%= javascript_include_tag "main", "columns" %>
698
+ </erb>
699
+
700
+ To include +app/assets/javascripts/main.js+ and +app/assets/javascripts/photos/columns.js+:
701
+
702
+ <erb>
703
+ <%= javascript_include_tag "main", "/photos/columns" %>
704
+ </erb>
705
+
706
+ To include +http://example.com/main.js+:
707
+
708
+ <erb>
709
+ <%= javascript_include_tag "http://example.com/main.js" %>
710
+ </erb>
711
+
712
+ If the application does not use the asset pipeline, the +:defaults+ option loads jQuery by default:
713
+
714
+ <erb>
715
+ <%= javascript_include_tag :defaults %>
716
+ </erb>
717
+
718
+ Outputting +script+ tags such as this:
719
+
720
+ <html>
721
+ <script src="/javascripts/jquery.js" type="text/javascript"></script>
722
+ <script src="/javascripts/jquery_ujs.js" type="text/javascript"></script>
723
+ </html>
724
+
725
+ These two files for jQuery, +jquery.js+ and +jquery_ujs.js+ must be placed inside +public/javascripts+ if the application doesn't use the asset pipeline. These files can be downloaded from the "jquery-rails repository on GitHub":https://github.com/indirect/jquery-rails/tree/master/vendor/assets/javascripts
726
+
727
+ WARNING: If you are using the asset pipeline, this tag will render a +script+ tag for an asset called +defaults.js+, which would not exist in your application unless you've explicitly defined it to be.
728
+
729
+ And you can in any case override the +:defaults+ expansion in <tt>config/application.rb</tt>:
730
+
731
+ <ruby>
732
+ config.action_view.javascript_expansions[:defaults] = %w(foo.js bar.js)
733
+ </ruby>
734
+
735
+ You can also define new defaults:
736
+
737
+ <ruby>
738
+ config.action_view.javascript_expansions[:projects] = %w(projects.js tickets.js)
739
+ </ruby>
740
+
741
+ And use them by referencing them exactly like +:defaults+:
742
+
743
+ <erb>
744
+ <%= javascript_include_tag :projects %>
745
+ </erb>
746
+
747
+ When using <tt>:defaults</tt>, if an <tt>application.js</tt> file exists in <tt>public/javascripts</tt> it will be included as well at the end.
748
+
749
+ Also, if the asset pipeline is disabled, the +:all+ expansion loads every JavaScript file in +public/javascripts+:
750
+
751
+ <erb>
752
+ <%= javascript_include_tag :all %>
753
+ </erb>
754
+
755
+ Note that your defaults of choice will be included first, so they will be available to all subsequently included files.
756
+
757
+ You can supply the +:recursive+ option to load files in subfolders of +public/javascripts+ as well:
758
+
759
+ <erb>
760
+ <%= javascript_include_tag :all, :recursive => true %>
761
+ </erb>
762
+
763
+ If you're loading multiple JavaScript files, you can create a better user experience by combining multiple files into a single download. To make this happen in production, specify +:cache => true+ in your +javascript_include_tag+:
764
+
765
+ <erb>
766
+ <%= javascript_include_tag "main", "columns", :cache => true %>
767
+ </erb>
768
+
769
+ By default, the combined file will be delivered as +javascripts/all.js+. You can specify a location for the cached asset file instead:
770
+
771
+ <erb>
772
+ <%= javascript_include_tag "main", "columns",
773
+ :cache => 'cache/main/display' %>
774
+ </erb>
775
+
776
+ You can even use dynamic paths such as +cache/#{current_site}/main/display+.
777
+
778
+ h5. Linking to CSS Files with the +stylesheet_link_tag+
779
+
780
+ The +stylesheet_link_tag+ helper returns an HTML +&lt;link&gt;+ tag for each source provided.
781
+
782
+ If you are using Rails with the "Asset Pipeline" enabled, this helper will generate a link to +/assets/stylesheets/+. This link is then processed by the Sprockets gem. A stylesheet file can be stored in one of three locations: +app/assets+, +lib/assets+ or +vendor/assets+.
783
+
784
+ You can specify a full path relative to the document root, or a URL. For example, to link to a stylesheet file that is inside a directory called +stylesheets+ inside of one of +app/assets+, +lib/assets+ or +vendor/assets+, you would do this:
785
+
786
+ <erb>
787
+ <%= stylesheet_link_tag "main" %>
788
+ </erb>
789
+
790
+ To include +app/assets/stylesheets/main.css+ and +app/assets/stylesheets/columns.css+:
791
+
792
+ <erb>
793
+ <%= stylesheet_link_tag "main", "columns" %>
794
+ </erb>
795
+
796
+ To include +app/assets/stylesheets/main.css+ and +app/assets/stylesheets/photos/columns.css+:
797
+
798
+ <erb>
799
+ <%= stylesheet_link_tag "main", "/photos/columns" %>
800
+ </erb>
801
+
802
+ To include +http://example.com/main.css+:
803
+
804
+ <erb>
805
+ <%= stylesheet_link_tag "http://example.com/main.css" %>
806
+ </erb>
807
+
808
+ By default, the +stylesheet_link_tag+ creates links with +media="screen" rel="stylesheet" type="text/css"+. You can override any of these defaults by specifying an appropriate option (+:media+, +:rel+, or +:type+):
809
+
810
+ <erb>
811
+ <%= stylesheet_link_tag "main_print", :media => "print" %>
812
+ </erb>
813
+
814
+ If the asset pipeline is disabled, the +all+ option links every CSS file in +public/stylesheets+:
815
+
816
+ <erb>
817
+ <%= stylesheet_link_tag :all %>
818
+ </erb>
819
+
820
+ You can supply the +:recursive+ option to link files in subfolders of +public/stylesheets+ as well:
821
+
822
+ <erb>
823
+ <%= stylesheet_link_tag :all, :recursive => true %>
824
+ </erb>
825
+
826
+ If you're loading multiple CSS files, you can create a better user experience by combining multiple files into a single download. To make this happen in production, specify +:cache => true+ in your +stylesheet_link_tag+:
827
+
828
+ <erb>
829
+ <%= stylesheet_link_tag "main", "columns", :cache => true %>
830
+ </erb>
831
+
832
+ By default, the combined file will be delivered as +stylesheets/all.css+. You can specify a location for the cached asset file instead:
833
+
834
+ <erb>
835
+ <%= stylesheet_link_tag "main", "columns",
836
+ :cache => 'cache/main/display' %>
837
+ </erb>
838
+
839
+ You can even use dynamic paths such as +cache/#{current_site}/main/display+.
840
+
841
+ h5. Linking to Images with the +image_tag+
842
+
843
+ The +image_tag+ helper builds an HTML +&lt;img /&gt;+ tag to the specified file. By default, files are loaded from +public/images+.
844
+
845
+ WARNING: Note that you must specify the extension of the image. Previous versions of Rails would allow you to just use the image name and would append +.png+ if no extension was given but Rails 3.0 does not.
846
+
847
+ <erb>
848
+ <%= image_tag "header.png" %>
849
+ </erb>
850
+
851
+ You can supply a path to the image if you like:
852
+
853
+ <erb>
854
+ <%= image_tag "icons/delete.gif" %>
855
+ </erb>
856
+
857
+ You can supply a hash of additional HTML options:
858
+
859
+ <erb>
860
+ <%= image_tag "icons/delete.gif", {:height => 45} %>
861
+ </erb>
862
+
863
+ You can also supply an alternate image to show on mouseover:
864
+
865
+ <erb>
866
+ <%= image_tag "home.gif", :onmouseover => "menu/home_highlight.gif" %>
867
+ </erb>
868
+
869
+ You can supply alternate text for the image which will be used if the user has images turned off in their browser. If you do not specify an alt text explicitly, it defaults to the file name of the file, capitalized and with no extension. For example, these two image tags would return the same code:
870
+
871
+ <erb>
872
+ <%= image_tag "home.gif" %>
873
+ <%= image_tag "home.gif", :alt => "Home" %>
874
+ </erb>
875
+
876
+ You can also specify a special size tag, in the format "{width}x{height}":
877
+
878
+ <erb>
879
+ <%= image_tag "home.gif", :size => "50x20" %>
880
+ </erb>
881
+
882
+ In addition to the above special tags, you can supply a final hash of standard HTML options, such as +:class+, +:id+ or +:name+:
883
+
884
+ <erb>
885
+ <%= image_tag "home.gif", :alt => "Go Home",
886
+ :id => "HomeImage",
887
+ :class => 'nav_bar' %>
888
+ </erb>
889
+
890
+ h5. Linking to Videos with the +video_tag+
891
+
892
+ The +video_tag+ helper builds an HTML 5 +&lt;video&gt;+ tag to the specified file. By default, files are loaded from +public/videos+.
893
+
894
+ <erb>
895
+ <%= video_tag "movie.ogg" %>
896
+ </erb>
897
+
898
+ Produces
899
+
900
+ <erb>
901
+ <video src="/videos/movie.ogg" />
902
+ </erb>
903
+
904
+ Like an +image_tag+ you can supply a path, either absolute, or relative to the +public/videos+ directory. Additionally you can specify the +:size => "#{width}x#{height}"+ option just like an +image_tag+. Video tags can also have any of the HTML options specified at the end (+id+, +class+ et al).
905
+
906
+ The video tag also supports all of the +&lt;video&gt;+ HTML options through the HTML options hash, including:
907
+
908
+ * +:poster => 'image_name.png'+, provides an image to put in place of the video before it starts playing.
909
+ * +:autoplay => true+, starts playing the video on page load.
910
+ * +:loop => true+, loops the video once it gets to the end.
911
+ * +:controls => true+, provides browser supplied controls for the user to interact with the video.
912
+ * +:autobuffer => true+, the video will pre load the file for the user on page load.
913
+
914
+ You can also specify multiple videos to play by passing an array of videos to the +video_tag+:
915
+
916
+ <erb>
917
+ <%= video_tag ["trailer.ogg", "movie.ogg"] %>
918
+ </erb>
919
+
920
+ This will produce:
921
+
922
+ <erb>
923
+ <video><source src="trailer.ogg" /><source src="movie.ogg" /></video>
924
+ </erb>
925
+
926
+ h5. Linking to Audio Files with the +audio_tag+
927
+
928
+ The +audio_tag+ helper builds an HTML 5 +&lt;audio&gt;+ tag to the specified file. By default, files are loaded from +public/audios+.
929
+
930
+ <erb>
931
+ <%= audio_tag "music.mp3" %>
932
+ </erb>
933
+
934
+ You can supply a path to the audio file if you like:
935
+
936
+ <erb>
937
+ <%= audio_tag "music/first_song.mp3" %>
938
+ </erb>
939
+
940
+ You can also supply a hash of additional options, such as +:id+, +:class+ etc.
941
+
942
+ Like the +video_tag+, the +audio_tag+ has special options:
943
+
944
+ * +:autoplay => true+, starts playing the audio on page load
945
+ * +:controls => true+, provides browser supplied controls for the user to interact with the audio.
946
+ * +:autobuffer => true+, the audio will pre load the file for the user on page load.
947
+
948
+ h4. Understanding +yield+
949
+
950
+ Within the context of a layout, +yield+ identifies a section where content from the view should be inserted. The simplest way to use this is to have a single +yield+, into which the entire contents of the view currently being rendered is inserted:
951
+
952
+ <erb>
953
+ <html>
954
+ <head>
955
+ </head>
956
+ <body>
957
+ <%= yield %>
958
+ </body>
959
+ </html>
960
+ </erb>
961
+
962
+ You can also create a layout with multiple yielding regions:
963
+
964
+ <erb>
965
+ <html>
966
+ <head>
967
+ <%= yield :head %>
968
+ </head>
969
+ <body>
970
+ <%= yield %>
971
+ </body>
972
+ </html>
973
+ </erb>
974
+
975
+ The main body of the view will always render into the unnamed +yield+. To render content into a named +yield+, you use the +content_for+ method.
976
+
977
+ h4. Using the +content_for+ Method
978
+
979
+ The +content_for+ method allows you to insert content into a named +yield+ block in your layout. For example, this view would work with the layout that you just saw:
980
+
981
+ <erb>
982
+ <% content_for :head do %>
983
+ <title>A simple page</title>
984
+ <% end %>
985
+
986
+ <p>Hello, Rails!</p>
987
+ </erb>
988
+
989
+ The result of rendering this page into the supplied layout would be this HTML:
990
+
991
+ <erb>
992
+ <html>
993
+ <head>
994
+ <title>A simple page</title>
995
+ </head>
996
+ <body>
997
+ <p>Hello, Rails!</p>
998
+ </body>
999
+ </html>
1000
+ </erb>
1001
+
1002
+ The +content_for+ method is very helpful when your layout contains distinct regions such as sidebars and footers that should get their own blocks of content inserted. It's also useful for inserting tags that load page-specific JavaScript or css files into the header of an otherwise generic layout.
1003
+
1004
+ h4. Using Partials
1005
+
1006
+ Partial templates - usually just called "partials" - are another device for breaking the rendering process into more manageable chunks. With a partial, you can move the code for rendering a particular piece of a response to its own file.
1007
+
1008
+ h5. Naming Partials
1009
+
1010
+ To render a partial as part of a view, you use the +render+ method within the view:
1011
+
1012
+ <ruby>
1013
+ <%= render "menu" %>
1014
+ </ruby>
1015
+
1016
+ This will render a file named +_menu.html.erb+ at that point within the view being rendered. Note the leading underscore character: partials are named with a leading underscore to distinguish them from regular views, even though they are referred to without the underscore. This holds true even when you're pulling in a partial from another folder:
1017
+
1018
+ <ruby>
1019
+ <%= render "shared/menu" %>
1020
+ </ruby>
1021
+
1022
+ That code will pull in the partial from +app/views/shared/_menu.html.erb+.
1023
+
1024
+ h5. Using Partials to Simplify Views
1025
+
1026
+ One way to use partials is to treat them as the equivalent of subroutines: as a way to move details out of a view so that you can grasp what's going on more easily. For example, you might have a view that looked like this:
1027
+
1028
+ <erb>
1029
+ <%= render "shared/ad_banner" %>
1030
+
1031
+ <h1>Products</h1>
1032
+
1033
+ <p>Here are a few of our fine products:</p>
1034
+ ...
1035
+
1036
+ <%= render "shared/footer" %>
1037
+ </erb>
1038
+
1039
+ Here, the +_ad_banner.html.erb+ and +_footer.html.erb+ partials could contain content that is shared among many pages in your application. You don't need to see the details of these sections when you're concentrating on a particular page.
1040
+
1041
+ TIP: For content that is shared among all pages in your application, you can use partials directly from layouts.
1042
+
1043
+ h5. Partial Layouts
1044
+
1045
+ A partial can use its own layout file, just as a view can use a layout. For example, you might call a partial like this:
1046
+
1047
+ <erb>
1048
+ <%= render :partial => "link_area", :layout => "graybar" %>
1049
+ </erb>
1050
+
1051
+ This would look for a partial named +_link_area.html.erb+ and render it using the layout +_graybar.html.erb+. Note that layouts for partials follow the same leading-underscore naming as regular partials, and are placed in the same folder with the partial that they belong to (not in the master +layouts+ folder).
1052
+
1053
+ Also note that explicitly specifying +:partial+ is required when passing additional options such as +:layout+.
1054
+
1055
+ h5. Passing Local Variables
1056
+
1057
+ You can also pass local variables into partials, making them even more powerful and flexible. For example, you can use this technique to reduce duplication between new and edit pages, while still keeping a bit of distinct content:
1058
+
1059
+ * +new.html.erb+
1060
+
1061
+ <erb>
1062
+ <h1>New zone</h1>
1063
+ <%= error_messages_for :zone %>
1064
+ <%= render :partial => "form", :locals => { :zone => @zone } %>
1065
+ </erb>
1066
+
1067
+ * +edit.html.erb+
1068
+
1069
+ <erb>
1070
+ <h1>Editing zone</h1>
1071
+ <%= error_messages_for :zone %>
1072
+ <%= render :partial => "form", :locals => { :zone => @zone } %>
1073
+ </erb>
1074
+
1075
+ * +_form.html.erb+
1076
+
1077
+ <erb>
1078
+ <%= form_for(zone) do |f| %>
1079
+ <p>
1080
+ <b>Zone name</b><br />
1081
+ <%= f.text_field :name %>
1082
+ </p>
1083
+ <p>
1084
+ <%= f.submit %>
1085
+ </p>
1086
+ <% end %>
1087
+ </erb>
1088
+
1089
+ Although the same partial will be rendered into both views, Action View's submit helper will return "Create Zone" for the new action and "Update Zone" for the edit action.
1090
+
1091
+ Every partial also has a local variable with the same name as the partial (minus the underscore). You can pass an object in to this local variable via the +:object+ option:
1092
+
1093
+ <erb>
1094
+ <%= render :partial => "customer", :object => @new_customer %>
1095
+ </erb>
1096
+
1097
+ Within the +customer+ partial, the +customer+ variable will refer to +@new_customer+ from the parent view.
1098
+
1099
+ WARNING: In previous versions of Rails, the default local variable would look for an instance variable with the same name as the partial in the parent. This behavior was deprecated in 2.3 and has been removed in Rails 3.0.
1100
+
1101
+ If you have an instance of a model to render into a partial, you can use a shorthand syntax:
1102
+
1103
+ <erb>
1104
+ <%= render @customer %>
1105
+ </erb>
1106
+
1107
+ Assuming that the +@customer+ instance variable contains an instance of the +Customer+ model, this will use +_customer.html.erb+ to render it and will pass the local variable +customer+ into the partial which will refer to the +@customer+ instance variable in the parent view.
1108
+
1109
+ h5. Rendering Collections
1110
+
1111
+ Partials are very useful in rendering collections. When you pass a collection to a partial via the +:collection+ option, the partial will be inserted once for each member in the collection:
1112
+
1113
+ * +index.html.erb+
1114
+
1115
+ <erb>
1116
+ <h1>Products</h1>
1117
+ <%= render :partial => "product", :collection => @products %>
1118
+ </erb>
1119
+
1120
+ * +_product.html.erb+
1121
+
1122
+ <erb>
1123
+ <p>Product Name: <%= product.name %></p>
1124
+ </erb>
1125
+
1126
+ When a partial is called with a pluralized collection, then the individual instances of the partial have access to the member of the collection being rendered via a variable named after the partial. In this case, the partial is +_product+, and within the +_product+ partial, you can refer to +product+ to get the instance that is being rendered.
1127
+
1128
+ In Rails 3.0, there is also a shorthand for this. Assuming +@products+ is a collection of +product+ instances, you can simply write this in the +index.html.erb+ to produce the same result:
1129
+
1130
+ <erb>
1131
+ <h1>Products</h1>
1132
+ <%= render @products %>
1133
+ </erb>
1134
+
1135
+ Rails determines the name of the partial to use by looking at the model name in the collection. In fact, you can even create a heterogeneous collection and render it this way, and Rails will choose the proper partial for each member of the collection:
1136
+
1137
+ In the event that the collection is empty, +render+ will return nil, so it should be fairly simple to provide alternative content.
1138
+
1139
+ <erb>
1140
+ <h1>Products</h1>
1141
+ <%= render(@products) || 'There are no products available.' %>
1142
+ </erb>
1143
+
1144
+ * +index.html.erb+
1145
+
1146
+ <erb>
1147
+ <h1>Contacts</h1>
1148
+ <%= render [customer1, employee1, customer2, employee2] %>
1149
+ </erb>
1150
+
1151
+ * +customers/_customer.html.erb+
1152
+
1153
+ <erb>
1154
+ <p>Customer: <%= customer.name %></p>
1155
+ </erb>
1156
+
1157
+ * +employees/_employee.html.erb+
1158
+
1159
+ <erb>
1160
+ <p>Employee: <%= employee.name %></p>
1161
+ </erb>
1162
+
1163
+ In this case, Rails will use the customer or employee partials as appropriate for each member of the collection.
1164
+
1165
+ h5. Local Variables
1166
+
1167
+ To use a custom local variable name within the partial, specify the +:as+ option in the call to the partial:
1168
+
1169
+ <erb>
1170
+ <%= render :partial => "product", :collection => @products, :as => :item %>
1171
+ </erb>
1172
+
1173
+ With this change, you can access an instance of the +@products+ collection as the +item+ local variable within the partial.
1174
+
1175
+ You can also pass in arbitrary local variables to any partial you are rendering with the +:locals => {}+ option:
1176
+
1177
+ <erb>
1178
+ <%= render :partial => 'products', :collection => @products,
1179
+ :as => :item, :locals => {:title => "Products Page"} %>
1180
+ </erb>
1181
+
1182
+ Would render a partial +_products.html.erb+ once for each instance of +product+ in the +@products+ instance variable passing the instance to the partial as a local variable called +item+ and to each partial, make the local variable +title+ available with the value +Products Page+.
1183
+
1184
+ TIP: Rails also makes a counter variable available within a partial called by the collection, named after the member of the collection followed by +_counter+. For example, if you're rendering +@products+, within the partial you can refer to +product_counter+ to tell you how many times the partial has been rendered. This does not work in conjunction with the +:as => :value+ option.
1185
+
1186
+ You can also specify a second partial to be rendered between instances of the main partial by using the +:spacer_template+ option:
1187
+
1188
+ h5. Spacer Templates
1189
+
1190
+ <erb>
1191
+ <%= render :partial => @products, :spacer_template => "product_ruler" %>
1192
+ </erb>
1193
+
1194
+ Rails will render the +_product_ruler+ partial (with no data passed in to it) between each pair of +_product+ partials.
1195
+
1196
+ h4. Using Nested Layouts
1197
+
1198
+ You may find that your application requires a layout that differs slightly from your regular application layout to support one particular controller. Rather than repeating the main layout and editing it, you can accomplish this by using nested layouts (sometimes called sub-templates). Here's an example:
1199
+
1200
+ Suppose you have the following +ApplicationController+ layout:
1201
+
1202
+ * +app/views/layouts/application.html.erb+
1203
+
1204
+ <erb>
1205
+ <html>
1206
+ <head>
1207
+ <title><%= @page_title or 'Page Title' %></title>
1208
+ <%= stylesheet_link_tag 'layout' %>
1209
+ <style type="text/css"><%= yield :stylesheets %></style>
1210
+ </head>
1211
+ <body>
1212
+ <div id="top_menu">Top menu items here</div>
1213
+ <div id="menu">Menu items here</div>
1214
+ <div id="content"><%= content_for?(:content) ? yield(:content) : yield %></div>
1215
+ </body>
1216
+ </html>
1217
+ </erb>
1218
+
1219
+ On pages generated by +NewsController+, you want to hide the top menu and add a right menu:
1220
+
1221
+ * +app/views/layouts/news.html.erb+
1222
+
1223
+ <erb>
1224
+ <% content_for :stylesheets do %>
1225
+ #top_menu {display: none}
1226
+ #right_menu {float: right; background-color: yellow; color: black}
1227
+ <% end %>
1228
+ <% content_for :content do %>
1229
+ <div id="right_menu">Right menu items here</div>
1230
+ <%= content_for?(:news_content) ? yield(:news_content) : yield %>
1231
+ <% end %>
1232
+ <%= render :template => 'layouts/application' %>
1233
+ </erb>
1234
+
1235
+ That's it. The News views will use the new layout, hiding the top menu and adding a new right menu inside the "content" div.
1236
+
1237
+ There are several ways of getting similar results with different sub-templating schemes using this technique. Note that there is no limit in nesting levels. One can use the +ActionView::render+ method via +render :template => 'layouts/news'+ to base a new layout on the News layout. If you are sure you will not subtemplate the +News+ layout, you can replace the +content_for?(:news_content) ? yield(:news_content) : yield+ with simply +yield+.