-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrails-3-0-jquery.html
134 lines (124 loc) · 8.45 KB
/
rails-3-0-jquery.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>jQuery with Rails 3.0</title>
<link href="https://plus.google.com/u/0/b/117374718581973393536/117374718581973393536/posts/" rel="publisher" />
<link rel="stylesheet" href="/css/bootstrap.css" type="text/css" charset="utf-8" />
<link rel="stylesheet" href="/css/screen.css" type="text/css" charset="utf-8" />
<link rel="stylesheet" href="/css/gollum.css" type="text/css" charset="utf-8" />
<link rel="stylesheet" href="/css/site.css" type="text/css" charset="utf-8" />
<link rel="stylesheet" href="/css/syntax.css" type="text/css" charset="utf-8" />
<script src="http://code.jquery.com/jquery-1.6.min.js" type="text/javascript"></script>
<script src="/javascript/jquery.text_selection-1.0.0.min.js" type="text/javascript"></script>
<script src="/javascript/jquery.previewable_comment_form.js" type="text/javascript"></script>
<script src="/javascript/jquery.tabs.js" type="text/javascript"></script>
<script src="/javascript/gollum.js" type="text/javascript"></script>
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-5109366-14']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
</head>
<body>
<div class="navbar navbar-fixed-top">
<div class="navbar-inner">
<div class="container">
<a href="/" class="brand">RailsApps Project</a>
<ul class="pull-right nav">
<li><a href="http://blog.railsapps.org/" class="twitter">Blog</a></li>
<li><a href="http://twitter.com/rails_apps" class="twitter">Twitter</a></li>
<li><a href="https://plus.google.com/117374718581973393536" class="google">Google +</a></li>
<li><a href="https://github.com/RailsApps" class="github">GitHub Repository</a></li>
</ul>
</div>
</div>
</div>
<div class="container">
<div class="page-header">
<h1>jQuery with Rails 3.0</h1>
</div>
<div class="content wikistyle gollum textile">
<h1>jQuery with Rails 3.0</h1>
<p>This is a guide for developers using the example apps from the <a href="http://railsapps.github.com/">Rails Apps</a> repository. Others may find it helpful as well.</p>
<p>The example applications don’t make use of jQuery. But many Rails developers prefer it as an alternative to the Prototype Javascript framework so instructions are included here for Rails 3.0 projects.</p>
<blockquote>
<p><ins>If you are creating an application template, this step uses the <a href="https://github.com/RailsApps/rails_apps_composer/raw/master/recipes/jquery.rb">jquery</a> recipe from the <a href="https://github.com/RailsApps/rails_apps_composer">rails_apps_composer</a> repository.</ins></p>
</blockquote>
<h2>jQuery is the Default for Rails 3.1</h2>
<p>Rails 3.1 uses jQuery by default so no additional effort is required.</p>
<h2>How to Manually Install jQuery for Rails 3.0</h2>
<p>Here’s how to manually install jQuery for Rails 3.0.</p>
<p>If you used the <code>-J</code> flag when you ran <code>rails new</code> the Prototype Javascript files won’t be there. If not, remove the Prototype Javascript files from the <strong>public/javascripts</strong> directory:</p>
<ul>
<li>controls.js</li>
<li>dragdrop.js</li>
<li>effects.js</li>
<li>prototype.js</li>
</ul><p><a href="http://docs.jquery.com/Downloading_jQuery">Download jQuery</a> and <a href="https://github.com/rails/jquery-ujs/raw/master/src/rails.js">rails.js</a> and place them in the <strong>public/javascripts</strong> directory.</p>
<pre>
cd public/javascripts
curl -o rails.js https://github.com/rails/jquery-ujs/raw/master/src/rails.js
curl -o jquery.js http://code.jquery.com/jquery-1.6.min.js
</pre>
<p>If you used the <code>-J</code> flag when you ran <code>rails new</code> the following statement will be uncommented and you don’t need to do anything. Otherwise, uncomment the following statement in the <strong>config/application.rb</strong> file (or add it if it is missing):</p>
<pre>
config.action_view.javascript_expansions[:defaults] = %w(jquery rails)
</pre>
<p>This configures the template helper <code>javascript_include_tag :defaults</code> to generate <span class="caps">SCRIPT</span> tags to load jQuery and the <strong>rails.js</strong> script.</p>
<h2>How to Automatically Install jQuery for Rails 3.0</h2>
<p>If you used the <code>-J</code> flag when you ran <code>rails new</code> it is best to manually install jQuery as described above.</p>
<p>As an alternative to manual installation of jQuery for Rails 3.0, you can add a gem and run a generator command.</p>
<pre>
# Gemfile
gem 'jquery-rails', '>= 0.2.7'
</pre>
<p>Run the generator command (add <code>--ui</code> if you want the optional jQuery UI library):</p>
<pre>
$ bundle install
$ rails generate jquery:install
</pre>
<p>This will remove the Prototype files from the <strong>public/javascripts</strong> directory, download jQuery files, and download the <strong>rails.js</strong> file. Be sure to choose to overwrite the <strong>rails.js</strong> file (or remove it first).</p>
<p><strong>Note:</strong> If you used the <code>-J</code> flag when you ran <code>rails new</code> (using the “-J” flag to eliminate the Prototype files) then in the <strong>config/application.rb</strong> file the statement <code>config.action_view.javascript_expansions[:defaults] = %w()</code> is not commented out and the jquery-rails gem <strong>lib/jquery-rails.rb</strong> file cannot override <code>config.action_view.javascript_expansions[:defaults]</code> to include the jquery files and you’ll get an error “No expansion found for :defaults” when you run your app. See this <a href="https://github.com/indirect/jquery-rails/issues#issue/54">issue</a> for more information. The solution is to comment out the statement <code>config.action_view.javascript_expansions[:defaults] = %w()</code> in the <strong>config/application.rb</strong> file (it takes precedence over the jquery-rails gem <strong>lib/jquery-rails.rb</strong> file).</p>
</div><!-- class="content" -->
<div class="comments">
<div class="content wikistyle gollum">
<h2>Comments</h2>
</div>
<p>Is this helpful? Please "like" below. Question or suggestion? Please add a comment below. Got a correction or addition? You can edit this page <a href="https://github.com/RailsApps/railsapps.github.com/wiki/_pages">on the wiki</a> or create a <a href="https://github.com/RailsApps/railsapps.github.com/issues">GitHub issue</a> to alert me.</p>
<div id="disqus_thread"></div>
<script type="text/javascript">
/* * * CONFIGURATION VARIABLES: EDIT BEFORE PASTING INTO YOUR WEBPAGE * * */
var disqus_shortname = 'railsapps'; // required: replace example with your forum shortname
/* * * DON'T EDIT BELOW THIS LINE * * */
(function() {
var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
dsq.src = 'http://' + disqus_shortname + '.disqus.com/embed.js';
(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
})();
</script>
<noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
<a href="http://disqus.com" class="dsq-brlink">comments powered by <span class="logo-disqus">Disqus</span></a>
</div><!-- class="comments" -->
<div class="footer row">
<div class="span4">
<h3>Credits</h3>
<p><a href="http://danielkehoe.com/">Daniel Kehoe</a> initiated the <a href="http://railsapps.github.com/">RailsApps Project</a>. Thanks to <a href="http://tigrish.com/">Christopher Dell</a> for design contributions.</p>
</div>
<div class="span4">
<h3>Contributions</h3>
<p>Corrections? Additions? You can edit this page <a href="https://github.com/RailsApps/railsapps.github.com/wiki/_pages">on the wiki</a>.</p>
</div>
<div class="span4">
<h3>Last edit</h3>
<p>by <b>Daniel Kehoe</b>, 2012-05-19 20:21:06</p>
</div>
</div>
</div>
</body>
</html>