rails 4.2.6.rc1 → 5.2.8.1
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 +24 -12
- metadata +54 -248
- data/guides/CHANGELOG.md +0 -73
- data/guides/Rakefile +0 -79
- 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 -59
- 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 -713
- 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 -54
- 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/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 -37
- data/guides/rails_guides/markdown/renderer.rb +0 -82
- data/guides/rails_guides/markdown.rb +0 -167
- data/guides/rails_guides.rb +0 -63
- 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 -559
- data/guides/source/3_2_release_notes.md +0 -568
- data/guides/source/4_0_release_notes.md +0 -279
- data/guides/source/4_1_release_notes.md +0 -730
- data/guides/source/4_2_release_notes.md +0 -877
- data/guides/source/_license.html.erb +0 -2
- data/guides/source/_welcome.html.erb +0 -23
- data/guides/source/action_controller_overview.md +0 -1192
- data/guides/source/action_mailer_basics.md +0 -757
- data/guides/source/action_view_overview.md +0 -1561
- data/guides/source/active_job_basics.md +0 -339
- data/guides/source/active_model_basics.md +0 -554
- data/guides/source/active_record_basics.md +0 -374
- data/guides/source/active_record_callbacks.md +0 -413
- data/guides/source/active_record_migrations.md +0 -1018
- data/guides/source/active_record_postgresql.md +0 -433
- data/guides/source/active_record_querying.md +0 -1781
- data/guides/source/active_record_validations.md +0 -1179
- data/guides/source/active_support_core_extensions.md +0 -3857
- data/guides/source/active_support_instrumentation.md +0 -488
- data/guides/source/api_documentation_guidelines.md +0 -361
- data/guides/source/asset_pipeline.md +0 -1304
- data/guides/source/association_basics.md +0 -2245
- data/guides/source/autoloading_and_reloading_constants.md +0 -1311
- data/guides/source/caching_with_rails.md +0 -379
- data/guides/source/command_line.md +0 -625
- data/guides/source/configuring.md +0 -1072
- data/guides/source/contributing_to_ruby_on_rails.md +0 -628
- data/guides/source/credits.html.erb +0 -80
- data/guides/source/debugging_rails_applications.md +0 -861
- data/guides/source/development_dependencies_install.md +0 -289
- data/guides/source/documents.yaml +0 -205
- data/guides/source/engines.md +0 -1412
- data/guides/source/form_helpers.md +0 -1024
- data/guides/source/generators.md +0 -676
- data/guides/source/getting_started.md +0 -2086
- data/guides/source/i18n.md +0 -1087
- data/guides/source/index.html.erb +0 -28
- data/guides/source/initialization.md +0 -704
- 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 -140
- data/guides/source/layouts_and_rendering.md +0 -1227
- data/guides/source/maintenance_policy.md +0 -78
- data/guides/source/nested_model_forms.md +0 -228
- data/guides/source/plugins.md +0 -444
- data/guides/source/rails_application_templates.md +0 -266
- data/guides/source/rails_on_rack.md +0 -336
- data/guides/source/routing.md +0 -1155
- data/guides/source/ruby_on_rails_guides_guidelines.md +0 -127
- data/guides/source/security.md +0 -1024
- data/guides/source/testing.md +0 -1132
- data/guides/source/upgrading_ruby_on_rails.md +0 -1186
- data/guides/source/working_with_javascript_in_rails.md +0 -407
- data/guides/w3c_validator.rb +0 -97
@@ -1,407 +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 (e) ->
|
115
|
-
e.preventDefault()
|
116
|
-
|
117
|
-
backgroundColor = $(this).data("background-color")
|
118
|
-
textColor = $(this).data("text-color")
|
119
|
-
paintIt(this, backgroundColor, textColor)
|
120
|
-
```
|
121
|
-
```html
|
122
|
-
<a href="#" data-background-color="#990000">Paint it red</a>
|
123
|
-
<a href="#" data-background-color="#009900" data-text-color="#FFFFFF">Paint it green</a>
|
124
|
-
<a href="#" data-background-color="#000099" data-text-color="#FFFFFF">Paint it blue</a>
|
125
|
-
```
|
126
|
-
|
127
|
-
We call this 'unobtrusive' JavaScript because we're no longer mixing our
|
128
|
-
JavaScript into our HTML. We've properly separated our concerns, making future
|
129
|
-
change easy. We can easily add behavior to any link by adding the data
|
130
|
-
attribute. We can run all of our JavaScript through a minimizer and
|
131
|
-
concatenator. We can serve our entire JavaScript bundle on every page, which
|
132
|
-
means that it'll get downloaded on the first page load and then be cached on
|
133
|
-
every page after that. Lots of little benefits really add up.
|
134
|
-
|
135
|
-
The Rails team strongly encourages you to write your CoffeeScript (and
|
136
|
-
JavaScript) in this style, and you can expect that many libraries will also
|
137
|
-
follow this pattern.
|
138
|
-
|
139
|
-
Built-in Helpers
|
140
|
-
----------------------
|
141
|
-
|
142
|
-
Rails provides a bunch of view helper methods written in Ruby to assist you
|
143
|
-
in generating HTML. Sometimes, you want to add a little Ajax to those elements,
|
144
|
-
and Rails has got your back in those cases.
|
145
|
-
|
146
|
-
Because of Unobtrusive JavaScript, the Rails "Ajax helpers" are actually in two
|
147
|
-
parts: the JavaScript half and the Ruby half.
|
148
|
-
|
149
|
-
[rails.js](https://github.com/rails/jquery-ujs/blob/master/src/rails.js)
|
150
|
-
provides the JavaScript half, and the regular Ruby view helpers add appropriate
|
151
|
-
tags to your DOM. The CoffeeScript in rails.js then listens for these
|
152
|
-
attributes, and attaches appropriate handlers.
|
153
|
-
|
154
|
-
### form_for
|
155
|
-
|
156
|
-
[`form_for`](http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#method-i-form_for)
|
157
|
-
is a helper that assists with writing forms. `form_for` takes a `:remote`
|
158
|
-
option. It works like this:
|
159
|
-
|
160
|
-
```erb
|
161
|
-
<%= form_for(@article, remote: true) do |f| %>
|
162
|
-
...
|
163
|
-
<% end %>
|
164
|
-
```
|
165
|
-
|
166
|
-
This will generate the following HTML:
|
167
|
-
|
168
|
-
```html
|
169
|
-
<form accept-charset="UTF-8" action="/articles" class="new_article" data-remote="true" id="new_article" method="post">
|
170
|
-
...
|
171
|
-
</form>
|
172
|
-
```
|
173
|
-
|
174
|
-
Note the `data-remote="true"`. Now, the form will be submitted by Ajax rather
|
175
|
-
than by the browser's normal submit mechanism.
|
176
|
-
|
177
|
-
You probably don't want to just sit there with a filled out `<form>`, though.
|
178
|
-
You probably want to do something upon a successful submission. To do that,
|
179
|
-
bind to the `ajax:success` event. On failure, use `ajax:error`. Check it out:
|
180
|
-
|
181
|
-
```coffeescript
|
182
|
-
$(document).ready ->
|
183
|
-
$("#new_article").on("ajax:success", (e, data, status, xhr) ->
|
184
|
-
$("#new_article").append xhr.responseText
|
185
|
-
).on "ajax:error", (e, xhr, status, error) ->
|
186
|
-
$("#new_article").append "<p>ERROR</p>"
|
187
|
-
```
|
188
|
-
|
189
|
-
Obviously, you'll want to be a bit more sophisticated than that, but it's a
|
190
|
-
start. You can see more about the events [in the jquery-ujs wiki](https://github.com/rails/jquery-ujs/wiki/ajax).
|
191
|
-
|
192
|
-
### form_tag
|
193
|
-
|
194
|
-
[`form_tag`](http://api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html#method-i-form_tag)
|
195
|
-
is very similar to `form_for`. It has a `:remote` option that you can use like
|
196
|
-
this:
|
197
|
-
|
198
|
-
```erb
|
199
|
-
<%= form_tag('/articles', remote: true) do %>
|
200
|
-
...
|
201
|
-
<% end %>
|
202
|
-
```
|
203
|
-
|
204
|
-
This will generate the following HTML:
|
205
|
-
|
206
|
-
```html
|
207
|
-
<form accept-charset="UTF-8" action="/articles" data-remote="true" method="post">
|
208
|
-
...
|
209
|
-
</form>
|
210
|
-
```
|
211
|
-
|
212
|
-
Everything else is the same as `form_for`. See its documentation for full
|
213
|
-
details.
|
214
|
-
|
215
|
-
### link_to
|
216
|
-
|
217
|
-
[`link_to`](http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-link_to)
|
218
|
-
is a helper that assists with generating links. It has a `:remote` option you
|
219
|
-
can use like this:
|
220
|
-
|
221
|
-
```erb
|
222
|
-
<%= link_to "an article", @article, remote: true %>
|
223
|
-
```
|
224
|
-
|
225
|
-
which generates
|
226
|
-
|
227
|
-
```html
|
228
|
-
<a href="/articles/1" data-remote="true">an article</a>
|
229
|
-
```
|
230
|
-
|
231
|
-
You can bind to the same Ajax events as `form_for`. Here's an example. Let's
|
232
|
-
assume that we have a list of articles that can be deleted with just one
|
233
|
-
click. We would generate some HTML like this:
|
234
|
-
|
235
|
-
```erb
|
236
|
-
<%= link_to "Delete article", @article, remote: true, method: :delete %>
|
237
|
-
```
|
238
|
-
|
239
|
-
and write some CoffeeScript like this:
|
240
|
-
|
241
|
-
```coffeescript
|
242
|
-
$ ->
|
243
|
-
$("a[data-remote]").on "ajax:success", (e, data, status, xhr) ->
|
244
|
-
alert "The article was deleted."
|
245
|
-
```
|
246
|
-
|
247
|
-
### button_to
|
248
|
-
|
249
|
-
[`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:
|
250
|
-
|
251
|
-
```erb
|
252
|
-
<%= button_to "An article", @article, remote: true %>
|
253
|
-
```
|
254
|
-
|
255
|
-
this generates
|
256
|
-
|
257
|
-
```html
|
258
|
-
<form action="/articles/1" class="button_to" data-remote="true" method="post">
|
259
|
-
<div><input type="submit" value="An article"></div>
|
260
|
-
</form>
|
261
|
-
```
|
262
|
-
|
263
|
-
Since it's just a `<form>`, all of the information on `form_for` also applies.
|
264
|
-
|
265
|
-
Server-Side Concerns
|
266
|
-
--------------------
|
267
|
-
|
268
|
-
Ajax isn't just client-side, you also need to do some work on the server
|
269
|
-
side to support it. Often, people like their Ajax requests to return JSON
|
270
|
-
rather than HTML. Let's discuss what it takes to make that happen.
|
271
|
-
|
272
|
-
### A Simple Example
|
273
|
-
|
274
|
-
Imagine you have a series of users that you would like to display and provide a
|
275
|
-
form on that same page to create a new user. The index action of your
|
276
|
-
controller looks like this:
|
277
|
-
|
278
|
-
```ruby
|
279
|
-
class UsersController < ApplicationController
|
280
|
-
def index
|
281
|
-
@users = User.all
|
282
|
-
@user = User.new
|
283
|
-
end
|
284
|
-
# ...
|
285
|
-
```
|
286
|
-
|
287
|
-
The index view (`app/views/users/index.html.erb`) contains:
|
288
|
-
|
289
|
-
```erb
|
290
|
-
<b>Users</b>
|
291
|
-
|
292
|
-
<ul id="users">
|
293
|
-
<%= render @users %>
|
294
|
-
</ul>
|
295
|
-
|
296
|
-
<br>
|
297
|
-
|
298
|
-
<%= form_for(@user, remote: true) do |f| %>
|
299
|
-
<%= f.label :name %><br>
|
300
|
-
<%= f.text_field :name %>
|
301
|
-
<%= f.submit %>
|
302
|
-
<% end %>
|
303
|
-
```
|
304
|
-
|
305
|
-
The `app/views/users/_user.html.erb` partial contains the following:
|
306
|
-
|
307
|
-
```erb
|
308
|
-
<li><%= user.name %></li>
|
309
|
-
```
|
310
|
-
|
311
|
-
The top portion of the index page displays the users. The bottom portion
|
312
|
-
provides a form to create a new user.
|
313
|
-
|
314
|
-
The bottom form will call the `create` action on the `UsersController`. Because
|
315
|
-
the form's remote option is set to true, the request will be posted to the
|
316
|
-
`UsersController` as an Ajax request, looking for JavaScript. In order to
|
317
|
-
serve that request, the `create` action of your controller would look like
|
318
|
-
this:
|
319
|
-
|
320
|
-
```ruby
|
321
|
-
# app/controllers/users_controller.rb
|
322
|
-
# ......
|
323
|
-
def create
|
324
|
-
@user = User.new(params[:user])
|
325
|
-
|
326
|
-
respond_to do |format|
|
327
|
-
if @user.save
|
328
|
-
format.html { redirect_to @user, notice: 'User was successfully created.' }
|
329
|
-
format.js {}
|
330
|
-
format.json { render json: @user, status: :created, location: @user }
|
331
|
-
else
|
332
|
-
format.html { render action: "new" }
|
333
|
-
format.json { render json: @user.errors, status: :unprocessable_entity }
|
334
|
-
end
|
335
|
-
end
|
336
|
-
end
|
337
|
-
```
|
338
|
-
|
339
|
-
Notice the format.js in the `respond_to` block; that allows the controller to
|
340
|
-
respond to your Ajax request. You then have a corresponding
|
341
|
-
`app/views/users/create.js.erb` view file that generates the actual JavaScript
|
342
|
-
code that will be sent and executed on the client side.
|
343
|
-
|
344
|
-
```erb
|
345
|
-
$("<%= escape_javascript(render @user) %>").appendTo("#users");
|
346
|
-
```
|
347
|
-
|
348
|
-
Turbolinks
|
349
|
-
----------
|
350
|
-
|
351
|
-
Rails 4 ships with the [Turbolinks gem](https://github.com/rails/turbolinks).
|
352
|
-
This gem uses Ajax to speed up page rendering in most applications.
|
353
|
-
|
354
|
-
### How Turbolinks Works
|
355
|
-
|
356
|
-
Turbolinks attaches a click handler to all `<a>` on the page. If your browser
|
357
|
-
supports
|
358
|
-
[PushState](https://developer.mozilla.org/en-US/docs/Web/Guide/API/DOM/Manipulating_the_browser_history#The_pushState%28%29_method),
|
359
|
-
Turbolinks will make an Ajax request for the page, parse the response, and
|
360
|
-
replace the entire `<body>` of the page with the `<body>` of the response. It
|
361
|
-
will then use PushState to change the URL to the correct one, preserving
|
362
|
-
refresh semantics and giving you pretty URLs.
|
363
|
-
|
364
|
-
The only thing you have to do to enable Turbolinks is have it in your Gemfile,
|
365
|
-
and put `//= require turbolinks` in your CoffeeScript manifest, which is usually
|
366
|
-
`app/assets/javascripts/application.js`.
|
367
|
-
|
368
|
-
If you want to disable Turbolinks for certain links, add a `data-no-turbolink`
|
369
|
-
attribute to the tag:
|
370
|
-
|
371
|
-
```html
|
372
|
-
<a href="..." data-no-turbolink>No turbolinks here</a>.
|
373
|
-
```
|
374
|
-
|
375
|
-
### Page Change Events
|
376
|
-
|
377
|
-
When writing CoffeeScript, you'll often want to do some sort of processing upon
|
378
|
-
page load. With jQuery, you'd write something like this:
|
379
|
-
|
380
|
-
```coffeescript
|
381
|
-
$(document).ready ->
|
382
|
-
alert "page has loaded!"
|
383
|
-
```
|
384
|
-
|
385
|
-
However, because Turbolinks overrides the normal page loading process, the
|
386
|
-
event that this relies on will not be fired. If you have code that looks like
|
387
|
-
this, you must change your code to do this instead:
|
388
|
-
|
389
|
-
```coffeescript
|
390
|
-
$(document).on "page:change", ->
|
391
|
-
alert "page has loaded!"
|
392
|
-
```
|
393
|
-
|
394
|
-
For more details, including other events you can bind to, check out [the
|
395
|
-
Turbolinks
|
396
|
-
README](https://github.com/rails/turbolinks/blob/master/README.md).
|
397
|
-
|
398
|
-
Other Resources
|
399
|
-
---------------
|
400
|
-
|
401
|
-
Here are some helpful links to help you learn even more:
|
402
|
-
|
403
|
-
* [jquery-ujs wiki](https://github.com/rails/jquery-ujs/wiki)
|
404
|
-
* [jquery-ujs list of external articles](https://github.com/rails/jquery-ujs/wiki/External-articles)
|
405
|
-
* [Rails 3 Remote Links and Forms: A Definitive Guide](http://www.alfajango.com/blog/rails-3-remote-links-and-forms/)
|
406
|
-
* [Railscasts: Unobtrusive JavaScript](http://railscasts.com/episodes/205-unobtrusive-javascript)
|
407
|
-
* [Railscasts: Turbolinks](http://railscasts.com/episodes/390-turbolinks)
|
data/guides/w3c_validator.rb
DELETED
@@ -1,97 +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
|
-
guides.delete("./output/_license.html")
|
64
|
-
guides.delete("./output/_welcome.html")
|
65
|
-
ENV.key?('ONLY') ? select_only(guides) : guides
|
66
|
-
end
|
67
|
-
|
68
|
-
def select_only(guides)
|
69
|
-
prefixes = ENV['ONLY'].split(",").map(&:strip)
|
70
|
-
guides.select do |guide|
|
71
|
-
prefixes.any? {|p| guide.start_with?("./output/#{p}")}
|
72
|
-
end
|
73
|
-
end
|
74
|
-
|
75
|
-
def show_results(error_list)
|
76
|
-
if error_list.size == 0
|
77
|
-
puts "\n\nAll checked guides validate OK!"
|
78
|
-
else
|
79
|
-
error_summary = error_detail = ""
|
80
|
-
|
81
|
-
error_list.each_pair do |name, errors|
|
82
|
-
error_summary += "\n #{name}"
|
83
|
-
error_detail += "\n\n #{name} has #{errors.size} validation error(s):\n"
|
84
|
-
errors.each do |error|
|
85
|
-
error_detail += "\n "+error.to_s.delete("\n")
|
86
|
-
end
|
87
|
-
end
|
88
|
-
|
89
|
-
puts "\n\nThere are #{error_list.size} guides with validation errors:\n" + error_summary
|
90
|
-
puts "\nHere are the detailed errors for each guide:" + error_detail
|
91
|
-
end
|
92
|
-
end
|
93
|
-
|
94
|
-
end
|
95
|
-
end
|
96
|
-
|
97
|
-
RailsGuides::Validator.new.validate
|