rails 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.
Potentially problematic release.
This version of rails might be problematic. Click here for more details.
- checksums.yaml +4 -4
- data/README.rdoc +77 -0
- data/guides/CHANGELOG.md +9 -0
- data/guides/Rakefile +77 -0
- 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/edge_badge.png +0 -0
- data/guides/assets/images/favicon.ico +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/getting_started/confirm_dialog.png +0 -0
- data/guides/assets/images/getting_started/forbidden_attributes_for_new_post.png +0 -0
- data/guides/assets/images/getting_started/form_with_errors.png +0 -0
- data/guides/assets/images/getting_started/index_action_with_edit_link.png +0 -0
- data/guides/assets/images/getting_started/new_post.png +0 -0
- data/guides/assets/images/getting_started/post_with_comments.png +0 -0
- data/guides/assets/images/getting_started/routing_error_no_controller.png +0 -0
- data/guides/assets/images/getting_started/routing_error_no_route_matches.png +0 -0
- data/guides/assets/images/getting_started/show_action_for_posts.png +0 -0
- data/guides/assets/images/getting_started/template_is_missing_posts_new.png +0 -0
- data/guides/assets/images/getting_started/undefined_method_post_path.png +0 -0
- data/guides/assets/images/getting_started/unknown_action_create_for_posts.png +0 -0
- data/guides/assets/images/getting_started/unknown_action_new_for_posts.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 +5 -0
- 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/oscardelben.jpg +0 -0
- data/guides/assets/images/polymorphic.png +0 -0
- data/guides/assets/images/radar.png +0 -0
- data/guides/assets/images/rails4_features.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/vijaydev.jpg +0 -0
- data/guides/assets/javascripts/guides.js +57 -0
- data/guides/assets/javascripts/jquery.min.js +4 -0
- data/guides/assets/javascripts/responsive-tables.js +43 -0
- data/guides/assets/javascripts/syntaxhighlighter/shBrushAS3.js +59 -0
- data/guides/assets/javascripts/syntaxhighlighter/shBrushAppleScript.js +75 -0
- data/guides/assets/javascripts/syntaxhighlighter/shBrushBash.js +59 -0
- data/guides/assets/javascripts/syntaxhighlighter/shBrushCSharp.js +65 -0
- data/guides/assets/javascripts/syntaxhighlighter/shBrushColdFusion.js +100 -0
- data/guides/assets/javascripts/syntaxhighlighter/shBrushCpp.js +97 -0
- data/guides/assets/javascripts/syntaxhighlighter/shBrushCss.js +91 -0
- data/guides/assets/javascripts/syntaxhighlighter/shBrushDelphi.js +55 -0
- data/guides/assets/javascripts/syntaxhighlighter/shBrushDiff.js +41 -0
- data/guides/assets/javascripts/syntaxhighlighter/shBrushErlang.js +52 -0
- data/guides/assets/javascripts/syntaxhighlighter/shBrushGroovy.js +67 -0
- data/guides/assets/javascripts/syntaxhighlighter/shBrushJScript.js +52 -0
- data/guides/assets/javascripts/syntaxhighlighter/shBrushJava.js +57 -0
- data/guides/assets/javascripts/syntaxhighlighter/shBrushJavaFX.js +58 -0
- data/guides/assets/javascripts/syntaxhighlighter/shBrushPerl.js +72 -0
- data/guides/assets/javascripts/syntaxhighlighter/shBrushPhp.js +88 -0
- data/guides/assets/javascripts/syntaxhighlighter/shBrushPlain.js +33 -0
- data/guides/assets/javascripts/syntaxhighlighter/shBrushPowerShell.js +74 -0
- data/guides/assets/javascripts/syntaxhighlighter/shBrushPython.js +64 -0
- data/guides/assets/javascripts/syntaxhighlighter/shBrushRuby.js +55 -0
- data/guides/assets/javascripts/syntaxhighlighter/shBrushSass.js +94 -0
- data/guides/assets/javascripts/syntaxhighlighter/shBrushScala.js +51 -0
- data/guides/assets/javascripts/syntaxhighlighter/shBrushSql.js +66 -0
- data/guides/assets/javascripts/syntaxhighlighter/shBrushVb.js +56 -0
- data/guides/assets/javascripts/syntaxhighlighter/shBrushXml.js +69 -0
- data/guides/assets/javascripts/syntaxhighlighter/shCore.js +17 -0
- data/guides/assets/stylesheets/fixes.css +16 -0
- data/guides/assets/stylesheets/kindle.css +11 -0
- data/guides/assets/stylesheets/main.css +709 -0
- data/guides/assets/stylesheets/print.css +52 -0
- data/guides/assets/stylesheets/reset.css +43 -0
- data/guides/assets/stylesheets/responsive-tables.css +50 -0
- data/guides/assets/stylesheets/style.css +13 -0
- data/guides/assets/stylesheets/syntaxhighlighter/shCore.css +226 -0
- data/guides/assets/stylesheets/syntaxhighlighter/shCoreDefault.css +328 -0
- data/guides/assets/stylesheets/syntaxhighlighter/shCoreDjango.css +331 -0
- data/guides/assets/stylesheets/syntaxhighlighter/shCoreEclipse.css +339 -0
- data/guides/assets/stylesheets/syntaxhighlighter/shCoreEmacs.css +324 -0
- data/guides/assets/stylesheets/syntaxhighlighter/shCoreFadeToGrey.css +328 -0
- data/guides/assets/stylesheets/syntaxhighlighter/shCoreMDUltra.css +324 -0
- data/guides/assets/stylesheets/syntaxhighlighter/shCoreMidnight.css +324 -0
- data/guides/assets/stylesheets/syntaxhighlighter/shCoreRDark.css +324 -0
- data/guides/assets/stylesheets/syntaxhighlighter/shThemeDefault.css +117 -0
- data/guides/assets/stylesheets/syntaxhighlighter/shThemeDjango.css +120 -0
- data/guides/assets/stylesheets/syntaxhighlighter/shThemeEclipse.css +128 -0
- data/guides/assets/stylesheets/syntaxhighlighter/shThemeEmacs.css +113 -0
- data/guides/assets/stylesheets/syntaxhighlighter/shThemeFadeToGrey.css +117 -0
- data/guides/assets/stylesheets/syntaxhighlighter/shThemeMDUltra.css +113 -0
- data/guides/assets/stylesheets/syntaxhighlighter/shThemeMidnight.css +113 -0
- data/guides/assets/stylesheets/syntaxhighlighter/shThemeRDark.css +113 -0
- data/guides/assets/stylesheets/syntaxhighlighter/shThemeRailsGuides.css +116 -0
- data/guides/code/getting_started/Gemfile +38 -0
- data/guides/code/getting_started/Gemfile.lock +150 -0
- data/guides/code/getting_started/README.rdoc +28 -0
- data/guides/code/getting_started/Rakefile +6 -0
- data/guides/code/getting_started/app/assets/images/rails.png +0 -0
- data/guides/code/getting_started/app/assets/javascripts/application.js +16 -0
- data/guides/code/getting_started/app/assets/javascripts/comments.js.coffee +3 -0
- data/guides/code/getting_started/app/assets/javascripts/posts.js.coffee +3 -0
- data/guides/code/getting_started/app/assets/javascripts/welcome.js.coffee +3 -0
- data/guides/code/getting_started/app/assets/stylesheets/application.css +13 -0
- data/guides/code/getting_started/app/assets/stylesheets/comments.css.scss +3 -0
- data/guides/code/getting_started/app/assets/stylesheets/posts.css.scss +3 -0
- data/guides/code/getting_started/app/assets/stylesheets/welcome.css.scss +3 -0
- data/guides/code/getting_started/app/controllers/application_controller.rb +5 -0
- data/guides/code/getting_started/app/controllers/comments_controller.rb +17 -0
- data/guides/code/getting_started/app/controllers/posts_controller.rb +47 -0
- data/guides/code/getting_started/app/controllers/welcome_controller.rb +4 -0
- data/guides/code/getting_started/app/helpers/application_helper.rb +2 -0
- data/guides/code/getting_started/app/helpers/comments_helper.rb +2 -0
- data/guides/code/getting_started/app/helpers/posts_helper.rb +2 -0
- data/guides/code/getting_started/app/helpers/welcome_helper.rb +2 -0
- data/guides/code/getting_started/app/models/comment.rb +3 -0
- data/guides/code/getting_started/app/models/post.rb +7 -0
- data/guides/code/getting_started/app/views/comments/_comment.html.erb +15 -0
- data/guides/code/getting_started/app/views/comments/_form.html.erb +13 -0
- data/guides/code/getting_started/app/views/layouts/application.html.erb +14 -0
- data/guides/code/getting_started/app/views/posts/_form.html.erb +27 -0
- data/guides/code/getting_started/app/views/posts/edit.html.erb +5 -0
- data/guides/code/getting_started/app/views/posts/index.html.erb +21 -0
- data/guides/code/getting_started/app/views/posts/new.html.erb +5 -0
- data/guides/code/getting_started/app/views/posts/show.html.erb +18 -0
- data/guides/code/getting_started/app/views/welcome/index.html.erb +3 -0
- data/guides/code/getting_started/bin/bundle +4 -0
- data/guides/code/getting_started/bin/rails +4 -0
- data/guides/code/getting_started/bin/rake +4 -0
- data/guides/code/getting_started/config.ru +4 -0
- data/guides/code/getting_started/config/application.rb +17 -0
- data/guides/code/getting_started/config/boot.rb +4 -0
- data/guides/code/getting_started/config/database.yml +25 -0
- data/guides/code/getting_started/config/environment.rb +5 -0
- data/guides/code/getting_started/config/environments/development.rb +30 -0
- data/guides/code/getting_started/config/environments/production.rb +80 -0
- data/guides/code/getting_started/config/environments/test.rb +36 -0
- data/guides/code/getting_started/config/initializers/backtrace_silencers.rb +7 -0
- data/guides/code/getting_started/config/initializers/filter_parameter_logging.rb +4 -0
- data/guides/code/getting_started/config/initializers/inflections.rb +16 -0
- data/guides/code/getting_started/config/initializers/locale.rb +9 -0
- data/guides/code/getting_started/config/initializers/mime_types.rb +5 -0
- data/guides/code/getting_started/config/initializers/secret_token.rb +12 -0
- data/guides/code/getting_started/config/initializers/session_store.rb +3 -0
- data/guides/code/getting_started/config/initializers/wrap_parameters.rb +14 -0
- data/guides/code/getting_started/config/locales/en.yml +23 -0
- data/guides/code/getting_started/config/routes.rb +7 -0
- data/guides/code/getting_started/db/migrate/20130122042648_create_posts.rb +10 -0
- data/guides/code/getting_started/db/migrate/20130122045842_create_comments.rb +11 -0
- data/guides/code/getting_started/db/schema.rb +33 -0
- data/guides/code/getting_started/db/seeds.rb +7 -0
- data/guides/code/getting_started/public/404.html +27 -0
- data/guides/code/getting_started/public/422.html +26 -0
- data/guides/code/getting_started/public/500.html +26 -0
- data/guides/code/getting_started/public/favicon.ico +0 -0
- data/guides/code/getting_started/public/robots.txt +5 -0
- data/guides/code/getting_started/test/controllers/comments_controller_test.rb +7 -0
- data/guides/code/getting_started/test/controllers/posts_controller_test.rb +7 -0
- data/guides/code/getting_started/test/controllers/welcome_controller_test.rb +9 -0
- data/guides/code/getting_started/test/fixtures/comments.yml +11 -0
- data/guides/code/getting_started/test/fixtures/posts.yml +9 -0
- data/guides/code/getting_started/test/helpers/comments_helper_test.rb +4 -0
- data/guides/code/getting_started/test/helpers/posts_helper_test.rb +4 -0
- data/guides/code/getting_started/test/helpers/welcome_helper_test.rb +4 -0
- data/guides/code/getting_started/test/models/comment_test.rb +7 -0
- data/guides/code/getting_started/test/models/post_test.rb +7 -0
- data/guides/code/getting_started/test/test_helper.rb +15 -0
- data/guides/rails_guides.rb +44 -0
- data/guides/rails_guides/generator.rb +248 -0
- data/guides/rails_guides/helpers.rb +51 -0
- data/guides/rails_guides/indexer.rb +68 -0
- data/guides/rails_guides/kindle.rb +119 -0
- data/guides/rails_guides/levenshtein.rb +31 -0
- data/guides/rails_guides/markdown.rb +163 -0
- data/guides/rails_guides/markdown/renderer.rb +82 -0
- data/guides/source/2_2_release_notes.md +435 -0
- data/guides/source/2_3_release_notes.md +621 -0
- data/guides/source/3_0_release_notes.md +614 -0
- data/guides/source/3_1_release_notes.md +556 -0
- data/guides/source/3_2_release_notes.md +565 -0
- data/guides/source/4_0_release_notes.md +228 -0
- data/guides/source/_license.html.erb +2 -0
- data/guides/source/_welcome.html.erb +19 -0
- data/guides/source/action_controller_overview.md +872 -0
- data/guides/source/action_mailer_basics.md +599 -0
- data/guides/source/action_view_overview.md +1565 -0
- data/guides/source/active_model_basics.md +200 -0
- data/guides/source/active_record_basics.md +370 -0
- data/guides/source/active_record_callbacks.md +358 -0
- data/guides/source/active_record_querying.md +1621 -0
- data/guides/source/active_record_validations.md +1128 -0
- data/guides/source/active_support_core_extensions.md +3791 -0
- data/guides/source/active_support_instrumentation.md +487 -0
- data/guides/source/api_documentation_guidelines.md +209 -0
- data/guides/source/asset_pipeline.md +832 -0
- data/guides/source/association_basics.md +2129 -0
- data/guides/source/caching_with_rails.md +350 -0
- data/guides/source/command_line.md +594 -0
- data/guides/source/configuring.md +736 -0
- data/guides/source/contributing_to_ruby_on_rails.md +455 -0
- data/guides/source/credits.html.erb +76 -0
- data/guides/source/debugging_rails_applications.md +675 -0
- data/guides/source/development_dependencies_install.md +195 -0
- data/guides/source/documents.yaml +179 -0
- data/guides/source/engines.md +961 -0
- data/guides/source/form_helpers.md +955 -0
- data/guides/source/generators.md +644 -0
- data/guides/source/getting_started.md +1775 -0
- data/guides/source/i18n.md +983 -0
- data/guides/source/index.html.erb +27 -0
- data/guides/source/initialization.md +562 -0
- data/guides/source/kindle/KINDLE.md +26 -0
- data/guides/source/kindle/copyright.html.erb +1 -0
- data/guides/source/kindle/layout.html.erb +27 -0
- data/guides/source/kindle/rails_guides.opf.erb +52 -0
- data/guides/source/kindle/toc.html.erb +24 -0
- data/guides/source/kindle/toc.ncx.erb +64 -0
- data/guides/source/kindle/welcome.html.erb +5 -0
- data/guides/source/layout.html.erb +148 -0
- data/guides/source/layouts_and_rendering.md +1132 -0
- data/guides/source/migrations.md +1059 -0
- data/guides/source/nested_model_forms.md +225 -0
- data/guides/source/plugins.md +435 -0
- data/guides/source/rails_application_templates.md +229 -0
- data/guides/source/rails_on_rack.md +342 -0
- data/guides/source/routing.md +1088 -0
- data/guides/source/ruby_on_rails_guides_guidelines.md +124 -0
- data/guides/source/security.md +973 -0
- data/guides/source/testing.md +981 -0
- data/guides/source/upgrading_ruby_on_rails.md +286 -0
- data/guides/source/working_with_javascript_in_rails.md +396 -0
- data/guides/w3c_validator.rb +95 -0
- metadata +315 -31
@@ -0,0 +1,286 @@
|
|
1
|
+
A Guide for Upgrading Ruby on Rails
|
2
|
+
===================================
|
3
|
+
|
4
|
+
This guide provides steps to be followed when you upgrade your applications to a newer version of Ruby on Rails. These steps are also available in individual release guides.
|
5
|
+
|
6
|
+
General Advice
|
7
|
+
--------------
|
8
|
+
|
9
|
+
Before attempting to upgrade an existing application, you should be sure you have a good reason to upgrade. You need to balance out several factors: the need for new features, the increasing difficulty of finding support for old code, and your available time and skills, to name a few.
|
10
|
+
|
11
|
+
### Test Coverage
|
12
|
+
|
13
|
+
The best way to be sure that your application still works after upgrading is to have good test coverage before you start the process. If you don't have automated tests that exercise the bulk of your application, you'll need to spend time manually exercising all the parts that have changed. In the case of a Rails upgrade, that will mean every single piece of functionality in the application. Do yourself a favor and make sure your test coverage is good _before_ you start an upgrade.
|
14
|
+
|
15
|
+
### Ruby Versions
|
16
|
+
|
17
|
+
Rails generally stays close to the latest released Ruby version when it's released:
|
18
|
+
|
19
|
+
* Rails 3 and above require Ruby 1.8.7 or higher. Support for all of the previous Ruby versions has been dropped officially. You should upgrade as early as possible.
|
20
|
+
* Rails 3.2.x is the last branch to support Ruby 1.8.7.
|
21
|
+
* Rails 4 prefers Ruby 2.0 and requires 1.9.3 or newer.
|
22
|
+
|
23
|
+
TIP: Ruby 1.8.7 p248 and p249 have marshaling bugs that crash Rails. Ruby Enterprise Edition has these fixed since the release of 1.8.7-2010.02. On the 1.9 front, Ruby 1.9.1 is not usable because it outright segfaults, so if you want to use 1.9.x, jump straight to 1.9.3 for smooth sailing.
|
24
|
+
|
25
|
+
Upgrading from Rails 3.2 to Rails 4.0
|
26
|
+
-------------------------------------
|
27
|
+
|
28
|
+
NOTE: This section is a work in progress.
|
29
|
+
|
30
|
+
If your application is currently on any version of Rails older than 3.2.x, you should upgrade to Rails 3.2 before attempting one to Rails 4.0.
|
31
|
+
|
32
|
+
The following changes are meant for upgrading your application to Rails 4.0.
|
33
|
+
|
34
|
+
### vendor/plugins
|
35
|
+
|
36
|
+
Rails 4.0 no longer supports loading plugins from `vendor/plugins`. You must replace any plugins by extracting them to gems and adding them to your Gemfile. If you choose not to make them gems, you can move them into, say, `lib/my_plugin/*` and add an appropriate initializer in `config/initializers/my_plugin.rb`.
|
37
|
+
|
38
|
+
### Active Record
|
39
|
+
|
40
|
+
* Rails 4.0 has removed the identity map from Active Record, due to [some inconsistencies with associations](https://github.com/rails/rails/commit/302c912bf6bcd0fa200d964ec2dc4a44abe328a6). If you have manually enabled it in your application, you will have to remove the following config that has no effect anymore: `config.active_record.identity_map`.
|
41
|
+
|
42
|
+
* The `delete` method in collection associations can now receive `Fixnum` or `String` arguments as record ids, besides records, pretty much like the `destroy` method does. Previously it raised `ActiveRecord::AssociationTypeMismatch` for such arguments. From Rails 4.0 on `delete` automatically tries to find the records matching the given ids before deleting them.
|
43
|
+
|
44
|
+
* Rails 4.0 has changed how orders get stacked in `ActiveRecord::Relation`. In previous versions of Rails, the new order was applied after the previously defined order. But this is no longer true. Check [Active Record Query guide](active_record_querying.html#ordering) for more information.
|
45
|
+
|
46
|
+
* Rails 4.0 has changed `serialized_attributes` and `attr_readonly` to class methods only. Now you shouldn't use instance methods, it's deprecated. You must change them, e.g. `self.serialized_attributes` to `self.class.serialized_attributes`.
|
47
|
+
|
48
|
+
* Rails 4.0 has removed `attr_accessible` and `attr_protected` feature in favor of Strong Parameters. You can use the [Protected Attributes gem](https://github.com/rails/protected_attributes) to a smoothly upgrade path.
|
49
|
+
|
50
|
+
### Active Resource
|
51
|
+
|
52
|
+
Rails 4.0 extracted Active Resource to its our gem. If you still need the feature you can add the [Active Resource gem](https://github.com/rails/activeresource) in your Gemfile.
|
53
|
+
|
54
|
+
### Active Model
|
55
|
+
|
56
|
+
* Rails 4.0 has changed how errors attach with the `ActiveModel::Validations::ConfirmationValidator`. Now when confirmation validations fail the error will be attached to `:#{attribute}_confirmation` instead of `attribute`.
|
57
|
+
|
58
|
+
* Rails 4.0 has changed `ActiveModel::Serializers::JSON.include_root_in_json` default value to `false`. Now, Active Model Serializers and Active Record objects have the same default behaviour. This means that you can comment or remove the following option in the `config/initializers/wrap_parameters.rb` file:
|
59
|
+
|
60
|
+
```ruby
|
61
|
+
# Disable root element in JSON by default.
|
62
|
+
# ActiveSupport.on_load(:active_record) do
|
63
|
+
# self.include_root_in_json = false
|
64
|
+
# end
|
65
|
+
```
|
66
|
+
|
67
|
+
### Action Pack
|
68
|
+
|
69
|
+
* There is an upgrading cookie store `UpgradeSignatureToEncryptionCookieStore` which helps you upgrading apps that use `CookieStore` to the new default `EncryptedCookieStore`. To use this `CookieStore` set `Myapp::Application.config.session_store :upgrade_signature_to_encryption_cookie_store, key: '_myapp_session'` in `config/initializers/session_store.rb`. Additionally, add `Myapp::Application.config.secret_key_base = 'some secret'` in `config/initializers/secret_token.rb`. Do not remove `Myapp::Application.config.secret_token = 'some secret'`.
|
70
|
+
|
71
|
+
* Rails 4.0 removed the `ActionController::Base.asset_path` option. Use the assets pipeline feature.
|
72
|
+
|
73
|
+
* Rails 4.0 has deprecated `ActionController::Base.page_cache_extension` option. Use `ActionController::Base.default_static_extension` instead.
|
74
|
+
|
75
|
+
* Rails 4.0 has removed Action and Page caching from Action Pack. You will need to add the `actionpack-action_caching` gem in order to use `caches_action` and the `actionpack-page_caching` to use `caches_pages` in your controllers.
|
76
|
+
|
77
|
+
* Rails 4.0 changed how `assert_generates`, `assert_recognizes`, and `assert_routing` work. Now all these assertions raise `Assertion` instead of `ActionController::RoutingError`.
|
78
|
+
|
79
|
+
* Rails 4.0 also changed the way unicode character routes are drawn. Now you can draw unicode character routes directly. If you already draw such routes, you must change them, for example:
|
80
|
+
|
81
|
+
```ruby
|
82
|
+
get Rack::Utils.escape('こんにちは'), controller: 'welcome', action: 'index'
|
83
|
+
```
|
84
|
+
|
85
|
+
becomes
|
86
|
+
|
87
|
+
```ruby
|
88
|
+
get 'こんにちは', controller: 'welcome', action: 'index'
|
89
|
+
```
|
90
|
+
|
91
|
+
* Rails 4.0 has removed ActionDispatch::BestStandardsSupport middleware, !DOCTYPE html already triggers standards mode per http://msdn.microsoft.com/en-us/library/jj676915(v=vs.85).aspx and ChromeFrame header has been moved to `config.action_dispatch.default_headers`
|
92
|
+
|
93
|
+
Remember you must also remove any references to the middleware from your application code, for example:
|
94
|
+
|
95
|
+
```ruby
|
96
|
+
# Raise exception
|
97
|
+
config.middleware.insert_before(Rack::Lock, ActionDispatch::BestStandardsSupport)
|
98
|
+
```
|
99
|
+
|
100
|
+
Also check your environment settings for `config.action_dispatch.best_standards_support` and remove it if present.
|
101
|
+
|
102
|
+
* In Rails 4.0, precompiling assets no longer automatically copies non-JS/CSS assets from `vendor/assets` and `lib/assets`. Rails application and engine developers should put these assets in `app/assets` or configure `config.assets.precompile`.
|
103
|
+
|
104
|
+
### Active Support
|
105
|
+
|
106
|
+
Rails 4.0 removes the `j` alias for `ERB::Util#json_escape` since `j` is already used for `ActionView::Helpers::JavaScriptHelper#escape_javascript`.
|
107
|
+
|
108
|
+
### Helpers Loading Order
|
109
|
+
|
110
|
+
The order in which helpers from more than one directory are loaded has changed in Rails 4.0. Previously, they were gathered and then sorted alphabetically. After upgrading to Rails 4.0, helpers will preserve the order of loaded directories and will be sorted alphabetically only within each directory. Unless you explicitly use the `helpers_path` parameter, this change will only impact the way of loading helpers from engines. If you rely on the ordering, you should check if correct methods are available after upgrade. If you would like to change the order in which engines are loaded, you can use `config.railties_order=` method.
|
111
|
+
|
112
|
+
Upgrading from Rails 3.1 to Rails 3.2
|
113
|
+
-------------------------------------
|
114
|
+
|
115
|
+
If your application is currently on any version of Rails older than 3.1.x, you should upgrade to Rails 3.1 before attempting an update to Rails 3.2.
|
116
|
+
|
117
|
+
The following changes are meant for upgrading your application to Rails 3.2.2, the latest 3.2.x version of Rails.
|
118
|
+
|
119
|
+
### Gemfile
|
120
|
+
|
121
|
+
Make the following changes to your `Gemfile`.
|
122
|
+
|
123
|
+
```ruby
|
124
|
+
gem 'rails', '= 3.2.2'
|
125
|
+
|
126
|
+
group :assets do
|
127
|
+
gem 'sass-rails', '~> 3.2.3'
|
128
|
+
gem 'coffee-rails', '~> 3.2.1'
|
129
|
+
gem 'uglifier', '>= 1.0.3'
|
130
|
+
end
|
131
|
+
```
|
132
|
+
|
133
|
+
### config/environments/development.rb
|
134
|
+
|
135
|
+
There are a couple of new configuration settings that you should add to your development environment:
|
136
|
+
|
137
|
+
```ruby
|
138
|
+
# Raise exception on mass assignment protection for Active Record models
|
139
|
+
config.active_record.mass_assignment_sanitizer = :strict
|
140
|
+
|
141
|
+
# Log the query plan for queries taking more than this (works
|
142
|
+
# with SQLite, MySQL, and PostgreSQL)
|
143
|
+
config.active_record.auto_explain_threshold_in_seconds = 0.5
|
144
|
+
```
|
145
|
+
|
146
|
+
### config/environments/test.rb
|
147
|
+
|
148
|
+
The `mass_assignment_sanitizer` configuration setting should also be be added to `config/environments/test.rb`:
|
149
|
+
|
150
|
+
```ruby
|
151
|
+
# Raise exception on mass assignment protection for Active Record models
|
152
|
+
config.active_record.mass_assignment_sanitizer = :strict
|
153
|
+
```
|
154
|
+
|
155
|
+
### vendor/plugins
|
156
|
+
|
157
|
+
Rails 3.2 deprecates `vendor/plugins` and Rails 4.0 will remove them completely. While it's not strictly necessary as part of a Rails 3.2 upgrade, you can start replacing any plugins by extracting them to gems and adding them to your Gemfile. If you choose not to make them gems, you can move them into, say, `lib/my_plugin/*` and add an appropriate initializer in `config/initializers/my_plugin.rb`.
|
158
|
+
|
159
|
+
Upgrading from Rails 3.0 to Rails 3.1
|
160
|
+
-------------------------------------
|
161
|
+
|
162
|
+
If your application is currently on any version of Rails older than 3.0.x, you should upgrade to Rails 3.0 before attempting an update to Rails 3.1.
|
163
|
+
|
164
|
+
The following changes are meant for upgrading your application to Rails 3.1.3, the latest 3.1.x version of Rails.
|
165
|
+
|
166
|
+
### Gemfile
|
167
|
+
|
168
|
+
Make the following changes to your `Gemfile`.
|
169
|
+
|
170
|
+
```ruby
|
171
|
+
gem 'rails', '= 3.1.3'
|
172
|
+
gem 'mysql2'
|
173
|
+
|
174
|
+
# Needed for the new asset pipeline
|
175
|
+
group :assets do
|
176
|
+
gem 'sass-rails', "~> 3.1.5"
|
177
|
+
gem 'coffee-rails', "~> 3.1.1"
|
178
|
+
gem 'uglifier', ">= 1.0.3"
|
179
|
+
end
|
180
|
+
|
181
|
+
# jQuery is the default JavaScript library in Rails 3.1
|
182
|
+
gem 'jquery-rails'
|
183
|
+
```
|
184
|
+
|
185
|
+
### config/application.rb
|
186
|
+
|
187
|
+
The asset pipeline requires the following additions:
|
188
|
+
|
189
|
+
```ruby
|
190
|
+
config.assets.enabled = true
|
191
|
+
config.assets.version = '1.0'
|
192
|
+
```
|
193
|
+
|
194
|
+
If your application is using an "/assets" route for a resource you may want change the prefix used for assets to avoid conflicts:
|
195
|
+
|
196
|
+
```ruby
|
197
|
+
# Defaults to '/assets'
|
198
|
+
config.assets.prefix = '/asset-files'
|
199
|
+
```
|
200
|
+
|
201
|
+
### config/environments/development.rb
|
202
|
+
|
203
|
+
Remove the RJS setting `config.action_view.debug_rjs = true`.
|
204
|
+
|
205
|
+
Add these settings if you enable the asset pipeline:
|
206
|
+
|
207
|
+
```ruby
|
208
|
+
# Do not compress assets
|
209
|
+
config.assets.compress = false
|
210
|
+
|
211
|
+
# Expands the lines which load the assets
|
212
|
+
config.assets.debug = true
|
213
|
+
```
|
214
|
+
|
215
|
+
### config/environments/production.rb
|
216
|
+
|
217
|
+
Again, most of the changes below are for the asset pipeline. You can read more about these in the [Asset Pipeline](asset_pipeline.html) guide.
|
218
|
+
|
219
|
+
```ruby
|
220
|
+
# Compress JavaScripts and CSS
|
221
|
+
config.assets.compress = true
|
222
|
+
|
223
|
+
# Don't fallback to assets pipeline if a precompiled asset is missed
|
224
|
+
config.assets.compile = false
|
225
|
+
|
226
|
+
# Generate digests for assets URLs
|
227
|
+
config.assets.digest = true
|
228
|
+
|
229
|
+
# Defaults to Rails.root.join("public/assets")
|
230
|
+
# config.assets.manifest = YOUR_PATH
|
231
|
+
|
232
|
+
# Precompile additional assets (application.js, application.css, and all non-JS/CSS are already added)
|
233
|
+
# config.assets.precompile += %w( search.js )
|
234
|
+
|
235
|
+
# Force all access to the app over SSL, use Strict-Transport-Security, and use secure cookies.
|
236
|
+
# config.force_ssl = true
|
237
|
+
```
|
238
|
+
|
239
|
+
### config/environments/test.rb
|
240
|
+
|
241
|
+
You can help test performance with these additions to your test environment:
|
242
|
+
|
243
|
+
```ruby
|
244
|
+
# Configure static asset server for tests with Cache-Control for performance
|
245
|
+
config.serve_static_assets = true
|
246
|
+
config.static_cache_control = "public, max-age=3600"
|
247
|
+
```
|
248
|
+
|
249
|
+
### config/initializers/wrap_parameters.rb
|
250
|
+
|
251
|
+
Add this file with the following contents, if you wish to wrap parameters into a nested hash. This is on by default in new applications.
|
252
|
+
|
253
|
+
```ruby
|
254
|
+
# Be sure to restart your server when you modify this file.
|
255
|
+
# This file contains settings for ActionController::ParamsWrapper which
|
256
|
+
# is enabled by default.
|
257
|
+
|
258
|
+
# Enable parameter wrapping for JSON. You can disable this by setting :format to an empty array.
|
259
|
+
ActiveSupport.on_load(:action_controller) do
|
260
|
+
wrap_parameters format: [:json]
|
261
|
+
end
|
262
|
+
|
263
|
+
# Disable root element in JSON by default.
|
264
|
+
ActiveSupport.on_load(:active_record) do
|
265
|
+
self.include_root_in_json = false
|
266
|
+
end
|
267
|
+
```
|
268
|
+
|
269
|
+
### config/initializers/session_store.rb
|
270
|
+
|
271
|
+
You need to change your session key to something new, or remove all sessions:
|
272
|
+
|
273
|
+
```ruby
|
274
|
+
# in config/initializers/session_store.rb
|
275
|
+
AppName::Application.config.session_store :cookie_store, key: 'SOMETHINGNEW'
|
276
|
+
```
|
277
|
+
|
278
|
+
or
|
279
|
+
|
280
|
+
```bash
|
281
|
+
$ rake db:sessions:clear
|
282
|
+
```
|
283
|
+
|
284
|
+
### Remove :cache and :concat options in asset helpers references in views
|
285
|
+
|
286
|
+
* With the Asset Pipeline the :cache and :concat options aren't used anymore, delete these options from your views.
|
@@ -0,0 +1,396 @@
|
|
1
|
+
Working with JavaScript in Rails
|
2
|
+
================================
|
3
|
+
|
4
|
+
This guide covers the built-in Ajax/JavaScript functionality of Rails (and
|
5
|
+
more); it will enable you to create rich and dynamic Ajax applications with
|
6
|
+
ease!
|
7
|
+
|
8
|
+
After reading this guide, you will know:
|
9
|
+
|
10
|
+
* The basics of Ajax.
|
11
|
+
* Unobtrusive JavaScript.
|
12
|
+
* How Rails' built-in helpers assist you.
|
13
|
+
* How to handle Ajax on the server side.
|
14
|
+
* The Turbolinks gem.
|
15
|
+
|
16
|
+
-------------------------------------------------------------------------------
|
17
|
+
|
18
|
+
An Introduction to Ajax
|
19
|
+
------------------------
|
20
|
+
|
21
|
+
In order to understand Ajax, you must first understand what a web browser does
|
22
|
+
normally.
|
23
|
+
|
24
|
+
When you type `http://localhost:3000` into your browser's address bar and hit
|
25
|
+
'Go,' the browser (your 'client') makes a request to the server. It parses the
|
26
|
+
response, then fetches all associated assets, like JavaScript files,
|
27
|
+
stylesheets and images. It then assembles the page. If you click a link, it
|
28
|
+
does the same process: fetch the page, fetch the assets, put it all together,
|
29
|
+
show you the results. This is called the 'request response cycle.'
|
30
|
+
|
31
|
+
JavaScript can also make requests to the server, and parse the response. It
|
32
|
+
also has the ability to update information on the page. Combining these two
|
33
|
+
powers, a JavaScript writer can make a web page that can update just parts of
|
34
|
+
itself, without needing to get the full page data from the server. This is a
|
35
|
+
powerful technique that we call Ajax.
|
36
|
+
|
37
|
+
Rails ships with CoffeeScript by default, and so the rest of the examples
|
38
|
+
in this guide will be in CoffeeScript. All of these lessons, of course, apply
|
39
|
+
to vanilla JavaScript as well.
|
40
|
+
|
41
|
+
As an example, here's some CoffeeScript code that makes an Ajax request using
|
42
|
+
the jQuery library:
|
43
|
+
|
44
|
+
```coffeescript
|
45
|
+
$.ajax(url: "/test").done (html) ->
|
46
|
+
$("#results").append html
|
47
|
+
```
|
48
|
+
|
49
|
+
This code fetches data from "/test", and then appends the result to the `div`
|
50
|
+
with an id of `results`.
|
51
|
+
|
52
|
+
Rails provides quite a bit of built-in support for building web pages with this
|
53
|
+
technique. You rarely have to write this code yourself. The rest of this guide
|
54
|
+
will show you how Rails can help you write websites in this way, but it's
|
55
|
+
all built on top of this fairly simple technique.
|
56
|
+
|
57
|
+
Unobtrusive JavaScript
|
58
|
+
-------------------------------------
|
59
|
+
|
60
|
+
Rails uses a technique called "Unobtrusive JavaScript" to handle attaching
|
61
|
+
JavaScript to the DOM. This is generally considered to be a best-practice
|
62
|
+
within the frontend community, but you may occasionally read tutorials that
|
63
|
+
demonstrate other ways.
|
64
|
+
|
65
|
+
Here's the simplest way to write JavaScript. You may see it referred to as
|
66
|
+
'inline JavaScript':
|
67
|
+
|
68
|
+
```html
|
69
|
+
<a href="#" onclick="this.style.backgroundColor='#990000'">Paint it red</a>
|
70
|
+
```
|
71
|
+
When clicked, the link background will become red. Here's the problem: what
|
72
|
+
happens when we have lots of JavaScript we want to execute on a click?
|
73
|
+
|
74
|
+
```html
|
75
|
+
<a href="#" onclick="this.style.backgroundColor='#009900';this.style.color='#FFFFFF';">Paint it green</a>
|
76
|
+
```
|
77
|
+
|
78
|
+
Awkward, right? We could pull the function definition out of the click handler,
|
79
|
+
and turn it into CoffeeScript:
|
80
|
+
|
81
|
+
```coffeescript
|
82
|
+
paintIt = (element, backgroundColor, textColor) ->
|
83
|
+
element.style.backgroundColor = backgroundColor
|
84
|
+
if textColor?
|
85
|
+
element.style.color = textColor
|
86
|
+
```
|
87
|
+
|
88
|
+
And then on our page:
|
89
|
+
|
90
|
+
```html
|
91
|
+
<a href="#" onclick="paintIt(this, '#990000')">Paint it red</a>
|
92
|
+
```
|
93
|
+
|
94
|
+
That's a little bit better, but what about multiple links that have the same
|
95
|
+
effect?
|
96
|
+
|
97
|
+
```html
|
98
|
+
<a href="#" onclick="paintIt(this, '#990000')">Paint it red</a>
|
99
|
+
<a href="#" onclick="paintIt(this, '#009900', '#FFFFFF')">Paint it green</a>
|
100
|
+
<a href="#" onclick="paintIt(this, '#000099', '#FFFFFF')">Paint it blue</a>
|
101
|
+
```
|
102
|
+
|
103
|
+
Not very DRY, eh? We can fix this by using events instead. We'll add a `data-*`
|
104
|
+
attribute to our link, and then bind a handler to the click event of every link
|
105
|
+
that has that attribute:
|
106
|
+
|
107
|
+
```coffeescript
|
108
|
+
paintIt = (element, backgroundColor, textColor) ->
|
109
|
+
element.style.backgroundColor = backgroundColor
|
110
|
+
if textColor?
|
111
|
+
element.style.color = textColor
|
112
|
+
|
113
|
+
$ ->
|
114
|
+
$("a[data-background-color]").click ->
|
115
|
+
backgroundColor = $(this).data("background-color")
|
116
|
+
textColor = $(this).data("text-color")
|
117
|
+
paintIt(this, backgroundColor, textColor)
|
118
|
+
```
|
119
|
+
```html
|
120
|
+
<a href="#" data-background-color="#990000">Paint it red</a>
|
121
|
+
<a href="#" data-background-color="#009900" data-text-color="#FFFFFF">Paint it green</a>
|
122
|
+
<a href="#" data-background-color="#000099" data-text-color="#FFFFFF">Paint it blue</a>
|
123
|
+
```
|
124
|
+
|
125
|
+
We call this 'unobtrusive' JavaScript because we're no longer mixing our
|
126
|
+
JavaScript into our HTML. We've properly separated our concerns, making future
|
127
|
+
change easy. We can easily add behavior to any link by adding the data
|
128
|
+
attribute. We can run all of our JavaScript through a minimizer and
|
129
|
+
concatenator. We can serve our entire JavaScript bundle on every page, which
|
130
|
+
means that it'll get downloaded on the first page load and then be cached on
|
131
|
+
every page after that. Lots of little benefits really add up.
|
132
|
+
|
133
|
+
The Rails team strongly encourages you to write your CoffeeScript (and
|
134
|
+
JavaScript) in this style, and you can expect that many libraries will also
|
135
|
+
follow this pattern.
|
136
|
+
|
137
|
+
Built-in Helpers
|
138
|
+
----------------------
|
139
|
+
|
140
|
+
Rails provides a bunch of view helper methods written in Ruby to assist you
|
141
|
+
in generating HTML. Sometimes, you want to add a little Ajax to those elements,
|
142
|
+
and Rails has got your back in those cases.
|
143
|
+
|
144
|
+
Because of Unobtrusive JavaScript, the Rails "Ajax helpers" are actually in two
|
145
|
+
parts: the JavaScript half and the Ruby half.
|
146
|
+
|
147
|
+
[rails.js](https://github.com/rails/jquery-ujs/blob/master/src/rails.js)
|
148
|
+
provides the JavaScript half, and the regular Ruby view helpers add appropriate
|
149
|
+
tags to your DOM. The CoffeeScript in rails.js then listens for these
|
150
|
+
attributes, and attaches appropriate handlers.
|
151
|
+
|
152
|
+
### form_for
|
153
|
+
|
154
|
+
[`form_for`](http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#method-i-form_for)
|
155
|
+
is a helper that assists with writing forms. `form_for` takes a `:remote`
|
156
|
+
option. It works like this:
|
157
|
+
|
158
|
+
```erb
|
159
|
+
<%= form_for(@post, remote: true) do |f| %>
|
160
|
+
...
|
161
|
+
<% end %>
|
162
|
+
```
|
163
|
+
|
164
|
+
This will generate the following HTML:
|
165
|
+
|
166
|
+
```html
|
167
|
+
<form accept-charset="UTF-8" action="/posts" class="new_post" data-remote="true" id="new_post" method="post">
|
168
|
+
...
|
169
|
+
</form>
|
170
|
+
```
|
171
|
+
|
172
|
+
Note the `data-remote='true'`. Now, the form will be submitted by Ajax rather
|
173
|
+
than by the browser's normal submit mechanism.
|
174
|
+
|
175
|
+
You probably don't want to just sit there with a filled out `<form>`, though.
|
176
|
+
You probably want to do something upon a successful submission. To do that,
|
177
|
+
bind to the `ajax:success` event. On failure, use `ajax:error`. Check it out:
|
178
|
+
|
179
|
+
```coffeescript
|
180
|
+
$(document).ready ->
|
181
|
+
$("#new_post").on("ajax:success", (e, data, status, xhr) ->
|
182
|
+
$("#new_post").append xhr.responseText
|
183
|
+
).bind "ajax:error", (e, xhr, status, error) ->
|
184
|
+
$("#new_post").append "<p>ERROR</p>"
|
185
|
+
```
|
186
|
+
|
187
|
+
Obviously, you'll want to be a bit more sophisticated than that, but it's a
|
188
|
+
start.
|
189
|
+
|
190
|
+
### form_tag
|
191
|
+
|
192
|
+
[`form_tag`](http://api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html#method-i-form_tag)
|
193
|
+
is very similar to `form_for`. It has a `:remote` option that you can use like
|
194
|
+
this:
|
195
|
+
|
196
|
+
```erb
|
197
|
+
<%= form_tag('/posts', remote: true) %>
|
198
|
+
```
|
199
|
+
|
200
|
+
Everything else is the same as `form_for`. See its documentation for full
|
201
|
+
details.
|
202
|
+
|
203
|
+
### link_to
|
204
|
+
|
205
|
+
[`link_to`](http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-link_to)
|
206
|
+
is a helper that assists with generating links. It has a `:remote` option you
|
207
|
+
can use like this:
|
208
|
+
|
209
|
+
```erb
|
210
|
+
<%= link_to "a post", @post, remote: true %>
|
211
|
+
```
|
212
|
+
|
213
|
+
which generates
|
214
|
+
|
215
|
+
```html
|
216
|
+
<a href="/posts/1" data-remote="true">a post</a>
|
217
|
+
```
|
218
|
+
|
219
|
+
You can bind to the same Ajax events as `form_for`. Here's an example. Let's
|
220
|
+
assume that we have a list of posts that can be deleted with just one
|
221
|
+
click. We would generate some HTML like this:
|
222
|
+
|
223
|
+
```erb
|
224
|
+
<%= link_to "Delete post", @post, remote: true, method: :delete %>
|
225
|
+
```
|
226
|
+
|
227
|
+
and write some CoffeeScript like this:
|
228
|
+
|
229
|
+
```coffeescript
|
230
|
+
$ ->
|
231
|
+
$("a[data-remote]").on "ajax:success", (e, data, status, xhr) ->
|
232
|
+
alert "The post was deleted."
|
233
|
+
```
|
234
|
+
|
235
|
+
### button_to
|
236
|
+
|
237
|
+
[`button_to`](http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-button_to) is a helper that helps you create buttons. It has a `:remote` option that you can call like this:
|
238
|
+
|
239
|
+
```erb
|
240
|
+
<%= button_to "A post", @post, remote: true %>
|
241
|
+
```
|
242
|
+
|
243
|
+
this generates
|
244
|
+
|
245
|
+
```html
|
246
|
+
<form action="/posts/1" class="button_to" data-remote="true" method="post">
|
247
|
+
<div><input type="submit" value="A post"></div>
|
248
|
+
</form>
|
249
|
+
```
|
250
|
+
|
251
|
+
Since it's just a `<form>`, all of the information on `form_for` also applies.
|
252
|
+
|
253
|
+
Server-Side Concerns
|
254
|
+
--------------------
|
255
|
+
|
256
|
+
Ajax isn't just client-side, you also need to do some work on the server
|
257
|
+
side to support it. Often, people like their Ajax requests to return JSON
|
258
|
+
rather than HTML. Let's discuss what it takes to make that happen.
|
259
|
+
|
260
|
+
### A Simple Example
|
261
|
+
|
262
|
+
Imagine you have a series of users that you would like to display and provide a
|
263
|
+
form on that same page to create a new user. The index action of your
|
264
|
+
controller looks like this:
|
265
|
+
|
266
|
+
```ruby
|
267
|
+
class UsersController < ApplicationController
|
268
|
+
def index
|
269
|
+
@users = User.all
|
270
|
+
@user = User.new
|
271
|
+
end
|
272
|
+
# ...
|
273
|
+
```
|
274
|
+
|
275
|
+
The index view (`app/views/users/index.html.erb`) contains:
|
276
|
+
|
277
|
+
```erb
|
278
|
+
<b>Users</b>
|
279
|
+
|
280
|
+
<ul id="users">
|
281
|
+
<% @users.each do |user| %>
|
282
|
+
<%= render user %>
|
283
|
+
<% end %>
|
284
|
+
</ul>
|
285
|
+
|
286
|
+
<br>
|
287
|
+
|
288
|
+
<%= form_for(@user, remote: true) do |f| %>
|
289
|
+
<%= f.label :name %><br>
|
290
|
+
<%= f.text_field :name %>
|
291
|
+
<%= f.submit %>
|
292
|
+
<% end %>
|
293
|
+
```
|
294
|
+
|
295
|
+
The `app/views/users/_user.html.erb` partial contains the following:
|
296
|
+
|
297
|
+
```erb
|
298
|
+
<li><%= user.name %></li>
|
299
|
+
```
|
300
|
+
|
301
|
+
The top portion of the index page displays the users. The bottom portion
|
302
|
+
provides a form to create a new user.
|
303
|
+
|
304
|
+
The bottom form will call the create action on the Users controller. Because
|
305
|
+
the form's remote option is set to true, the request will be posted to the
|
306
|
+
users controller as an Ajax request, looking for JavaScript. In order to
|
307
|
+
service that request, the create action of your controller would look like
|
308
|
+
this:
|
309
|
+
|
310
|
+
```ruby
|
311
|
+
# app/controllers/users_controller.rb
|
312
|
+
# ......
|
313
|
+
def create
|
314
|
+
@user = User.new(params[:user])
|
315
|
+
|
316
|
+
respond_to do |format|
|
317
|
+
if @user.save
|
318
|
+
format.html { redirect_to @user, notice: 'User was successfully created.' }
|
319
|
+
format.js {}
|
320
|
+
format.json { render json: @user, status: :created, location: @user }
|
321
|
+
else
|
322
|
+
format.html { render action: "new" }
|
323
|
+
format.json { render json: @user.errors, status: :unprocessable_entity }
|
324
|
+
end
|
325
|
+
end
|
326
|
+
end
|
327
|
+
```
|
328
|
+
|
329
|
+
Notice the format.js in the `respond_to` block; that allows the controller to
|
330
|
+
respond to your Ajax request. You then have a corresponding
|
331
|
+
`app/views/users/create.js.erb` view file that generates the actual JavaScript
|
332
|
+
code that will be sent and executed on the client side.
|
333
|
+
|
334
|
+
```erb
|
335
|
+
$("<%= escape_javascript(render @user) %>").appendTo("#users");
|
336
|
+
```
|
337
|
+
|
338
|
+
Turbolinks
|
339
|
+
----------
|
340
|
+
|
341
|
+
Rails 4 ships with the [Turbolinks gem](https://github.com/rails/turbolinks).
|
342
|
+
This gem uses Ajax to speed up page rendering in most applications.
|
343
|
+
|
344
|
+
### How Turbolinks Works
|
345
|
+
|
346
|
+
Turbolinks attaches a click handler to all `<a>` on the page. If your browser
|
347
|
+
supports
|
348
|
+
[PushState](https://developer.mozilla.org/en-US/docs/DOM/Manipulating_the_browser_history#The_pushState(\).C2.A0method),
|
349
|
+
Turbolinks will make an Ajax request for the page, parse the response, and
|
350
|
+
replace the entire `<body>` of the page with the `<body>` of the response. It
|
351
|
+
will then use PushState to change the URL to the correct one, preserving
|
352
|
+
refresh semantics and giving you pretty URLs.
|
353
|
+
|
354
|
+
The only thing you have to do to enable Turbolinks is have it in your Gemfile,
|
355
|
+
and put `//= require turbolinks` in your CoffeeScript manifest, which is usually
|
356
|
+
`app/assets/javascripts/application.js`.
|
357
|
+
|
358
|
+
If you want to disable Turbolinks for certain links, add a `data-no-turbolink`
|
359
|
+
attribute to the tag:
|
360
|
+
|
361
|
+
```html
|
362
|
+
<a href="..." data-no-turbolink>No turbolinks here</a>.
|
363
|
+
```
|
364
|
+
|
365
|
+
### Page Change Events
|
366
|
+
|
367
|
+
When writing CoffeeScript, you'll often want to do some sort of processing upon
|
368
|
+
page load. With jQuery, you'd write something like this:
|
369
|
+
|
370
|
+
```coffeescript
|
371
|
+
$(document).ready ->
|
372
|
+
alert "page has loaded!"
|
373
|
+
```
|
374
|
+
|
375
|
+
However, because Turbolinks overrides the normal page loading process, the
|
376
|
+
event that this relies on will not be fired. If you have code that looks like
|
377
|
+
this, you must change your code to do this instead:
|
378
|
+
|
379
|
+
```coffeescript
|
380
|
+
$(document).on "page:change", ->
|
381
|
+
alert "page has loaded!"
|
382
|
+
```
|
383
|
+
|
384
|
+
For more details, including other events you can bind to, check out [the
|
385
|
+
Turbolinks
|
386
|
+
README](https://github.com/rails/turbolinks/blob/master/README.md).
|
387
|
+
|
388
|
+
Other Resources
|
389
|
+
---------------
|
390
|
+
|
391
|
+
Here are some helpful links to help you learn even more:
|
392
|
+
|
393
|
+
* [jquery-ujs wiki](https://github.com/rails/jquery-ujs/wiki)
|
394
|
+
* [jquery-ujs list of external articles](https://github.com/rails/jquery-ujs/wiki/External-articles)
|
395
|
+
* [Rails 3 Remote Links and Forms: A Definitive Guide](http://www.alfajango.com/blog/rails-3-remote-links-and-forms/)
|
396
|
+
* [Railscasts: Unobtrusive JavaScript](http://railscasts.com/episodes/205-unobtrusive-javascript)
|