rails 4.1.14.2 → 5.2.4.4
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 +5 -5
- data/README.md +33 -19
- metadata +65 -313
- data/guides/CHANGELOG.md +0 -106
- data/guides/Rakefile +0 -77
- data/guides/assets/images/akshaysurve.jpg +0 -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/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/article_with_comments.png +0 -0
- data/guides/assets/images/getting_started/challenge.png +0 -0
- data/guides/assets/images/getting_started/confirm_dialog.png +0 -0
- data/guides/assets/images/getting_started/forbidden_attributes_for_new_article.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_article.png +0 -0
- data/guides/assets/images/getting_started/rails_welcome.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_articles.png +0 -0
- data/guides/assets/images/getting_started/template_is_missing_articles_new.png +0 -0
- data/guides/assets/images/getting_started/unknown_action_create_for_articles.png +0 -0
- data/guides/assets/images/getting_started/unknown_action_new_for_articles.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/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/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 +0 -53
- data/guides/assets/javascripts/jquery.min.js +0 -4
- data/guides/assets/javascripts/responsive-tables.js +0 -43
- 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 -710
- data/guides/assets/stylesheets/print.css +0 -52
- data/guides/assets/stylesheets/reset.css +0 -43
- data/guides/assets/stylesheets/responsive-tables.css +0 -50
- 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/bug_report_templates/action_controller_gem.rb +0 -47
- data/guides/bug_report_templates/action_controller_master.rb +0 -53
- data/guides/bug_report_templates/active_record_gem.rb +0 -40
- data/guides/bug_report_templates/active_record_master.rb +0 -49
- data/guides/bug_report_templates/generic_gem.rb +0 -15
- data/guides/bug_report_templates/generic_master.rb +0 -26
- data/guides/code/getting_started/Gemfile +0 -40
- data/guides/code/getting_started/Gemfile.lock +0 -125
- data/guides/code/getting_started/README.rdoc +0 -28
- data/guides/code/getting_started/Rakefile +0 -6
- 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/posts.js.coffee +0 -3
- data/guides/code/getting_started/app/assets/javascripts/welcome.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/posts.css.scss +0 -3
- data/guides/code/getting_started/app/assets/stylesheets/welcome.css.scss +0 -3
- data/guides/code/getting_started/app/controllers/application_controller.rb +0 -5
- data/guides/code/getting_started/app/controllers/comments_controller.rb +0 -23
- data/guides/code/getting_started/app/controllers/posts_controller.rb +0 -53
- data/guides/code/getting_started/app/controllers/welcome_controller.rb +0 -4
- 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/posts_helper.rb +0 -2
- data/guides/code/getting_started/app/helpers/welcome_helper.rb +0 -2
- data/guides/code/getting_started/app/models/comment.rb +0 -3
- data/guides/code/getting_started/app/models/post.rb +0 -7
- 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/layouts/application.html.erb +0 -14
- data/guides/code/getting_started/app/views/posts/_form.html.erb +0 -27
- data/guides/code/getting_started/app/views/posts/edit.html.erb +0 -5
- data/guides/code/getting_started/app/views/posts/index.html.erb +0 -21
- 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 -18
- data/guides/code/getting_started/app/views/welcome/index.html.erb +0 -4
- data/guides/code/getting_started/bin/bundle +0 -4
- data/guides/code/getting_started/bin/rails +0 -4
- data/guides/code/getting_started/bin/rake +0 -4
- data/guides/code/getting_started/config.ru +0 -4
- data/guides/code/getting_started/config/application.rb +0 -18
- data/guides/code/getting_started/config/boot.rb +0 -4
- 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 -30
- data/guides/code/getting_started/config/environments/production.rb +0 -80
- data/guides/code/getting_started/config/environments/test.rb +0 -36
- data/guides/code/getting_started/config/initializers/backtrace_silencers.rb +0 -7
- data/guides/code/getting_started/config/initializers/filter_parameter_logging.rb +0 -4
- data/guides/code/getting_started/config/initializers/inflections.rb +0 -16
- data/guides/code/getting_started/config/initializers/locale.rb +0 -9
- data/guides/code/getting_started/config/initializers/mime_types.rb +0 -5
- data/guides/code/getting_started/config/initializers/secret_token.rb +0 -12
- data/guides/code/getting_started/config/initializers/session_store.rb +0 -3
- data/guides/code/getting_started/config/initializers/wrap_parameters.rb +0 -14
- data/guides/code/getting_started/config/locales/en.yml +0 -23
- data/guides/code/getting_started/config/routes.rb +0 -7
- data/guides/code/getting_started/db/migrate/20130122042648_create_posts.rb +0 -10
- data/guides/code/getting_started/db/migrate/20130122045842_create_comments.rb +0 -11
- data/guides/code/getting_started/db/schema.rb +0 -33
- data/guides/code/getting_started/db/seeds.rb +0 -7
- data/guides/code/getting_started/public/404.html +0 -60
- data/guides/code/getting_started/public/422.html +0 -60
- data/guides/code/getting_started/public/500.html +0 -59
- 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/test/controllers/comments_controller_test.rb +0 -7
- data/guides/code/getting_started/test/controllers/posts_controller_test.rb +0 -7
- data/guides/code/getting_started/test/controllers/welcome_controller_test.rb +0 -9
- data/guides/code/getting_started/test/fixtures/comments.yml +0 -11
- data/guides/code/getting_started/test/fixtures/posts.yml +0 -9
- data/guides/code/getting_started/test/helpers/comments_helper_test.rb +0 -4
- data/guides/code/getting_started/test/helpers/posts_helper_test.rb +0 -4
- data/guides/code/getting_started/test/helpers/welcome_helper_test.rb +0 -4
- data/guides/code/getting_started/test/models/comment_test.rb +0 -7
- data/guides/code/getting_started/test/models/post_test.rb +0 -7
- data/guides/code/getting_started/test/test_helper.rb +0 -12
- data/guides/rails_guides.rb +0 -63
- data/guides/rails_guides/generator.rb +0 -248
- data/guides/rails_guides/helpers.rb +0 -53
- data/guides/rails_guides/indexer.rb +0 -68
- data/guides/rails_guides/kindle.rb +0 -119
- data/guides/rails_guides/levenshtein.rb +0 -31
- data/guides/rails_guides/markdown.rb +0 -163
- data/guides/rails_guides/markdown/renderer.rb +0 -82
- data/guides/source/2_2_release_notes.md +0 -435
- data/guides/source/2_3_release_notes.md +0 -621
- data/guides/source/3_0_release_notes.md +0 -611
- data/guides/source/3_1_release_notes.md +0 -556
- data/guides/source/3_2_release_notes.md +0 -565
- data/guides/source/4_0_release_notes.md +0 -276
- data/guides/source/4_1_release_notes.md +0 -731
- data/guides/source/_license.html.erb +0 -2
- data/guides/source/_welcome.html.erb +0 -25
- data/guides/source/action_controller_overview.md +0 -1175
- data/guides/source/action_mailer_basics.md +0 -689
- data/guides/source/action_view_overview.md +0 -1602
- data/guides/source/active_model_basics.md +0 -200
- data/guides/source/active_record_basics.md +0 -373
- data/guides/source/active_record_callbacks.md +0 -410
- data/guides/source/active_record_querying.md +0 -1785
- data/guides/source/active_record_validations.md +0 -1169
- data/guides/source/active_support_core_extensions.md +0 -3817
- data/guides/source/active_support_instrumentation.md +0 -496
- data/guides/source/api_documentation_guidelines.md +0 -315
- data/guides/source/asset_pipeline.md +0 -1165
- data/guides/source/association_basics.md +0 -2229
- data/guides/source/caching_with_rails.md +0 -354
- data/guides/source/command_line.md +0 -603
- data/guides/source/configuring.md +0 -942
- data/guides/source/contributing_to_ruby_on_rails.md +0 -544
- data/guides/source/credits.html.erb +0 -80
- data/guides/source/debugging_rails_applications.md +0 -707
- data/guides/source/development_dependencies_install.md +0 -278
- data/guides/source/documents.yaml +0 -193
- data/guides/source/engines.md +0 -1412
- data/guides/source/form_helpers.md +0 -1001
- data/guides/source/generators.md +0 -663
- data/guides/source/getting_started.md +0 -1946
- data/guides/source/i18n.md +0 -1043
- data/guides/source/index.html.erb +0 -27
- data/guides/source/initialization.md +0 -657
- 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 -146
- data/guides/source/layouts_and_rendering.md +0 -1226
- data/guides/source/maintenance_policy.md +0 -75
- data/guides/source/migrations.md +0 -1143
- data/guides/source/nested_model_forms.md +0 -225
- data/guides/source/plugins.md +0 -444
- data/guides/source/rails_application_templates.md +0 -248
- data/guides/source/rails_on_rack.md +0 -332
- data/guides/source/routing.md +0 -1115
- data/guides/source/ruby_on_rails_guides_guidelines.md +0 -128
- data/guides/source/security.md +0 -1018
- data/guides/source/testing.md +0 -1052
- data/guides/source/upgrading_ruby_on_rails.md +0 -928
- data/guides/source/working_with_javascript_in_rails.md +0 -405
- data/guides/w3c_validator.rb +0 -95
@@ -1,405 +0,0 @@
|
|
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
|
-
).on "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. You can see more about the events [in the jquery-ujs wiki](https://github.com/rails/jquery-ujs/wiki/ajax).
|
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) do %>
|
198
|
-
...
|
199
|
-
<% end %>
|
200
|
-
```
|
201
|
-
|
202
|
-
This will generate the following HTML:
|
203
|
-
|
204
|
-
```html
|
205
|
-
<form accept-charset="UTF-8" action="/posts" data-remote="true" method="post">
|
206
|
-
...
|
207
|
-
</form>
|
208
|
-
```
|
209
|
-
|
210
|
-
Everything else is the same as `form_for`. See its documentation for full
|
211
|
-
details.
|
212
|
-
|
213
|
-
### link_to
|
214
|
-
|
215
|
-
[`link_to`](http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-link_to)
|
216
|
-
is a helper that assists with generating links. It has a `:remote` option you
|
217
|
-
can use like this:
|
218
|
-
|
219
|
-
```erb
|
220
|
-
<%= link_to "a post", @post, remote: true %>
|
221
|
-
```
|
222
|
-
|
223
|
-
which generates
|
224
|
-
|
225
|
-
```html
|
226
|
-
<a href="/posts/1" data-remote="true">a post</a>
|
227
|
-
```
|
228
|
-
|
229
|
-
You can bind to the same Ajax events as `form_for`. Here's an example. Let's
|
230
|
-
assume that we have a list of posts that can be deleted with just one
|
231
|
-
click. We would generate some HTML like this:
|
232
|
-
|
233
|
-
```erb
|
234
|
-
<%= link_to "Delete post", @post, remote: true, method: :delete %>
|
235
|
-
```
|
236
|
-
|
237
|
-
and write some CoffeeScript like this:
|
238
|
-
|
239
|
-
```coffeescript
|
240
|
-
$ ->
|
241
|
-
$("a[data-remote]").on "ajax:success", (e, data, status, xhr) ->
|
242
|
-
alert "The post was deleted."
|
243
|
-
```
|
244
|
-
|
245
|
-
### button_to
|
246
|
-
|
247
|
-
[`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:
|
248
|
-
|
249
|
-
```erb
|
250
|
-
<%= button_to "A post", @post, remote: true %>
|
251
|
-
```
|
252
|
-
|
253
|
-
this generates
|
254
|
-
|
255
|
-
```html
|
256
|
-
<form action="/posts/1" class="button_to" data-remote="true" method="post">
|
257
|
-
<div><input type="submit" value="A post"></div>
|
258
|
-
</form>
|
259
|
-
```
|
260
|
-
|
261
|
-
Since it's just a `<form>`, all of the information on `form_for` also applies.
|
262
|
-
|
263
|
-
Server-Side Concerns
|
264
|
-
--------------------
|
265
|
-
|
266
|
-
Ajax isn't just client-side, you also need to do some work on the server
|
267
|
-
side to support it. Often, people like their Ajax requests to return JSON
|
268
|
-
rather than HTML. Let's discuss what it takes to make that happen.
|
269
|
-
|
270
|
-
### A Simple Example
|
271
|
-
|
272
|
-
Imagine you have a series of users that you would like to display and provide a
|
273
|
-
form on that same page to create a new user. The index action of your
|
274
|
-
controller looks like this:
|
275
|
-
|
276
|
-
```ruby
|
277
|
-
class UsersController < ApplicationController
|
278
|
-
def index
|
279
|
-
@users = User.all
|
280
|
-
@user = User.new
|
281
|
-
end
|
282
|
-
# ...
|
283
|
-
```
|
284
|
-
|
285
|
-
The index view (`app/views/users/index.html.erb`) contains:
|
286
|
-
|
287
|
-
```erb
|
288
|
-
<b>Users</b>
|
289
|
-
|
290
|
-
<ul id="users">
|
291
|
-
<%= render @users %>
|
292
|
-
</ul>
|
293
|
-
|
294
|
-
<br>
|
295
|
-
|
296
|
-
<%= form_for(@user, remote: true) do |f| %>
|
297
|
-
<%= f.label :name %><br>
|
298
|
-
<%= f.text_field :name %>
|
299
|
-
<%= f.submit %>
|
300
|
-
<% end %>
|
301
|
-
```
|
302
|
-
|
303
|
-
The `app/views/users/_user.html.erb` partial contains the following:
|
304
|
-
|
305
|
-
```erb
|
306
|
-
<li><%= user.name %></li>
|
307
|
-
```
|
308
|
-
|
309
|
-
The top portion of the index page displays the users. The bottom portion
|
310
|
-
provides a form to create a new user.
|
311
|
-
|
312
|
-
The bottom form will call the `create` action on the `UsersController`. Because
|
313
|
-
the form's remote option is set to true, the request will be posted to the
|
314
|
-
`UsersController` as an Ajax request, looking for JavaScript. In order to
|
315
|
-
serve that request, the `create` action of your controller would look like
|
316
|
-
this:
|
317
|
-
|
318
|
-
```ruby
|
319
|
-
# app/controllers/users_controller.rb
|
320
|
-
# ......
|
321
|
-
def create
|
322
|
-
@user = User.new(params[:user])
|
323
|
-
|
324
|
-
respond_to do |format|
|
325
|
-
if @user.save
|
326
|
-
format.html { redirect_to @user, notice: 'User was successfully created.' }
|
327
|
-
format.js {}
|
328
|
-
format.json { render json: @user, status: :created, location: @user }
|
329
|
-
else
|
330
|
-
format.html { render action: "new" }
|
331
|
-
format.json { render json: @user.errors, status: :unprocessable_entity }
|
332
|
-
end
|
333
|
-
end
|
334
|
-
end
|
335
|
-
```
|
336
|
-
|
337
|
-
Notice the format.js in the `respond_to` block; that allows the controller to
|
338
|
-
respond to your Ajax request. You then have a corresponding
|
339
|
-
`app/views/users/create.js.erb` view file that generates the actual JavaScript
|
340
|
-
code that will be sent and executed on the client side.
|
341
|
-
|
342
|
-
```erb
|
343
|
-
$("<%= escape_javascript(render @user) %>").appendTo("#users");
|
344
|
-
```
|
345
|
-
|
346
|
-
Turbolinks
|
347
|
-
----------
|
348
|
-
|
349
|
-
Rails 4 ships with the [Turbolinks gem](https://github.com/rails/turbolinks).
|
350
|
-
This gem uses Ajax to speed up page rendering in most applications.
|
351
|
-
|
352
|
-
### How Turbolinks Works
|
353
|
-
|
354
|
-
Turbolinks attaches a click handler to all `<a>` on the page. If your browser
|
355
|
-
supports
|
356
|
-
[PushState](https://developer.mozilla.org/en-US/docs/DOM/Manipulating_the_browser_history#The_pushState(\).C2.A0method),
|
357
|
-
Turbolinks will make an Ajax request for the page, parse the response, and
|
358
|
-
replace the entire `<body>` of the page with the `<body>` of the response. It
|
359
|
-
will then use PushState to change the URL to the correct one, preserving
|
360
|
-
refresh semantics and giving you pretty URLs.
|
361
|
-
|
362
|
-
The only thing you have to do to enable Turbolinks is have it in your Gemfile,
|
363
|
-
and put `//= require turbolinks` in your CoffeeScript manifest, which is usually
|
364
|
-
`app/assets/javascripts/application.js`.
|
365
|
-
|
366
|
-
If you want to disable Turbolinks for certain links, add a `data-no-turbolink`
|
367
|
-
attribute to the tag:
|
368
|
-
|
369
|
-
```html
|
370
|
-
<a href="..." data-no-turbolink>No turbolinks here</a>.
|
371
|
-
```
|
372
|
-
|
373
|
-
### Page Change Events
|
374
|
-
|
375
|
-
When writing CoffeeScript, you'll often want to do some sort of processing upon
|
376
|
-
page load. With jQuery, you'd write something like this:
|
377
|
-
|
378
|
-
```coffeescript
|
379
|
-
$(document).ready ->
|
380
|
-
alert "page has loaded!"
|
381
|
-
```
|
382
|
-
|
383
|
-
However, because Turbolinks overrides the normal page loading process, the
|
384
|
-
event that this relies on will not be fired. If you have code that looks like
|
385
|
-
this, you must change your code to do this instead:
|
386
|
-
|
387
|
-
```coffeescript
|
388
|
-
$(document).on "page:change", ->
|
389
|
-
alert "page has loaded!"
|
390
|
-
```
|
391
|
-
|
392
|
-
For more details, including other events you can bind to, check out [the
|
393
|
-
Turbolinks
|
394
|
-
README](https://github.com/rails/turbolinks/blob/master/README.md).
|
395
|
-
|
396
|
-
Other Resources
|
397
|
-
---------------
|
398
|
-
|
399
|
-
Here are some helpful links to help you learn even more:
|
400
|
-
|
401
|
-
* [jquery-ujs wiki](https://github.com/rails/jquery-ujs/wiki)
|
402
|
-
* [jquery-ujs list of external articles](https://github.com/rails/jquery-ujs/wiki/External-articles)
|
403
|
-
* [Rails 3 Remote Links and Forms: A Definitive Guide](http://www.alfajango.com/blog/rails-3-remote-links-and-forms/)
|
404
|
-
* [Railscasts: Unobtrusive JavaScript](http://railscasts.com/episodes/205-unobtrusive-javascript)
|
405
|
-
* [Railscasts: Turbolinks](http://railscasts.com/episodes/390-turbolinks)
|
data/guides/w3c_validator.rb
DELETED
@@ -1,95 +0,0 @@
|
|
1
|
-
# ---------------------------------------------------------------------------
|
2
|
-
#
|
3
|
-
# This script validates the generated guides against the W3C Validator.
|
4
|
-
#
|
5
|
-
# Guides are taken from the output directory, from where all .html files are
|
6
|
-
# submitted to the validator.
|
7
|
-
#
|
8
|
-
# This script is prepared to be launched from the guides directory as a rake task:
|
9
|
-
#
|
10
|
-
# rake guides:validate
|
11
|
-
#
|
12
|
-
# If nothing is specified, all files will be validated, but you can check just
|
13
|
-
# some of them using this environment variable:
|
14
|
-
#
|
15
|
-
# ONLY
|
16
|
-
# Use ONLY if you want to validate only one or a set of guides. Prefixes are
|
17
|
-
# enough:
|
18
|
-
#
|
19
|
-
# # validates only association_basics.html
|
20
|
-
# rake guides:validate ONLY=assoc
|
21
|
-
#
|
22
|
-
# Separate many using commas:
|
23
|
-
#
|
24
|
-
# # validates only association_basics.html and migrations.html
|
25
|
-
# rake guides:validate ONLY=assoc,migrations
|
26
|
-
#
|
27
|
-
# ---------------------------------------------------------------------------
|
28
|
-
|
29
|
-
require 'w3c_validators'
|
30
|
-
include W3CValidators
|
31
|
-
|
32
|
-
module RailsGuides
|
33
|
-
class Validator
|
34
|
-
|
35
|
-
def validate
|
36
|
-
validator = MarkupValidator.new
|
37
|
-
STDOUT.sync = true
|
38
|
-
errors_on_guides = {}
|
39
|
-
|
40
|
-
guides_to_validate.each do |f|
|
41
|
-
begin
|
42
|
-
results = validator.validate_file(f)
|
43
|
-
rescue Exception => e
|
44
|
-
puts "\nCould not validate #{f} because of #{e}"
|
45
|
-
next
|
46
|
-
end
|
47
|
-
|
48
|
-
if results.validity
|
49
|
-
print "."
|
50
|
-
else
|
51
|
-
print "E"
|
52
|
-
errors_on_guides[f] = results.errors
|
53
|
-
end
|
54
|
-
end
|
55
|
-
|
56
|
-
show_results(errors_on_guides)
|
57
|
-
end
|
58
|
-
|
59
|
-
private
|
60
|
-
def guides_to_validate
|
61
|
-
guides = Dir["./output/*.html"]
|
62
|
-
guides.delete("./output/layout.html")
|
63
|
-
ENV.key?('ONLY') ? select_only(guides) : guides
|
64
|
-
end
|
65
|
-
|
66
|
-
def select_only(guides)
|
67
|
-
prefixes = ENV['ONLY'].split(",").map(&:strip)
|
68
|
-
guides.select do |guide|
|
69
|
-
prefixes.any? {|p| guide.start_with?("./output/#{p}")}
|
70
|
-
end
|
71
|
-
end
|
72
|
-
|
73
|
-
def show_results(error_list)
|
74
|
-
if error_list.size == 0
|
75
|
-
puts "\n\nAll checked guides validate OK!"
|
76
|
-
else
|
77
|
-
error_summary = error_detail = ""
|
78
|
-
|
79
|
-
error_list.each_pair do |name, errors|
|
80
|
-
error_summary += "\n #{name}"
|
81
|
-
error_detail += "\n\n #{name} has #{errors.size} validation error(s):\n"
|
82
|
-
errors.each do |error|
|
83
|
-
error_detail += "\n "+error.to_s.delete("\n")
|
84
|
-
end
|
85
|
-
end
|
86
|
-
|
87
|
-
puts "\n\nThere are #{error_list.size} guides with validation errors:\n" + error_summary
|
88
|
-
puts "\nHere are the detailed errors for each guide:" + error_detail
|
89
|
-
end
|
90
|
-
end
|
91
|
-
|
92
|
-
end
|
93
|
-
end
|
94
|
-
|
95
|
-
RailsGuides::Validator.new.validate
|