railties 3.2.22.5 → 4.0.0.beta1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +186 -78
- data/README.rdoc +4 -2
- data/lib/rails.rb +11 -44
- data/lib/rails/all.rb +0 -1
- data/lib/rails/app_rails_loader.rb +37 -0
- data/lib/rails/application.rb +176 -124
- data/lib/rails/application/bootstrap.rb +29 -23
- data/lib/rails/application/configuration.rb +54 -48
- data/lib/rails/application/finisher.rb +8 -12
- data/lib/rails/application/routes_reloader.rb +2 -2
- data/lib/rails/backtrace_cleaner.rb +1 -16
- data/lib/rails/cli.rb +2 -2
- data/lib/rails/code_statistics.rb +30 -45
- data/lib/rails/code_statistics_calculator.rb +79 -0
- data/lib/rails/commands.rb +44 -37
- data/lib/rails/commands/application.rb +14 -9
- data/lib/rails/commands/console.rb +77 -33
- data/lib/rails/commands/dbconsole.rb +115 -64
- data/lib/rails/commands/destroy.rb +2 -3
- data/lib/rails/commands/generate.rb +2 -3
- data/lib/rails/commands/plugin_new.rb +0 -2
- data/lib/rails/commands/runner.rb +4 -5
- data/lib/rails/commands/server.rb +28 -9
- data/lib/rails/commands/update.rb +1 -1
- data/lib/rails/configuration.rb +35 -8
- data/lib/rails/console/app.rb +0 -4
- data/lib/rails/deprecation.rb +19 -0
- data/lib/rails/engine.rb +111 -92
- data/lib/rails/engine/commands.rb +1 -3
- data/lib/rails/engine/configuration.rb +26 -19
- data/lib/rails/engine/railties.rb +14 -18
- data/lib/rails/generators.rb +30 -50
- data/lib/rails/generators/actions.rb +38 -103
- data/lib/rails/generators/active_model.rb +14 -6
- data/lib/rails/generators/app_base.rb +70 -83
- data/lib/rails/generators/base.rb +23 -35
- data/lib/rails/generators/css/assets/assets_generator.rb +3 -3
- data/lib/rails/generators/css/scaffold/scaffold_generator.rb +3 -3
- data/lib/rails/generators/erb.rb +2 -2
- data/lib/rails/generators/erb/controller/controller_generator.rb +4 -4
- data/lib/rails/generators/erb/mailer/mailer_generator.rb +3 -3
- data/lib/rails/generators/erb/scaffold/scaffold_generator.rb +4 -4
- data/lib/rails/generators/erb/scaffold/templates/index.html.erb +18 -14
- data/lib/rails/generators/erb/scaffold/templates/show.html.erb +1 -1
- data/lib/rails/generators/generated_attribute.rb +51 -16
- data/lib/rails/generators/js/assets/assets_generator.rb +3 -3
- data/lib/rails/generators/migration.rb +1 -5
- data/lib/rails/generators/named_base.rb +20 -25
- data/lib/rails/generators/rails/app/app_generator.rb +40 -48
- data/lib/rails/generators/rails/app/templates/Gemfile +8 -7
- data/lib/rails/generators/rails/app/templates/README.rdoc +28 -0
- data/lib/rails/generators/rails/app/templates/Rakefile +0 -1
- data/lib/rails/generators/rails/app/templates/app/assets/javascripts/application.js.tt +2 -1
- data/lib/rails/generators/rails/app/templates/app/controllers/application_controller.rb.tt +5 -0
- data/lib/rails/generators/rails/app/templates/app/views/layouts/application.html.erb.tt +2 -2
- data/lib/rails/generators/rails/app/templates/bin/bundle +2 -0
- data/lib/rails/generators/rails/app/templates/bin/rails +3 -0
- data/lib/rails/generators/rails/app/templates/bin/rake +3 -0
- data/lib/rails/generators/rails/app/templates/config/application.rb +5 -43
- data/lib/rails/generators/rails/app/templates/config/boot.rb +0 -2
- data/lib/rails/generators/rails/app/templates/config/databases/jdbcpostgresql.yml +2 -2
- data/lib/rails/generators/rails/app/templates/config/databases/mysql.yml +1 -4
- data/lib/rails/generators/rails/app/templates/config/databases/postgresql.yml +8 -3
- data/lib/rails/generators/rails/app/templates/config/databases/sqlserver.yml +57 -0
- data/lib/rails/generators/rails/app/templates/config/environment.rb +2 -2
- data/lib/rails/generators/rails/app/templates/config/environments/development.rb.tt +9 -19
- data/lib/rails/generators/rails/app/templates/config/environments/production.rb.tt +40 -29
- data/lib/rails/generators/rails/app/templates/config/environments/test.rb.tt +12 -15
- data/lib/rails/generators/rails/app/templates/config/initializers/filter_parameter_logging.rb +4 -0
- data/lib/rails/generators/rails/app/templates/config/initializers/inflections.rb +6 -5
- data/lib/rails/generators/rails/app/templates/config/initializers/secret_token.rb.tt +6 -1
- data/lib/rails/generators/rails/app/templates/config/initializers/session_store.rb.tt +1 -6
- data/lib/rails/generators/rails/app/templates/config/initializers/wrap_parameters.rb.tt +6 -6
- data/lib/rails/generators/rails/app/templates/config/locales/en.yml +20 -2
- data/lib/rails/generators/rails/app/templates/config/routes.rb +15 -24
- data/lib/rails/generators/rails/app/templates/db/seeds.rb.tt +2 -2
- data/lib/rails/generators/rails/app/templates/gitignore +3 -2
- data/lib/rails/generators/rails/app/templates/public/404.html +2 -1
- data/lib/rails/generators/rails/app/templates/public/422.html +1 -1
- data/lib/rails/generators/rails/app/templates/public/500.html +2 -1
- data/lib/rails/generators/rails/app/templates/public/robots.txt +1 -1
- data/lib/rails/generators/rails/app/templates/test/test_helper.rb +3 -1
- data/lib/rails/generators/rails/assets/assets_generator.rb +5 -5
- data/lib/rails/generators/rails/controller/USAGE +7 -7
- data/lib/rails/generators/rails/controller/controller_generator.rb +3 -3
- data/lib/rails/generators/rails/controller/templates/controller.rb +1 -1
- data/lib/rails/generators/rails/generator/generator_generator.rb +4 -4
- data/lib/rails/generators/rails/helper/USAGE +1 -1
- data/lib/rails/generators/rails/helper/helper_generator.rb +2 -2
- data/lib/rails/generators/rails/integration_test/integration_test_generator.rb +2 -2
- data/lib/rails/generators/rails/migration/USAGE +12 -6
- data/lib/rails/generators/rails/migration/migration_generator.rb +3 -3
- data/lib/rails/generators/rails/model/USAGE +53 -4
- data/lib/rails/generators/rails/model/model_generator.rb +3 -3
- data/lib/rails/generators/rails/plugin_new/plugin_new_generator.rb +81 -39
- data/lib/rails/generators/rails/plugin_new/templates/%name%.gemspec +2 -2
- data/lib/rails/generators/rails/plugin_new/templates/Gemfile +16 -1
- data/lib/rails/generators/rails/plugin_new/templates/Rakefile +3 -9
- data/lib/rails/generators/rails/plugin_new/templates/app/controllers/%name%/application_controller.rb.tt +0 -1
- data/lib/rails/generators/rails/plugin_new/templates/app/views/layouts/%name%/application.html.erb.tt +1 -1
- data/lib/rails/generators/rails/plugin_new/templates/{script → bin}/rails.tt +0 -0
- data/lib/rails/generators/rails/plugin_new/templates/gitignore +3 -0
- data/lib/rails/generators/rails/plugin_new/templates/lib/%name%.rb +1 -1
- data/lib/rails/generators/rails/plugin_new/templates/rails/application.rb +0 -1
- data/lib/rails/generators/rails/plugin_new/templates/rails/boot.rb +0 -1
- data/lib/rails/generators/rails/resource/resource_generator.rb +5 -5
- data/lib/rails/generators/rails/resource_route/resource_route_generator.rb +2 -2
- data/lib/rails/generators/rails/scaffold/scaffold_generator.rb +11 -7
- data/lib/rails/generators/rails/scaffold_controller/USAGE +4 -4
- data/lib/rails/generators/rails/scaffold_controller/scaffold_controller_generator.rb +9 -7
- data/lib/rails/generators/rails/scaffold_controller/templates/controller.rb +27 -48
- data/lib/rails/generators/rails/task/task_generator.rb +2 -2
- data/lib/rails/generators/rails/task/templates/task.rb +1 -1
- data/lib/rails/generators/resource_helpers.rb +3 -5
- data/lib/rails/generators/test_case.rb +22 -28
- data/lib/rails/generators/test_unit.rb +3 -3
- data/lib/rails/generators/test_unit/controller/controller_generator.rb +6 -6
- data/lib/rails/generators/test_unit/helper/helper_generator.rb +5 -5
- data/lib/rails/generators/test_unit/integration/integration_generator.rb +4 -4
- data/lib/rails/generators/test_unit/mailer/mailer_generator.rb +6 -6
- data/lib/rails/generators/test_unit/model/model_generator.rb +7 -7
- data/lib/rails/generators/test_unit/model/templates/fixtures.yml +8 -2
- data/lib/rails/generators/test_unit/plugin/plugin_generator.rb +4 -4
- data/lib/rails/generators/test_unit/plugin/templates/test_helper.rb +1 -2
- data/lib/rails/generators/test_unit/scaffold/scaffold_generator.rb +10 -19
- data/lib/rails/generators/test_unit/scaffold/templates/functional_test.rb +5 -5
- data/lib/rails/info.rb +3 -3
- data/lib/rails/info_controller.rb +26 -8
- data/lib/rails/initializable.rb +2 -2
- data/lib/rails/paths.rb +59 -54
- data/lib/rails/rack/debugger.rb +2 -2
- data/lib/rails/rack/logger.rb +33 -8
- data/lib/rails/railtie.rb +24 -24
- data/lib/rails/railtie/configurable.rb +1 -1
- data/lib/rails/railtie/configuration.rb +15 -5
- data/lib/rails/ruby_version_check.rb +3 -13
- data/lib/rails/rubyprof_ext.rb +1 -1
- data/lib/rails/source_annotation_extractor.rb +40 -17
- data/lib/rails/tasks/annotations.rake +1 -1
- data/lib/rails/tasks/documentation.rake +10 -57
- data/lib/rails/tasks/engine.rake +1 -1
- data/lib/rails/tasks/framework.rake +9 -9
- data/lib/rails/tasks/log.rake +18 -4
- data/lib/rails/tasks/middleware.rake +1 -1
- data/lib/rails/tasks/misc.rake +2 -9
- data/lib/rails/tasks/routes.rake +4 -6
- data/lib/rails/tasks/statistics.rake +8 -2
- data/lib/rails/tasks/tmp.rake +12 -4
- data/lib/rails/templates/layouts/application.html.erb +36 -0
- data/lib/rails/templates/rails/info/properties.html.erb +1 -0
- data/lib/rails/templates/rails/info/routes.html.erb +9 -0
- data/lib/rails/{generators/rails/app/templates/public/index.html → templates/rails/welcome/index.html.erb} +18 -14
- data/lib/rails/test_help.rb +6 -29
- data/lib/rails/test_unit/railtie.rb +2 -3
- data/lib/rails/test_unit/testing.rake +45 -33
- data/lib/rails/version.rb +4 -4
- data/lib/rails/welcome_controller.rb +7 -0
- metadata +86 -378
- data/guides/assets/images/belongs_to.png +0 -0
- data/guides/assets/images/book_icon.gif +0 -0
- data/guides/assets/images/bullet.gif +0 -0
- data/guides/assets/images/challenge.png +0 -0
- data/guides/assets/images/chapters_icon.gif +0 -0
- data/guides/assets/images/check_bullet.gif +0 -0
- data/guides/assets/images/credits_pic_blank.gif +0 -0
- data/guides/assets/images/csrf.png +0 -0
- data/guides/assets/images/customized_error_messages.png +0 -0
- data/guides/assets/images/edge_badge.png +0 -0
- data/guides/assets/images/error_messages.png +0 -0
- data/guides/assets/images/feature_tile.gif +0 -0
- data/guides/assets/images/footer_tile.gif +0 -0
- data/guides/assets/images/fxn.png +0 -0
- data/guides/assets/images/grey_bullet.gif +0 -0
- data/guides/assets/images/habtm.png +0 -0
- data/guides/assets/images/has_many.png +0 -0
- data/guides/assets/images/has_many_through.png +0 -0
- data/guides/assets/images/has_one.png +0 -0
- data/guides/assets/images/has_one_through.png +0 -0
- data/guides/assets/images/header_backdrop.png +0 -0
- data/guides/assets/images/header_tile.gif +0 -0
- data/guides/assets/images/i18n/demo_html_safe.png +0 -0
- data/guides/assets/images/i18n/demo_localized_pirate.png +0 -0
- data/guides/assets/images/i18n/demo_translated_en.png +0 -0
- data/guides/assets/images/i18n/demo_translated_pirate.png +0 -0
- data/guides/assets/images/i18n/demo_translation_missing.png +0 -0
- data/guides/assets/images/i18n/demo_untranslated.png +0 -0
- data/guides/assets/images/icons/README +0 -5
- data/guides/assets/images/icons/callouts/1.png +0 -0
- data/guides/assets/images/icons/callouts/10.png +0 -0
- data/guides/assets/images/icons/callouts/11.png +0 -0
- data/guides/assets/images/icons/callouts/12.png +0 -0
- data/guides/assets/images/icons/callouts/13.png +0 -0
- data/guides/assets/images/icons/callouts/14.png +0 -0
- data/guides/assets/images/icons/callouts/15.png +0 -0
- data/guides/assets/images/icons/callouts/2.png +0 -0
- data/guides/assets/images/icons/callouts/3.png +0 -0
- data/guides/assets/images/icons/callouts/4.png +0 -0
- data/guides/assets/images/icons/callouts/5.png +0 -0
- data/guides/assets/images/icons/callouts/6.png +0 -0
- data/guides/assets/images/icons/callouts/7.png +0 -0
- data/guides/assets/images/icons/callouts/8.png +0 -0
- data/guides/assets/images/icons/callouts/9.png +0 -0
- data/guides/assets/images/icons/caution.png +0 -0
- data/guides/assets/images/icons/example.png +0 -0
- data/guides/assets/images/icons/home.png +0 -0
- data/guides/assets/images/icons/important.png +0 -0
- data/guides/assets/images/icons/next.png +0 -0
- data/guides/assets/images/icons/note.png +0 -0
- data/guides/assets/images/icons/prev.png +0 -0
- data/guides/assets/images/icons/tip.png +0 -0
- data/guides/assets/images/icons/up.png +0 -0
- data/guides/assets/images/icons/warning.png +0 -0
- data/guides/assets/images/jaimeiniesta.jpg +0 -0
- data/guides/assets/images/nav_arrow.gif +0 -0
- data/guides/assets/images/polymorphic.png +0 -0
- data/guides/assets/images/posts_index.png +0 -0
- data/guides/assets/images/radar.png +0 -0
- data/guides/assets/images/rails_guides_kindle_cover.jpg +0 -0
- data/guides/assets/images/rails_guides_logo.gif +0 -0
- data/guides/assets/images/rails_logo_remix.gif +0 -0
- data/guides/assets/images/rails_welcome.png +0 -0
- data/guides/assets/images/session_fixation.png +0 -0
- data/guides/assets/images/tab_grey.gif +0 -0
- data/guides/assets/images/tab_info.gif +0 -0
- data/guides/assets/images/tab_note.gif +0 -0
- data/guides/assets/images/tab_red.gif +0 -0
- data/guides/assets/images/tab_yellow.gif +0 -0
- data/guides/assets/images/tab_yellow.png +0 -0
- data/guides/assets/images/validation_error_messages.png +0 -0
- data/guides/assets/images/vijaydev.jpg +0 -0
- data/guides/assets/javascripts/guides.js +0 -7
- data/guides/assets/javascripts/syntaxhighlighter/shBrushAS3.js +0 -59
- data/guides/assets/javascripts/syntaxhighlighter/shBrushAppleScript.js +0 -75
- data/guides/assets/javascripts/syntaxhighlighter/shBrushBash.js +0 -59
- data/guides/assets/javascripts/syntaxhighlighter/shBrushCSharp.js +0 -65
- data/guides/assets/javascripts/syntaxhighlighter/shBrushColdFusion.js +0 -100
- data/guides/assets/javascripts/syntaxhighlighter/shBrushCpp.js +0 -97
- data/guides/assets/javascripts/syntaxhighlighter/shBrushCss.js +0 -91
- data/guides/assets/javascripts/syntaxhighlighter/shBrushDelphi.js +0 -55
- data/guides/assets/javascripts/syntaxhighlighter/shBrushDiff.js +0 -41
- data/guides/assets/javascripts/syntaxhighlighter/shBrushErlang.js +0 -52
- data/guides/assets/javascripts/syntaxhighlighter/shBrushGroovy.js +0 -67
- data/guides/assets/javascripts/syntaxhighlighter/shBrushJScript.js +0 -52
- data/guides/assets/javascripts/syntaxhighlighter/shBrushJava.js +0 -57
- data/guides/assets/javascripts/syntaxhighlighter/shBrushJavaFX.js +0 -58
- data/guides/assets/javascripts/syntaxhighlighter/shBrushPerl.js +0 -72
- data/guides/assets/javascripts/syntaxhighlighter/shBrushPhp.js +0 -88
- data/guides/assets/javascripts/syntaxhighlighter/shBrushPlain.js +0 -33
- data/guides/assets/javascripts/syntaxhighlighter/shBrushPowerShell.js +0 -74
- data/guides/assets/javascripts/syntaxhighlighter/shBrushPython.js +0 -64
- data/guides/assets/javascripts/syntaxhighlighter/shBrushRuby.js +0 -55
- data/guides/assets/javascripts/syntaxhighlighter/shBrushSass.js +0 -94
- data/guides/assets/javascripts/syntaxhighlighter/shBrushScala.js +0 -51
- data/guides/assets/javascripts/syntaxhighlighter/shBrushSql.js +0 -66
- data/guides/assets/javascripts/syntaxhighlighter/shBrushVb.js +0 -56
- data/guides/assets/javascripts/syntaxhighlighter/shBrushXml.js +0 -69
- data/guides/assets/javascripts/syntaxhighlighter/shCore.js +0 -17
- data/guides/assets/stylesheets/fixes.css +0 -16
- data/guides/assets/stylesheets/kindle.css +0 -11
- data/guides/assets/stylesheets/main.css +0 -453
- data/guides/assets/stylesheets/print.css +0 -52
- data/guides/assets/stylesheets/reset.css +0 -43
- data/guides/assets/stylesheets/style.css +0 -13
- data/guides/assets/stylesheets/syntaxhighlighter/shCore.css +0 -226
- data/guides/assets/stylesheets/syntaxhighlighter/shCoreDefault.css +0 -328
- data/guides/assets/stylesheets/syntaxhighlighter/shCoreDjango.css +0 -331
- data/guides/assets/stylesheets/syntaxhighlighter/shCoreEclipse.css +0 -339
- data/guides/assets/stylesheets/syntaxhighlighter/shCoreEmacs.css +0 -324
- data/guides/assets/stylesheets/syntaxhighlighter/shCoreFadeToGrey.css +0 -328
- data/guides/assets/stylesheets/syntaxhighlighter/shCoreMDUltra.css +0 -324
- data/guides/assets/stylesheets/syntaxhighlighter/shCoreMidnight.css +0 -324
- data/guides/assets/stylesheets/syntaxhighlighter/shCoreRDark.css +0 -324
- data/guides/assets/stylesheets/syntaxhighlighter/shThemeDefault.css +0 -117
- data/guides/assets/stylesheets/syntaxhighlighter/shThemeDjango.css +0 -120
- data/guides/assets/stylesheets/syntaxhighlighter/shThemeEclipse.css +0 -128
- data/guides/assets/stylesheets/syntaxhighlighter/shThemeEmacs.css +0 -113
- data/guides/assets/stylesheets/syntaxhighlighter/shThemeFadeToGrey.css +0 -117
- data/guides/assets/stylesheets/syntaxhighlighter/shThemeMDUltra.css +0 -113
- data/guides/assets/stylesheets/syntaxhighlighter/shThemeMidnight.css +0 -113
- data/guides/assets/stylesheets/syntaxhighlighter/shThemeRDark.css +0 -113
- data/guides/assets/stylesheets/syntaxhighlighter/shThemeRailsGuides.css +0 -116
- data/guides/code/getting_started/Gemfile +0 -38
- data/guides/code/getting_started/README.rdoc +0 -261
- data/guides/code/getting_started/Rakefile +0 -7
- data/guides/code/getting_started/app/assets/images/rails.png +0 -0
- data/guides/code/getting_started/app/assets/javascripts/application.js +0 -15
- data/guides/code/getting_started/app/assets/javascripts/comments.js.coffee +0 -3
- data/guides/code/getting_started/app/assets/javascripts/home.js.coffee +0 -3
- data/guides/code/getting_started/app/assets/javascripts/posts.js.coffee +0 -3
- data/guides/code/getting_started/app/assets/stylesheets/application.css +0 -13
- data/guides/code/getting_started/app/assets/stylesheets/comments.css.scss +0 -3
- data/guides/code/getting_started/app/assets/stylesheets/home.css.scss +0 -3
- data/guides/code/getting_started/app/assets/stylesheets/posts.css.scss +0 -3
- data/guides/code/getting_started/app/assets/stylesheets/scaffolds.css.scss +0 -56
- data/guides/code/getting_started/app/controllers/application_controller.rb +0 -3
- data/guides/code/getting_started/app/controllers/comments_controller.rb +0 -16
- data/guides/code/getting_started/app/controllers/home_controller.rb +0 -5
- data/guides/code/getting_started/app/controllers/posts_controller.rb +0 -84
- data/guides/code/getting_started/app/helpers/application_helper.rb +0 -2
- data/guides/code/getting_started/app/helpers/comments_helper.rb +0 -2
- data/guides/code/getting_started/app/helpers/home_helper.rb +0 -2
- data/guides/code/getting_started/app/helpers/posts_helper.rb +0 -5
- data/guides/code/getting_started/app/models/comment.rb +0 -3
- data/guides/code/getting_started/app/models/post.rb +0 -11
- data/guides/code/getting_started/app/models/tag.rb +0 -3
- data/guides/code/getting_started/app/views/comments/_comment.html.erb +0 -15
- data/guides/code/getting_started/app/views/comments/_form.html.erb +0 -13
- data/guides/code/getting_started/app/views/home/index.html.erb +0 -2
- data/guides/code/getting_started/app/views/layouts/application.html.erb +0 -14
- data/guides/code/getting_started/app/views/posts/_form.html.erb +0 -32
- data/guides/code/getting_started/app/views/posts/edit.html.erb +0 -6
- data/guides/code/getting_started/app/views/posts/index.html.erb +0 -27
- data/guides/code/getting_started/app/views/posts/new.html.erb +0 -5
- data/guides/code/getting_started/app/views/posts/show.html.erb +0 -31
- data/guides/code/getting_started/app/views/tags/_form.html.erb +0 -12
- data/guides/code/getting_started/config.ru +0 -4
- data/guides/code/getting_started/config/application.rb +0 -59
- data/guides/code/getting_started/config/boot.rb +0 -6
- data/guides/code/getting_started/config/database.yml +0 -25
- data/guides/code/getting_started/config/environment.rb +0 -5
- data/guides/code/getting_started/config/environments/development.rb +0 -37
- data/guides/code/getting_started/config/environments/production.rb +0 -67
- data/guides/code/getting_started/config/environments/test.rb +0 -37
- data/guides/code/getting_started/config/initializers/backtrace_silencers.rb +0 -7
- data/guides/code/getting_started/config/initializers/inflections.rb +0 -15
- data/guides/code/getting_started/config/initializers/mime_types.rb +0 -5
- data/guides/code/getting_started/config/initializers/secret_token.rb +0 -7
- data/guides/code/getting_started/config/initializers/session_store.rb +0 -8
- data/guides/code/getting_started/config/initializers/wrap_parameters.rb +0 -14
- data/guides/code/getting_started/config/locales/en.yml +0 -5
- data/guides/code/getting_started/config/routes.rb +0 -64
- data/guides/code/getting_started/db/migrate/20110901012504_create_posts.rb +0 -11
- data/guides/code/getting_started/db/migrate/20110901012815_create_comments.rb +0 -12
- data/guides/code/getting_started/db/migrate/20110901013701_create_tags.rb +0 -11
- data/guides/code/getting_started/db/schema.rb +0 -43
- data/guides/code/getting_started/db/seeds.rb +0 -7
- data/guides/code/getting_started/doc/README_FOR_APP +0 -2
- data/guides/code/getting_started/public/404.html +0 -26
- data/guides/code/getting_started/public/422.html +0 -26
- data/guides/code/getting_started/public/500.html +0 -25
- data/guides/code/getting_started/public/favicon.ico +0 -0
- data/guides/code/getting_started/public/robots.txt +0 -5
- data/guides/code/getting_started/script/rails +0 -6
- data/guides/code/getting_started/test/fixtures/comments.yml +0 -11
- data/guides/code/getting_started/test/fixtures/posts.yml +0 -11
- data/guides/code/getting_started/test/fixtures/tags.yml +0 -9
- data/guides/code/getting_started/test/functional/comments_controller_test.rb +0 -7
- data/guides/code/getting_started/test/functional/home_controller_test.rb +0 -9
- data/guides/code/getting_started/test/functional/posts_controller_test.rb +0 -49
- data/guides/code/getting_started/test/performance/browsing_test.rb +0 -12
- data/guides/code/getting_started/test/test_helper.rb +0 -13
- data/guides/code/getting_started/test/unit/comment_test.rb +0 -7
- data/guides/code/getting_started/test/unit/helpers/comments_helper_test.rb +0 -4
- data/guides/code/getting_started/test/unit/helpers/home_helper_test.rb +0 -4
- data/guides/code/getting_started/test/unit/helpers/posts_helper_test.rb +0 -4
- data/guides/code/getting_started/test/unit/post_test.rb +0 -7
- data/guides/code/getting_started/test/unit/tag_test.rb +0 -7
- data/guides/rails_guides.rb +0 -50
- data/guides/rails_guides/generator.rb +0 -310
- data/guides/rails_guides/helpers.rb +0 -45
- data/guides/rails_guides/indexer.rb +0 -69
- data/guides/rails_guides/levenshtein.rb +0 -31
- data/guides/rails_guides/textile_extensions.rb +0 -63
- data/guides/source/2_2_release_notes.textile +0 -422
- data/guides/source/2_3_release_notes.textile +0 -610
- data/guides/source/3_0_release_notes.textile +0 -595
- data/guides/source/3_1_release_notes.textile +0 -553
- data/guides/source/3_2_release_notes.textile +0 -540
- data/guides/source/_license.html.erb +0 -2
- data/guides/source/_welcome.html.erb +0 -19
- data/guides/source/action_controller_overview.textile +0 -820
- data/guides/source/action_mailer_basics.textile +0 -516
- data/guides/source/action_view_overview.textile +0 -1497
- data/guides/source/active_model_basics.textile +0 -205
- data/guides/source/active_record_basics.textile +0 -218
- data/guides/source/active_record_querying.textile +0 -1433
- data/guides/source/active_record_validations_callbacks.textile +0 -1287
- data/guides/source/active_resource_basics.textile +0 -120
- data/guides/source/active_support_core_extensions.textile +0 -3715
- data/guides/source/api_documentation_guidelines.textile +0 -185
- data/guides/source/asset_pipeline.textile +0 -707
- data/guides/source/association_basics.textile +0 -1959
- data/guides/source/caching_with_rails.textile +0 -437
- data/guides/source/command_line.textile +0 -574
- data/guides/source/configuring.textile +0 -643
- data/guides/source/contributing_to_ruby_on_rails.textile +0 -448
- data/guides/source/credits.html.erb +0 -72
- data/guides/source/debugging_rails_applications.textile +0 -714
- data/guides/source/documents.yaml +0 -157
- data/guides/source/engines.textile +0 -618
- data/guides/source/form_helpers.textile +0 -798
- data/guides/source/generators.textile +0 -621
- data/guides/source/getting_started.textile +0 -1931
- data/guides/source/i18n.textile +0 -931
- data/guides/source/index.html.erb +0 -30
- data/guides/source/initialization.textile +0 -1116
- data/guides/source/kindle/KINDLE.md +0 -26
- data/guides/source/kindle/copyright.html.erb +0 -1
- data/guides/source/kindle/layout.html.erb +0 -27
- data/guides/source/kindle/rails_guides.opf.erb +0 -52
- data/guides/source/kindle/toc.html.erb +0 -24
- data/guides/source/kindle/toc.ncx.erb +0 -64
- data/guides/source/kindle/welcome.html.erb +0 -5
- data/guides/source/layout.html.erb +0 -124
- data/guides/source/layouts_and_rendering.textile +0 -1237
- data/guides/source/migrations.textile +0 -900
- data/guides/source/nested_model_forms.textile +0 -222
- data/guides/source/performance_testing.textile +0 -597
- data/guides/source/plugins.textile +0 -464
- data/guides/source/rails_application_templates.textile +0 -240
- data/guides/source/rails_on_rack.textile +0 -236
- data/guides/source/routing.textile +0 -885
- data/guides/source/ruby_on_rails_guides_guidelines.textile +0 -79
- data/guides/source/security.textile +0 -1004
- data/guides/source/testing.textile +0 -947
- data/guides/w3c_validator.rb +0 -91
- data/lib/rails/application/railties.rb +0 -13
- data/lib/rails/application/route_inspector.rb +0 -84
- data/lib/rails/commands/benchmarker.rb +0 -34
- data/lib/rails/commands/plugin.rb +0 -544
- data/lib/rails/commands/profiler.rb +0 -33
- data/lib/rails/generators/rails/app/templates/README +0 -261
- data/lib/rails/generators/rails/app/templates/app/controllers/application_controller.rb +0 -3
- data/lib/rails/generators/rails/app/templates/app/mailers/.empty_directory +0 -0
- data/lib/rails/generators/rails/app/templates/app/models/.empty_directory +0 -0
- data/lib/rails/generators/rails/app/templates/doc/README_FOR_APP +0 -2
- data/lib/rails/generators/rails/app/templates/public/stylesheets/.empty_directory +0 -0
- data/lib/rails/generators/rails/app/templates/script/rails +0 -5
- data/lib/rails/generators/rails/app/templates/test/fixtures/.empty_directory +0 -0
- data/lib/rails/generators/rails/app/templates/test/functional/.empty_directory +0 -0
- data/lib/rails/generators/rails/app/templates/test/integration/.empty_directory +0 -0
- data/lib/rails/generators/rails/app/templates/test/performance/browsing_test.rb +0 -12
- data/lib/rails/generators/rails/app/templates/test/unit/.empty_directory +0 -0
- data/lib/rails/generators/rails/generator/templates/templates/.empty_directory +0 -0
- data/lib/rails/generators/rails/observer/USAGE +0 -12
- data/lib/rails/generators/rails/observer/observer_generator.rb +0 -7
- data/lib/rails/generators/rails/performance_test/USAGE +0 -10
- data/lib/rails/generators/rails/performance_test/performance_test_generator.rb +0 -7
- data/lib/rails/generators/rails/plugin_new/templates/app/mailers/.empty_directory +0 -0
- data/lib/rails/generators/rails/plugin_new/templates/app/models/.empty_directory +0 -0
- data/lib/rails/generators/rails/session_migration/USAGE +0 -8
- data/lib/rails/generators/rails/session_migration/session_migration_generator.rb +0 -8
- data/lib/rails/generators/test_unit/observer/observer_generator.rb +0 -13
- data/lib/rails/generators/test_unit/observer/templates/unit_test.rb +0 -9
- data/lib/rails/generators/test_unit/performance/performance_generator.rb +0 -13
- data/lib/rails/generators/test_unit/performance/templates/performance_test.rb +0 -12
- data/lib/rails/performance_test_help.rb +0 -3
- data/lib/rails/plugin.rb +0 -92
- data/lib/rails/script_rails_loader.rb +0 -29
@@ -1,26 +0,0 @@
|
|
1
|
-
# Rails Guides on the Kindle
|
2
|
-
|
3
|
-
|
4
|
-
## Synopsis
|
5
|
-
|
6
|
-
1. Obtain `kindlegen` from the link below and put the binary in your path
|
7
|
-
2. Run `KINDLE=1 rake generate_guides` to generate the guides and compile the `.mobi` file
|
8
|
-
3. Copy `output/kindle/rails_guides.mobi` to your Kindle
|
9
|
-
|
10
|
-
## Resources
|
11
|
-
|
12
|
-
* [StackOverflow: Kindle Periodical Format](http://stackoverflow.com/questions/5379565/kindle-periodical-format)
|
13
|
-
* Example Periodical [.ncx](https://gist.github.com/808c971ed087b839d462) and [.opf](https://gist.github.com/d6349aa8488eca2ee6d0)
|
14
|
-
* [Kindle Publishing guidelines](http://kindlegen.s3.amazonaws.com/AmazonKindlePublishingGuidelines.pdf)
|
15
|
-
* [KindleGen & Kindle Previewer](http://www.amazon.com/gp/feature.html?ie=UTF8&docId=1000234621)
|
16
|
-
|
17
|
-
## TODO
|
18
|
-
|
19
|
-
### Post release
|
20
|
-
|
21
|
-
* Integrate generated Kindle document in to published HTML guides
|
22
|
-
* Tweak heading styles (most docs use h3/h4/h5, which end up being smaller than the text under it)
|
23
|
-
* Tweak table styles (smaller text? Many of the tables are unusable on a Kindle in portrait mode)
|
24
|
-
* Have the HTML/XML TOC 'drill down' into the TOCs of the individual guides
|
25
|
-
* `.epub` generation.
|
26
|
-
|
@@ -1 +0,0 @@
|
|
1
|
-
<%= render 'license' %>
|
@@ -1,27 +0,0 @@
|
|
1
|
-
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
|
2
|
-
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
|
3
|
-
|
4
|
-
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
|
5
|
-
<head>
|
6
|
-
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
|
7
|
-
|
8
|
-
<title><%= yield(:page_title) || 'Ruby on Rails Guides' %></title>
|
9
|
-
|
10
|
-
<link rel="stylesheet" type="text/css" href="stylesheets/kindle.css" />
|
11
|
-
|
12
|
-
</head>
|
13
|
-
<body class="guide">
|
14
|
-
|
15
|
-
<% if content_for? :header_section %>
|
16
|
-
<%= yield :header_section %>
|
17
|
-
<div class="pagebreak">
|
18
|
-
<% end %>
|
19
|
-
|
20
|
-
<% if content_for? :index_section %>
|
21
|
-
<%= yield :index_section %>
|
22
|
-
<div class="pagebreak">
|
23
|
-
<% end %>
|
24
|
-
|
25
|
-
<%= yield.html_safe %>
|
26
|
-
</body>
|
27
|
-
</html>
|
@@ -1,52 +0,0 @@
|
|
1
|
-
<?xml version="1.0" encoding="utf-8"?>
|
2
|
-
|
3
|
-
<package xmlns="http://www.idpf.org/2007/opf" version="2.0" unique-identifier="RailsGuides">
|
4
|
-
<metadata>
|
5
|
-
<meta name="cover" content="cover" />
|
6
|
-
<dc-metadata xmlns:dc="http://purl.org/dc/elements/1.1/">
|
7
|
-
|
8
|
-
<dc:title>Ruby on Rails Guides (<%= @version %>)</dc:title>
|
9
|
-
|
10
|
-
<dc:language>en-us</dc:language>
|
11
|
-
<dc:creator>Ruby on Rails</dc:creator>
|
12
|
-
<dc:publisher>Ruby on Rails</dc:publisher>
|
13
|
-
<dc:subject>Reference</dc:subject>
|
14
|
-
<dc:date><%= Time.now.strftime('%Y-%m-%d') %></dc:date>
|
15
|
-
|
16
|
-
<dc:description>These guides are designed to make you immediately productive with Rails, and to help you understand how all of the pieces fit together.</dc:description>
|
17
|
-
</dc-metadata>
|
18
|
-
<x-metadata>
|
19
|
-
<output content-type="application/x-mobipocket-subscription-magazine" encoding="utf-8"/>
|
20
|
-
</x-metadata>
|
21
|
-
</metadata>
|
22
|
-
|
23
|
-
<manifest>
|
24
|
-
<!-- HTML content files [mandatory] -->
|
25
|
-
<% documents_flat.each do |document| %>
|
26
|
-
<item id="<%= document['url'] %>" media-type="text/html" href="<%= document['url'] %>" />
|
27
|
-
<% end %>
|
28
|
-
|
29
|
-
<% %w{toc.html credits.html welcome.html copyright.html}.each do |url| %>
|
30
|
-
<item id="<%= url %>" media-type="text/html" href="<%= url %>" />
|
31
|
-
<% end %>
|
32
|
-
|
33
|
-
<item id="toc" media-type="application/x-dtbncx+xml" href="toc.ncx" />
|
34
|
-
|
35
|
-
<item id="cover" media-type="image/jpeg" href="images/rails_guides_kindle_cover.jpg"/>
|
36
|
-
</manifest>
|
37
|
-
|
38
|
-
<spine toc="toc">
|
39
|
-
<itemref idref="toc.html" />
|
40
|
-
<itemref idref="welcome.html" />
|
41
|
-
<itemref idref="credits.html" />
|
42
|
-
<itemref idref="copyright.html" />
|
43
|
-
<% documents_flat.each do |document| %>
|
44
|
-
<itemref idref="<%= document['url'] %>" />
|
45
|
-
<% end %>
|
46
|
-
</spine>
|
47
|
-
|
48
|
-
<guide>
|
49
|
-
<reference type="toc" title="Table of Contents" href="toc.html"></reference>
|
50
|
-
</guide>
|
51
|
-
|
52
|
-
</package>
|
@@ -1,24 +0,0 @@
|
|
1
|
-
<% content_for :page_title do %>
|
2
|
-
Ruby on Rails Guides
|
3
|
-
<% end %>
|
4
|
-
|
5
|
-
<h1>Table of Contents</h1>
|
6
|
-
<div id="toc">
|
7
|
-
<ul><li><a href="welcome.html">Welcome</a></li></ul>
|
8
|
-
<% documents_by_section.each_with_index do |section, i| %>
|
9
|
-
<h3><%= "#{i + 1}." %> <%= section['name'] %></h3>
|
10
|
-
<ul>
|
11
|
-
<% section['documents'].each do |document| %>
|
12
|
-
<li>
|
13
|
-
<a href="<%= document['url'] %>"><%= document['name'] %></a>
|
14
|
-
<% if document['work_in_progress']%>(WIP)<% end %>
|
15
|
-
</li>
|
16
|
-
<% end %>
|
17
|
-
</ul>
|
18
|
-
<% end %>
|
19
|
-
<hr />
|
20
|
-
<ul>
|
21
|
-
<li><a href="credits.html">Credits</a></li>
|
22
|
-
<li><a href="copyright.html">Copyright & License</a></li>
|
23
|
-
<ul>
|
24
|
-
</div>
|
@@ -1,64 +0,0 @@
|
|
1
|
-
<?xml version="1.0" encoding="UTF-8"?>
|
2
|
-
<!DOCTYPE ncx PUBLIC "-//NISO//DTD ncx 2005-1//EN"
|
3
|
-
"http://www.daisy.org/z3986/2005/ncx-2005-1.dtd">
|
4
|
-
|
5
|
-
<ncx xmlns="http://www.daisy.org/z3986/2005/ncx/" version="2005-1" xml:lang="en-US">
|
6
|
-
<head>
|
7
|
-
<meta name="dtb:uid" content="RailsGuides"/>
|
8
|
-
<meta name="dtb:depth" content="2"/>
|
9
|
-
<meta name="dtb:totalPageCount" content="0"/>
|
10
|
-
<meta name="dtb:maxPageNumber" content="0"/>
|
11
|
-
</head>
|
12
|
-
<docTitle><text>Ruby on Rails Guides</text></docTitle>
|
13
|
-
<docAuthor><text>docrails</text></docAuthor>
|
14
|
-
<navMap>
|
15
|
-
<navPoint playOrder="0" class="periodical" id="periodical">
|
16
|
-
<navLabel>
|
17
|
-
<text>Table of Contents</text>
|
18
|
-
</navLabel>
|
19
|
-
<content src="toc.html"/>
|
20
|
-
|
21
|
-
<navPoint class="section" id="welcome" playOrder="1">
|
22
|
-
<navLabel>
|
23
|
-
<text>Introduction</text>
|
24
|
-
</navLabel>
|
25
|
-
<content src="welcome.html"/>
|
26
|
-
|
27
|
-
<navPoint class="article" id="welcome" playOrder="2">
|
28
|
-
<navLabel>
|
29
|
-
<text>Welcome</text>
|
30
|
-
</navLabel>
|
31
|
-
<content src="welcome.html"/>
|
32
|
-
</navPoint>
|
33
|
-
<navPoint class="article" id="credits" playOrder="3">
|
34
|
-
<navLabel><text>Credits</text></navLabel>
|
35
|
-
<content src="credits.html">
|
36
|
-
</navPoint>
|
37
|
-
<navPoint class="article" id="copyright" playOrder="4">
|
38
|
-
<navLabel><text>Copyright & License</text></navLabel>
|
39
|
-
<content src="copyright.html">
|
40
|
-
</navPoint>
|
41
|
-
</navPoint>
|
42
|
-
|
43
|
-
<% play_order = 4 %>
|
44
|
-
<% documents_by_section.each_with_index do |section, section_no| %>
|
45
|
-
<navPoint class="section" id="chapter_<%= section_no + 1 %>" playOrder="<% play_order +=1 %>">
|
46
|
-
<navLabel>
|
47
|
-
<text><%= section['name'] %></text>
|
48
|
-
</navLabel>
|
49
|
-
<content src="<%=section['documents'].first['url'] %>"/>
|
50
|
-
|
51
|
-
<% section['documents'].each_with_index do |document, document_no| %>
|
52
|
-
<navPoint class="article" id="_<%=section_no+1%>.<%=document_no+1%>" playOrder="<%=play_order +=1 %>">
|
53
|
-
<navLabel>
|
54
|
-
<text><%= document['name'] %></text>
|
55
|
-
</navLabel>
|
56
|
-
<content src="<%=document['url'] %>"/>
|
57
|
-
</navPoint>
|
58
|
-
<% end %>
|
59
|
-
</navPoint>
|
60
|
-
<% end %>
|
61
|
-
|
62
|
-
</navPoint>
|
63
|
-
</navMap>
|
64
|
-
</ncx>
|
@@ -1,124 +0,0 @@
|
|
1
|
-
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
|
2
|
-
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
|
3
|
-
|
4
|
-
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
|
5
|
-
<head>
|
6
|
-
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
|
7
|
-
|
8
|
-
<title><%= yield(:page_title) || 'Ruby on Rails Guides' %></title>
|
9
|
-
|
10
|
-
<link rel="stylesheet" type="text/css" href="stylesheets/style.css" />
|
11
|
-
<link rel="stylesheet" type="text/css" href="stylesheets/print.css" media="print" />
|
12
|
-
|
13
|
-
<link rel="stylesheet" type="text/css" href="stylesheets/syntaxhighlighter/shCore.css" />
|
14
|
-
<link rel="stylesheet" type="text/css" href="stylesheets/syntaxhighlighter/shThemeRailsGuides.css" />
|
15
|
-
|
16
|
-
<link rel="stylesheet" type="text/css" href="stylesheets/fixes.css" />
|
17
|
-
</head>
|
18
|
-
<body class="guide">
|
19
|
-
<% if @edge %>
|
20
|
-
<div>
|
21
|
-
<img src="images/edge_badge.png" alt="edge-badge" id="edge-badge" />
|
22
|
-
</div>
|
23
|
-
<% end %>
|
24
|
-
<div id="topNav">
|
25
|
-
<div class="wrapper">
|
26
|
-
<strong>More at <a href="http://rubyonrails.org/">rubyonrails.org:</a> </strong>
|
27
|
-
<a href="http://rubyonrails.org/">Overview</a> |
|
28
|
-
<a href="http://rubyonrails.org/download">Download</a> |
|
29
|
-
<a href="http://rubyonrails.org/deploy">Deploy</a> |
|
30
|
-
<a href="https://github.com/rails/rails">Code</a> |
|
31
|
-
<a href="http://rubyonrails.org/screencasts">Screencasts</a> |
|
32
|
-
<a href="http://rubyonrails.org/documentation">Documentation</a> |
|
33
|
-
<a href="http://rubyonrails.org/ecosystem">Ecosystem</a> |
|
34
|
-
<a href="http://rubyonrails.org/community">Community</a> |
|
35
|
-
<a href="http://weblog.rubyonrails.org/">Blog</a>
|
36
|
-
</div>
|
37
|
-
</div>
|
38
|
-
<div id="header">
|
39
|
-
<div class="wrapper clearfix">
|
40
|
-
<h1><a href="index.html" title="Return to home page">Guides.rubyonrails.org</a></h1>
|
41
|
-
<ul class="nav">
|
42
|
-
<li><a href="index.html">Home</a></li>
|
43
|
-
<li class="index"><a href="index.html" onclick="guideMenu(); return false;" id="guidesMenu">Guides Index</a>
|
44
|
-
<div id="guides" class="clearfix" style="display: none;">
|
45
|
-
<hr />
|
46
|
-
<% ['L', 'R'].each do |position| %>
|
47
|
-
<dl class="<%= position %>">
|
48
|
-
<% docs_for_menu(position).each do |section| %>
|
49
|
-
<dt><%= section['name'] %></dt>
|
50
|
-
<% finished_documents(section['documents']).each do |document| %>
|
51
|
-
<dd><a href="<%= document['url'] %>"><%= document['name'] %></a></dd>
|
52
|
-
<% end %>
|
53
|
-
<% end %>
|
54
|
-
</dl>
|
55
|
-
<% end %>
|
56
|
-
</div>
|
57
|
-
</li>
|
58
|
-
<li><a href="contributing_to_ruby_on_rails.html">Contribute</a></li>
|
59
|
-
<li><a href="credits.html">Credits</a></li>
|
60
|
-
</ul>
|
61
|
-
</div>
|
62
|
-
</div>
|
63
|
-
<hr class="hide" />
|
64
|
-
|
65
|
-
<div id="feature">
|
66
|
-
<div class="wrapper">
|
67
|
-
<%= yield :header_section %>
|
68
|
-
|
69
|
-
<%= yield :index_section %>
|
70
|
-
</div>
|
71
|
-
</div>
|
72
|
-
|
73
|
-
<div id="container">
|
74
|
-
<div class="wrapper">
|
75
|
-
<div id="mainCol">
|
76
|
-
<%= yield.html_safe %>
|
77
|
-
|
78
|
-
<h3>Feedback</h3>
|
79
|
-
<p>
|
80
|
-
You're encouraged to help improve the quality of this guide.
|
81
|
-
</p>
|
82
|
-
<p>
|
83
|
-
If you see any typos or factual errors you are confident to
|
84
|
-
patch, please clone the <%= link_to 'rails', 'https://github.com/rails/rails' %>
|
85
|
-
repository and open a new pull request. You can also ask for commit rights on
|
86
|
-
<%= link_to 'docrails', 'https://github.com/rails/docrails' %> if you plan to submit
|
87
|
-
several patches. Commits are reviewed, but that happens after you've submitted your
|
88
|
-
contribution. This repository is cross-merged with master periodically.
|
89
|
-
</p>
|
90
|
-
<p>
|
91
|
-
You may also find incomplete content, or stuff that is not up to date.
|
92
|
-
Please do add any missing documentation for master. Check the
|
93
|
-
<%= link_to 'Ruby on Rails Guides Guidelines', 'ruby_on_rails_guides_guidelines.html' %>
|
94
|
-
for style and conventions.
|
95
|
-
</p>
|
96
|
-
<p>
|
97
|
-
If for whatever reason you spot something to fix but cannot patch it yourself, please
|
98
|
-
<%= link_to 'open an issue', 'https://github.com/rails/rails/issues' %>.
|
99
|
-
</p>
|
100
|
-
<p>And last but not least, any kind of discussion regarding Ruby on Rails
|
101
|
-
documentation is very welcome in the <%= link_to 'rubyonrails-docs mailing list', 'http://groups.google.com/group/rubyonrails-docs' %>.
|
102
|
-
</p>
|
103
|
-
</div>
|
104
|
-
</div>
|
105
|
-
</div>
|
106
|
-
|
107
|
-
<hr class="hide" />
|
108
|
-
<div id="footer">
|
109
|
-
<div class="wrapper">
|
110
|
-
<%= render 'license' %>
|
111
|
-
</div>
|
112
|
-
</div>
|
113
|
-
|
114
|
-
<script type="text/javascript" src="javascripts/guides.js"></script>
|
115
|
-
<script type="text/javascript" src="javascripts/syntaxhighlighter/shCore.js"></script>
|
116
|
-
<script type="text/javascript" src="javascripts/syntaxhighlighter/shBrushRuby.js"></script>
|
117
|
-
<script type="text/javascript" src="javascripts/syntaxhighlighter/shBrushXml.js"></script>
|
118
|
-
<script type="text/javascript" src="javascripts/syntaxhighlighter/shBrushSql.js"></script>
|
119
|
-
<script type="text/javascript" src="javascripts/syntaxhighlighter/shBrushPlain.js"></script>
|
120
|
-
<script type="text/javascript">
|
121
|
-
SyntaxHighlighter.all()
|
122
|
-
</script>
|
123
|
-
</body>
|
124
|
-
</html>
|
@@ -1,1237 +0,0 @@
|
|
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 +&& return+ because +&& 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 +<head>+ 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 +<link>+ 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 +<img />+ 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 +<video>+ 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 +<video>+ 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 +<audio>+ 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+.
|