rails 4.2.0.beta4 → 4.2.0.rc1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/guides/Rakefile +3 -1
- data/guides/bug_report_templates/action_controller_gem.rb +2 -2
- data/guides/bug_report_templates/action_controller_master.rb +2 -2
- data/guides/source/3_0_release_notes.md +2 -2
- data/guides/source/4_1_release_notes.md +1 -1
- data/guides/source/4_2_release_notes.md +250 -225
- data/guides/source/_welcome.html.erb +1 -1
- data/guides/source/action_controller_overview.md +6 -6
- data/guides/source/action_mailer_basics.md +26 -11
- data/guides/source/action_view_overview.md +0 -1
- data/guides/source/active_job_basics.md +29 -23
- data/guides/source/active_record_postgresql.md +2 -7
- data/guides/source/active_record_validations.md +1 -0
- data/guides/source/active_support_core_extensions.md +1 -1
- data/guides/source/active_support_instrumentation.md +5 -5
- data/guides/source/asset_pipeline.md +2 -2
- data/guides/source/association_basics.md +0 -2
- data/guides/source/command_line.md +6 -15
- data/guides/source/configuring.md +4 -4
- data/guides/source/documents.yaml +0 -1
- data/guides/source/engines.md +26 -15
- data/guides/source/form_helpers.md +10 -2
- data/guides/source/getting_started.md +9 -9
- data/guides/source/rails_on_rack.md +2 -2
- data/guides/source/ruby_on_rails_guides_guidelines.md +1 -0
- data/guides/source/security.md +1 -1
- data/guides/source/testing.md +80 -13
- data/guides/source/upgrading_ruby_on_rails.md +23 -20
- metadata +29 -229
- data/guides/output/2_2_release_notes.html +0 -724
- data/guides/output/2_3_release_notes.html +0 -870
- data/guides/output/3_0_release_notes.html +0 -773
- data/guides/output/3_1_release_notes.html +0 -740
- data/guides/output/3_2_release_notes.html +0 -797
- data/guides/output/4_0_release_notes.html +0 -523
- data/guides/output/4_1_release_notes.html +0 -806
- data/guides/output/4_2_release_notes.html +0 -728
- data/guides/output/Gemfile +0 -6
- data/guides/output/_license.html +0 -226
- data/guides/output/_welcome.html +0 -233
- data/guides/output/action_controller_overview.html +0 -1335
- data/guides/output/action_mailer_basics.html +0 -928
- data/guides/output/action_view_overview.html +0 -1509
- data/guides/output/active_job_basics.html +0 -546
- data/guides/output/active_model_basics.html +0 -438
- data/guides/output/active_record_basics.html +0 -594
- data/guides/output/active_record_callbacks.html +0 -592
- data/guides/output/active_record_migrations.html +0 -1123
- data/guides/output/active_record_postgresql.html +0 -675
- data/guides/output/active_record_querying.html +0 -1796
- data/guides/output/active_record_validations.html +0 -1301
- data/guides/output/active_support_core_extensions.html +0 -3450
- data/guides/output/active_support_instrumentation.html +0 -1121
- data/guides/output/api_documentation_guidelines.html +0 -498
- data/guides/output/asset_pipeline.html +0 -1167
- data/guides/output/association_basics.html +0 -2086
- data/guides/output/caching_with_rails.html +0 -553
- data/guides/output/command_line.html +0 -791
- data/guides/output/configuring.html +0 -1055
- data/guides/output/contributing_to_ruby_on_rails.html +0 -657
- data/guides/output/credits.html +0 -284
- data/guides/output/debugging_rails_applications.html +0 -1014
- data/guides/output/development_dependencies_install.html +0 -478
- data/guides/output/engines.html +0 -1438
- data/guides/output/form_helpers.html +0 -1074
- data/guides/output/generators.html +0 -838
- data/guides/output/getting_started.html +0 -2092
- data/guides/output/i18n.html +0 -1198
- data/guides/output/images/akshaysurve.jpg +0 -0
- data/guides/output/images/belongs_to.png +0 -0
- data/guides/output/images/book_icon.gif +0 -0
- data/guides/output/images/bullet.gif +0 -0
- data/guides/output/images/chapters_icon.gif +0 -0
- data/guides/output/images/check_bullet.gif +0 -0
- data/guides/output/images/credits_pic_blank.gif +0 -0
- data/guides/output/images/csrf.png +0 -0
- data/guides/output/images/edge_badge.png +0 -0
- data/guides/output/images/favicon.ico +0 -0
- data/guides/output/images/feature_tile.gif +0 -0
- data/guides/output/images/footer_tile.gif +0 -0
- data/guides/output/images/fxn.png +0 -0
- data/guides/output/images/getting_started/article_with_comments.png +0 -0
- data/guides/output/images/getting_started/challenge.png +0 -0
- data/guides/output/images/getting_started/confirm_dialog.png +0 -0
- data/guides/output/images/getting_started/forbidden_attributes_for_new_article.png +0 -0
- data/guides/output/images/getting_started/forbidden_attributes_for_new_post.png +0 -0
- data/guides/output/images/getting_started/form_with_errors.png +0 -0
- data/guides/output/images/getting_started/index_action_with_edit_link.png +0 -0
- data/guides/output/images/getting_started/new_article.png +0 -0
- data/guides/output/images/getting_started/new_post.png +0 -0
- data/guides/output/images/getting_started/post_with_comments.png +0 -0
- data/guides/output/images/getting_started/rails_welcome.png +0 -0
- data/guides/output/images/getting_started/routing_error_no_controller.png +0 -0
- data/guides/output/images/getting_started/routing_error_no_route_matches.png +0 -0
- data/guides/output/images/getting_started/show_action_for_articles.png +0 -0
- data/guides/output/images/getting_started/show_action_for_posts.png +0 -0
- data/guides/output/images/getting_started/template_is_missing_articles_new.png +0 -0
- data/guides/output/images/getting_started/template_is_missing_posts_new.png +0 -0
- data/guides/output/images/getting_started/undefined_method_post_path.png +0 -0
- data/guides/output/images/getting_started/unknown_action_create_for_articles.png +0 -0
- data/guides/output/images/getting_started/unknown_action_create_for_posts.png +0 -0
- data/guides/output/images/getting_started/unknown_action_new_for_articles.png +0 -0
- data/guides/output/images/getting_started/unknown_action_new_for_posts.png +0 -0
- data/guides/output/images/grey_bullet.gif +0 -0
- data/guides/output/images/habtm.png +0 -0
- data/guides/output/images/has_many.png +0 -0
- data/guides/output/images/has_many_through.png +0 -0
- data/guides/output/images/has_one.png +0 -0
- data/guides/output/images/has_one_through.png +0 -0
- data/guides/output/images/header_backdrop.png +0 -0
- data/guides/output/images/header_tile.gif +0 -0
- data/guides/output/images/i18n/demo_html_safe.png +0 -0
- data/guides/output/images/i18n/demo_localized_pirate.png +0 -0
- data/guides/output/images/i18n/demo_translated_en.png +0 -0
- data/guides/output/images/i18n/demo_translated_pirate.png +0 -0
- data/guides/output/images/i18n/demo_translation_missing.png +0 -0
- data/guides/output/images/i18n/demo_untranslated.png +0 -0
- data/guides/output/images/icons/README +0 -5
- data/guides/output/images/icons/callouts/1.png +0 -0
- data/guides/output/images/icons/callouts/10.png +0 -0
- data/guides/output/images/icons/callouts/11.png +0 -0
- data/guides/output/images/icons/callouts/12.png +0 -0
- data/guides/output/images/icons/callouts/13.png +0 -0
- data/guides/output/images/icons/callouts/14.png +0 -0
- data/guides/output/images/icons/callouts/15.png +0 -0
- data/guides/output/images/icons/callouts/2.png +0 -0
- data/guides/output/images/icons/callouts/3.png +0 -0
- data/guides/output/images/icons/callouts/4.png +0 -0
- data/guides/output/images/icons/callouts/5.png +0 -0
- data/guides/output/images/icons/callouts/6.png +0 -0
- data/guides/output/images/icons/callouts/7.png +0 -0
- data/guides/output/images/icons/callouts/8.png +0 -0
- data/guides/output/images/icons/callouts/9.png +0 -0
- data/guides/output/images/icons/caution.png +0 -0
- data/guides/output/images/icons/example.png +0 -0
- data/guides/output/images/icons/home.png +0 -0
- data/guides/output/images/icons/important.png +0 -0
- data/guides/output/images/icons/next.png +0 -0
- data/guides/output/images/icons/note.png +0 -0
- data/guides/output/images/icons/prev.png +0 -0
- data/guides/output/images/icons/tip.png +0 -0
- data/guides/output/images/icons/up.png +0 -0
- data/guides/output/images/icons/warning.png +0 -0
- data/guides/output/images/nav_arrow.gif +0 -0
- data/guides/output/images/oscardelben.jpg +0 -0
- data/guides/output/images/polymorphic.png +0 -0
- data/guides/output/images/radar.png +0 -0
- data/guides/output/images/rails4_features.png +0 -0
- data/guides/output/images/rails_guides_kindle_cover.jpg +0 -0
- data/guides/output/images/rails_guides_logo.gif +0 -0
- data/guides/output/images/rails_logo_remix.gif +0 -0
- data/guides/output/images/session_fixation.png +0 -0
- data/guides/output/images/tab_grey.gif +0 -0
- data/guides/output/images/tab_info.gif +0 -0
- data/guides/output/images/tab_note.gif +0 -0
- data/guides/output/images/tab_red.gif +0 -0
- data/guides/output/images/tab_yellow.gif +0 -0
- data/guides/output/images/tab_yellow.png +0 -0
- data/guides/output/images/vijaydev.jpg +0 -0
- data/guides/output/index.html +0 -354
- data/guides/output/initialization.html +0 -876
- data/guides/output/javascripts/guides.js +0 -59
- data/guides/output/javascripts/jquery.min.js +0 -4
- data/guides/output/javascripts/responsive-tables.js +0 -43
- data/guides/output/javascripts/syntaxhighlighter/shBrushAS3.js +0 -59
- data/guides/output/javascripts/syntaxhighlighter/shBrushAppleScript.js +0 -75
- data/guides/output/javascripts/syntaxhighlighter/shBrushBash.js +0 -59
- data/guides/output/javascripts/syntaxhighlighter/shBrushCSharp.js +0 -65
- data/guides/output/javascripts/syntaxhighlighter/shBrushColdFusion.js +0 -100
- data/guides/output/javascripts/syntaxhighlighter/shBrushCpp.js +0 -97
- data/guides/output/javascripts/syntaxhighlighter/shBrushCss.js +0 -91
- data/guides/output/javascripts/syntaxhighlighter/shBrushDelphi.js +0 -55
- data/guides/output/javascripts/syntaxhighlighter/shBrushDiff.js +0 -41
- data/guides/output/javascripts/syntaxhighlighter/shBrushErlang.js +0 -52
- data/guides/output/javascripts/syntaxhighlighter/shBrushGroovy.js +0 -67
- data/guides/output/javascripts/syntaxhighlighter/shBrushJScript.js +0 -52
- data/guides/output/javascripts/syntaxhighlighter/shBrushJava.js +0 -57
- data/guides/output/javascripts/syntaxhighlighter/shBrushJavaFX.js +0 -58
- data/guides/output/javascripts/syntaxhighlighter/shBrushPerl.js +0 -72
- data/guides/output/javascripts/syntaxhighlighter/shBrushPhp.js +0 -88
- data/guides/output/javascripts/syntaxhighlighter/shBrushPlain.js +0 -33
- data/guides/output/javascripts/syntaxhighlighter/shBrushPowerShell.js +0 -74
- data/guides/output/javascripts/syntaxhighlighter/shBrushPython.js +0 -64
- data/guides/output/javascripts/syntaxhighlighter/shBrushRuby.js +0 -55
- data/guides/output/javascripts/syntaxhighlighter/shBrushSass.js +0 -94
- data/guides/output/javascripts/syntaxhighlighter/shBrushScala.js +0 -51
- data/guides/output/javascripts/syntaxhighlighter/shBrushSql.js +0 -66
- data/guides/output/javascripts/syntaxhighlighter/shBrushVb.js +0 -56
- data/guides/output/javascripts/syntaxhighlighter/shBrushXml.js +0 -69
- data/guides/output/javascripts/syntaxhighlighter/shCore.js +0 -17
- data/guides/output/layout.html +0 -448
- data/guides/output/layouts_and_rendering.html +0 -1541
- data/guides/output/maintenance_policy.html +0 -257
- data/guides/output/migrations.html +0 -1360
- data/guides/output/nested_model_forms.html +0 -412
- data/guides/output/plugins.html +0 -644
- data/guides/output/rails_application_templates.html +0 -450
- data/guides/output/rails_on_rack.html +0 -547
- data/guides/output/routing.html +0 -1631
- data/guides/output/ruby_on_rails_guides_guidelines.html +0 -329
- data/guides/output/security.html +0 -935
- data/guides/output/stylesheets/fixes.css +0 -16
- data/guides/output/stylesheets/kindle.css +0 -11
- data/guides/output/stylesheets/main.css +0 -713
- data/guides/output/stylesheets/print.css +0 -52
- data/guides/output/stylesheets/reset.css +0 -43
- data/guides/output/stylesheets/responsive-tables.css +0 -50
- data/guides/output/stylesheets/style.css +0 -13
- data/guides/output/stylesheets/syntaxhighlighter/shCore.css +0 -226
- data/guides/output/stylesheets/syntaxhighlighter/shCoreDefault.css +0 -328
- data/guides/output/stylesheets/syntaxhighlighter/shCoreDjango.css +0 -331
- data/guides/output/stylesheets/syntaxhighlighter/shCoreEclipse.css +0 -339
- data/guides/output/stylesheets/syntaxhighlighter/shCoreEmacs.css +0 -324
- data/guides/output/stylesheets/syntaxhighlighter/shCoreFadeToGrey.css +0 -328
- data/guides/output/stylesheets/syntaxhighlighter/shCoreMDUltra.css +0 -324
- data/guides/output/stylesheets/syntaxhighlighter/shCoreMidnight.css +0 -324
- data/guides/output/stylesheets/syntaxhighlighter/shCoreRDark.css +0 -324
- data/guides/output/stylesheets/syntaxhighlighter/shThemeDefault.css +0 -117
- data/guides/output/stylesheets/syntaxhighlighter/shThemeDjango.css +0 -120
- data/guides/output/stylesheets/syntaxhighlighter/shThemeEclipse.css +0 -128
- data/guides/output/stylesheets/syntaxhighlighter/shThemeEmacs.css +0 -113
- data/guides/output/stylesheets/syntaxhighlighter/shThemeFadeToGrey.css +0 -117
- data/guides/output/stylesheets/syntaxhighlighter/shThemeMDUltra.css +0 -113
- data/guides/output/stylesheets/syntaxhighlighter/shThemeMidnight.css +0 -113
- data/guides/output/stylesheets/syntaxhighlighter/shThemeRDark.css +0 -113
- data/guides/output/stylesheets/syntaxhighlighter/shThemeRailsGuides.css +0 -116
- data/guides/output/testing.html +0 -1350
- data/guides/output/upgrading_ruby_on_rails.html +0 -1135
- data/guides/output/working_with_javascript_in_rails.html +0 -587
@@ -1,838 +0,0 @@
|
|
1
|
-
<!DOCTYPE html>
|
2
|
-
|
3
|
-
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
|
4
|
-
<head>
|
5
|
-
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
|
6
|
-
<meta name="viewport" content="width=device-width, initial-scale=1"/>
|
7
|
-
|
8
|
-
<title>Creating and Customizing Rails Generators & Templates — Ruby on Rails Guides</title>
|
9
|
-
<link rel="stylesheet" type="text/css" href="stylesheets/style.css" />
|
10
|
-
<link rel="stylesheet" type="text/css" href="stylesheets/print.css" media="print" />
|
11
|
-
|
12
|
-
<link rel="stylesheet" type="text/css" href="stylesheets/syntaxhighlighter/shCore.css" />
|
13
|
-
<link rel="stylesheet" type="text/css" href="stylesheets/syntaxhighlighter/shThemeRailsGuides.css" />
|
14
|
-
|
15
|
-
<link rel="stylesheet" type="text/css" href="stylesheets/fixes.css" />
|
16
|
-
|
17
|
-
<link href="images/favicon.ico" rel="shortcut icon" type="image/x-icon" />
|
18
|
-
</head>
|
19
|
-
<body class="guide">
|
20
|
-
<div id="topNav">
|
21
|
-
<div class="wrapper">
|
22
|
-
<strong class="more-info-label">More at <a href="http://rubyonrails.org/">rubyonrails.org:</a> </strong>
|
23
|
-
<span class="red-button more-info-button">
|
24
|
-
More Ruby on Rails
|
25
|
-
</span>
|
26
|
-
<ul class="more-info-links s-hidden">
|
27
|
-
<li class="more-info"><a href="http://rubyonrails.org/">Overview</a></li>
|
28
|
-
<li class="more-info"><a href="http://rubyonrails.org/download">Download</a></li>
|
29
|
-
<li class="more-info"><a href="http://rubyonrails.org/deploy">Deploy</a></li>
|
30
|
-
<li class="more-info"><a href="https://github.com/rails/rails">Code</a></li>
|
31
|
-
<li class="more-info"><a href="http://rubyonrails.org/screencasts">Screencasts</a></li>
|
32
|
-
<li class="more-info"><a href="http://rubyonrails.org/documentation">Documentation</a></li>
|
33
|
-
<li class="more-info"><a href="http://rubyonrails.org/community">Community</a></li>
|
34
|
-
<li class="more-info"><a href="http://weblog.rubyonrails.org/">Blog</a></li>
|
35
|
-
</ul>
|
36
|
-
</div>
|
37
|
-
</div>
|
38
|
-
<div id="header">
|
39
|
-
<div class="wrapper clearfix">
|
40
|
-
<h1><a href="index.html" title="Return to home page">Guides.rubyonrails.org</a></h1>
|
41
|
-
<ul class="nav">
|
42
|
-
<li><a class="nav-item" href="index.html">Home</a></li>
|
43
|
-
<li class="guides-index guides-index-large">
|
44
|
-
<a href="index.html" id="guidesMenu" class="guides-index-item nav-item">Guides Index</a>
|
45
|
-
<div id="guides" class="clearfix" style="display: none;">
|
46
|
-
<hr />
|
47
|
-
<dl class="L">
|
48
|
-
<dt>Start Here</dt>
|
49
|
-
<dd><a href="getting_started.html">Getting Started with Rails</a></dd>
|
50
|
-
<dt>Models</dt>
|
51
|
-
<dd><a href="active_record_basics.html">Active Record Basics</a></dd>
|
52
|
-
<dd><a href="active_record_migrations.html">Active Record Migrations</a></dd>
|
53
|
-
<dd><a href="active_record_validations.html">Active Record Validations</a></dd>
|
54
|
-
<dd><a href="active_record_callbacks.html">Active Record Callbacks</a></dd>
|
55
|
-
<dd><a href="association_basics.html">Active Record Associations</a></dd>
|
56
|
-
<dd><a href="active_record_querying.html">Active Record Query Interface</a></dd>
|
57
|
-
<dt>Views</dt>
|
58
|
-
<dd><a href="layouts_and_rendering.html">Layouts and Rendering in Rails</a></dd>
|
59
|
-
<dd><a href="form_helpers.html">Action View Form Helpers</a></dd>
|
60
|
-
<dt>Controllers</dt>
|
61
|
-
<dd><a href="action_controller_overview.html">Action Controller Overview</a></dd>
|
62
|
-
<dd><a href="routing.html">Rails Routing from the Outside In</a></dd>
|
63
|
-
</dl>
|
64
|
-
<dl class="R">
|
65
|
-
<dt>Digging Deeper</dt>
|
66
|
-
<dd><a href="active_support_core_extensions.html">Active Support Core Extensions</a></dd>
|
67
|
-
<dd><a href="i18n.html">Rails Internationalization API</a></dd>
|
68
|
-
<dd><a href="action_mailer_basics.html">Action Mailer Basics</a></dd>
|
69
|
-
<dd><a href="active_job_basics.html">Active Job Basics</a></dd>
|
70
|
-
<dd><a href="security.html">Securing Rails Applications</a></dd>
|
71
|
-
<dd><a href="debugging_rails_applications.html">Debugging Rails Applications</a></dd>
|
72
|
-
<dd><a href="configuring.html">Configuring Rails Applications</a></dd>
|
73
|
-
<dd><a href="command_line.html">Rails Command Line Tools and Rake Tasks</a></dd>
|
74
|
-
<dd><a href="asset_pipeline.html">Asset Pipeline</a></dd>
|
75
|
-
<dd><a href="working_with_javascript_in_rails.html">Working with JavaScript in Rails</a></dd>
|
76
|
-
<dt>Extending Rails</dt>
|
77
|
-
<dd><a href="rails_on_rack.html">Rails on Rack</a></dd>
|
78
|
-
<dd><a href="generators.html">Creating and Customizing Rails Generators</a></dd>
|
79
|
-
<dt>Contributing to Ruby on Rails</dt>
|
80
|
-
<dd><a href="contributing_to_ruby_on_rails.html">Contributing to Ruby on Rails</a></dd>
|
81
|
-
<dd><a href="api_documentation_guidelines.html">API Documentation Guidelines</a></dd>
|
82
|
-
<dd><a href="ruby_on_rails_guides_guidelines.html">Ruby on Rails Guides Guidelines</a></dd>
|
83
|
-
<dt>Maintenance Policy</dt>
|
84
|
-
<dd><a href="maintenance_policy.html">Maintenance Policy</a></dd>
|
85
|
-
<dt>Release Notes</dt>
|
86
|
-
<dd><a href="upgrading_ruby_on_rails.html">Upgrading Ruby on Rails</a></dd>
|
87
|
-
<dd><a href="4_1_release_notes.html">Ruby on Rails 4.1 Release Notes</a></dd>
|
88
|
-
<dd><a href="4_0_release_notes.html">Ruby on Rails 4.0 Release Notes</a></dd>
|
89
|
-
<dd><a href="3_2_release_notes.html">Ruby on Rails 3.2 Release Notes</a></dd>
|
90
|
-
<dd><a href="3_1_release_notes.html">Ruby on Rails 3.1 Release Notes</a></dd>
|
91
|
-
<dd><a href="3_0_release_notes.html">Ruby on Rails 3.0 Release Notes</a></dd>
|
92
|
-
<dd><a href="2_3_release_notes.html">Ruby on Rails 2.3 Release Notes</a></dd>
|
93
|
-
<dd><a href="2_2_release_notes.html">Ruby on Rails 2.2 Release Notes</a></dd>
|
94
|
-
</dl>
|
95
|
-
</div>
|
96
|
-
</li>
|
97
|
-
<li><a class="nav-item" href="contributing_to_ruby_on_rails.html">Contribute</a></li>
|
98
|
-
<li><a class="nav-item" href="credits.html">Credits</a></li>
|
99
|
-
<li class="guides-index guides-index-small">
|
100
|
-
<select class="guides-index-item nav-item">
|
101
|
-
<option value="index.html">Guides Index</option>
|
102
|
-
<optgroup label="Start Here">
|
103
|
-
<option value="getting_started.html">Getting Started with Rails</option>
|
104
|
-
</optgroup>
|
105
|
-
<optgroup label="Models">
|
106
|
-
<option value="active_record_basics.html">Active Record Basics</option>
|
107
|
-
<option value="active_record_migrations.html">Active Record Migrations</option>
|
108
|
-
<option value="active_record_validations.html">Active Record Validations</option>
|
109
|
-
<option value="active_record_callbacks.html">Active Record Callbacks</option>
|
110
|
-
<option value="association_basics.html">Active Record Associations</option>
|
111
|
-
<option value="active_record_querying.html">Active Record Query Interface</option>
|
112
|
-
</optgroup>
|
113
|
-
<optgroup label="Views">
|
114
|
-
<option value="layouts_and_rendering.html">Layouts and Rendering in Rails</option>
|
115
|
-
<option value="form_helpers.html">Action View Form Helpers</option>
|
116
|
-
</optgroup>
|
117
|
-
<optgroup label="Controllers">
|
118
|
-
<option value="action_controller_overview.html">Action Controller Overview</option>
|
119
|
-
<option value="routing.html">Rails Routing from the Outside In</option>
|
120
|
-
</optgroup>
|
121
|
-
<optgroup label="Digging Deeper">
|
122
|
-
<option value="active_support_core_extensions.html">Active Support Core Extensions</option>
|
123
|
-
<option value="i18n.html">Rails Internationalization API</option>
|
124
|
-
<option value="action_mailer_basics.html">Action Mailer Basics</option>
|
125
|
-
<option value="active_job_basics.html">Active Job Basics</option>
|
126
|
-
<option value="security.html">Securing Rails Applications</option>
|
127
|
-
<option value="debugging_rails_applications.html">Debugging Rails Applications</option>
|
128
|
-
<option value="configuring.html">Configuring Rails Applications</option>
|
129
|
-
<option value="command_line.html">Rails Command Line Tools and Rake Tasks</option>
|
130
|
-
<option value="asset_pipeline.html">Asset Pipeline</option>
|
131
|
-
<option value="working_with_javascript_in_rails.html">Working with JavaScript in Rails</option>
|
132
|
-
</optgroup>
|
133
|
-
<optgroup label="Extending Rails">
|
134
|
-
<option value="rails_on_rack.html">Rails on Rack</option>
|
135
|
-
<option value="generators.html">Creating and Customizing Rails Generators</option>
|
136
|
-
</optgroup>
|
137
|
-
<optgroup label="Contributing to Ruby on Rails">
|
138
|
-
<option value="contributing_to_ruby_on_rails.html">Contributing to Ruby on Rails</option>
|
139
|
-
<option value="api_documentation_guidelines.html">API Documentation Guidelines</option>
|
140
|
-
<option value="ruby_on_rails_guides_guidelines.html">Ruby on Rails Guides Guidelines</option>
|
141
|
-
</optgroup>
|
142
|
-
<optgroup label="Maintenance Policy">
|
143
|
-
<option value="maintenance_policy.html">Maintenance Policy</option>
|
144
|
-
</optgroup>
|
145
|
-
<optgroup label="Release Notes">
|
146
|
-
<option value="upgrading_ruby_on_rails.html">Upgrading Ruby on Rails</option>
|
147
|
-
<option value="4_1_release_notes.html">Ruby on Rails 4.1 Release Notes</option>
|
148
|
-
<option value="4_0_release_notes.html">Ruby on Rails 4.0 Release Notes</option>
|
149
|
-
<option value="3_2_release_notes.html">Ruby on Rails 3.2 Release Notes</option>
|
150
|
-
<option value="3_1_release_notes.html">Ruby on Rails 3.1 Release Notes</option>
|
151
|
-
<option value="3_0_release_notes.html">Ruby on Rails 3.0 Release Notes</option>
|
152
|
-
<option value="2_3_release_notes.html">Ruby on Rails 2.3 Release Notes</option>
|
153
|
-
<option value="2_2_release_notes.html">Ruby on Rails 2.2 Release Notes</option>
|
154
|
-
</optgroup>
|
155
|
-
</select>
|
156
|
-
</li>
|
157
|
-
</ul>
|
158
|
-
</div>
|
159
|
-
</div>
|
160
|
-
<hr class="hide" />
|
161
|
-
|
162
|
-
<div id="feature">
|
163
|
-
<div class="wrapper">
|
164
|
-
<h2>Creating and Customizing Rails Generators & Templates</h2><p>Rails generators are an essential tool if you plan to improve your workflow. With this guide you will learn how to create generators and customize existing ones.</p><p>After reading this guide, you will know:</p>
|
165
|
-
<ul>
|
166
|
-
<li>How to see which generators are available in your application.</li>
|
167
|
-
<li>How to create a generator using templates.</li>
|
168
|
-
<li>How Rails searches for generators before invoking them.</li>
|
169
|
-
<li>How Rails internally generates Rails code from the templates.</li>
|
170
|
-
<li>How to customize your scaffold by creating new generators.</li>
|
171
|
-
<li>How to customize your scaffold by changing generator templates.</li>
|
172
|
-
<li>How to use fallbacks to avoid overwriting a huge set of generators.</li>
|
173
|
-
<li>How to create an application template.</li>
|
174
|
-
</ul>
|
175
|
-
|
176
|
-
|
177
|
-
<div id="subCol">
|
178
|
-
<h3 class="chapter"><img src="images/chapters_icon.gif" alt="" />Chapters</h3>
|
179
|
-
<ol class="chapters">
|
180
|
-
<li><a href="#first-contact">First Contact</a></li>
|
181
|
-
<li><a href="#creating-your-first-generator">Creating Your First Generator</a></li>
|
182
|
-
<li><a href="#creating-generators-with-generators">Creating Generators with Generators</a></li>
|
183
|
-
<li><a href="#generators-lookup">Generators Lookup</a></li>
|
184
|
-
<li><a href="#customizing-your-workflow">Customizing Your Workflow</a></li>
|
185
|
-
<li><a href="#customizing-your-workflow-by-changing-generators-templates">Customizing Your Workflow by Changing Generators Templates</a></li>
|
186
|
-
<li><a href="#adding-generators-fallbacks">Adding Generators Fallbacks</a></li>
|
187
|
-
<li><a href="#application-templates">Application Templates</a></li>
|
188
|
-
<li>
|
189
|
-
<a href="#generator-methods">Generator methods</a>
|
190
|
-
|
191
|
-
<ul>
|
192
|
-
<li><a href="#gem"><code>gem</code></a></li>
|
193
|
-
<li><a href="#gem_group"><code>gem_group</code></a></li>
|
194
|
-
<li><a href="#add_source"><code>add_source</code></a></li>
|
195
|
-
<li><a href="#inject_into_file"><code>inject_into_file</code></a></li>
|
196
|
-
<li><a href="#gsub_file"><code>gsub_file</code></a></li>
|
197
|
-
<li><a href="#application"><code>application</code></a></li>
|
198
|
-
<li><a href="#git"><code>git</code></a></li>
|
199
|
-
<li><a href="#vendor"><code>vendor</code></a></li>
|
200
|
-
<li><a href="#lib"><code>lib</code></a></li>
|
201
|
-
<li><a href="#rakefile"><code>rakefile</code></a></li>
|
202
|
-
<li><a href="#initializer"><code>initializer</code></a></li>
|
203
|
-
<li><a href="#generate"><code>generate</code></a></li>
|
204
|
-
<li><a href="#rake"><code>rake</code></a></li>
|
205
|
-
<li><a href="#capify-bang"><code>capify!</code></a></li>
|
206
|
-
<li><a href="#route"><code>route</code></a></li>
|
207
|
-
<li><a href="#readme"><code>readme</code></a></li>
|
208
|
-
</ul>
|
209
|
-
</li>
|
210
|
-
</ol>
|
211
|
-
|
212
|
-
</div>
|
213
|
-
|
214
|
-
</div>
|
215
|
-
</div>
|
216
|
-
|
217
|
-
<div id="container">
|
218
|
-
<div class="wrapper">
|
219
|
-
<div id="mainCol">
|
220
|
-
<h3 id="first-contact">1 First Contact</h3><p>When you create an application using the <code>rails</code> command, you are in fact using a Rails generator. After that, you can get a list of all available generators by just invoking <code>rails generate</code>:</p><div class="code_container">
|
221
|
-
<pre class="brush: plain; gutter: false; toolbar: false">
|
222
|
-
$ rails new myapp
|
223
|
-
$ cd myapp
|
224
|
-
$ bin/rails generate
|
225
|
-
|
226
|
-
</pre>
|
227
|
-
</div>
|
228
|
-
<p>You will get a list of all generators that comes with Rails. If you need a detailed description of the helper generator, for example, you can simply do:</p><div class="code_container">
|
229
|
-
<pre class="brush: plain; gutter: false; toolbar: false">
|
230
|
-
$ bin/rails generate helper --help
|
231
|
-
|
232
|
-
</pre>
|
233
|
-
</div>
|
234
|
-
<h3 id="creating-your-first-generator">2 Creating Your First Generator</h3><p>Since Rails 3.0, generators are built on top of <a href="https://github.com/erikhuda/thor">Thor</a>. Thor provides powerful options for parsing and a great API for manipulating files. For instance, let's build a generator that creates an initializer file named <code>initializer.rb</code> inside <code>config/initializers</code>.</p><p>The first step is to create a file at <code>lib/generators/initializer_generator.rb</code> with the following content:</p><div class="code_container">
|
235
|
-
<pre class="brush: ruby; gutter: false; toolbar: false">
|
236
|
-
class InitializerGenerator < Rails::Generators::Base
|
237
|
-
def create_initializer_file
|
238
|
-
create_file "config/initializers/initializer.rb", "# Add initialization content here"
|
239
|
-
end
|
240
|
-
end
|
241
|
-
|
242
|
-
</pre>
|
243
|
-
</div>
|
244
|
-
<div class="note"><p><code>create_file</code> is a method provided by <code>Thor::Actions</code>. Documentation for <code>create_file</code> and other Thor methods can be found in <a href="http://rdoc.info/github/erikhuda/thor/master/Thor/Actions.html">Thor's documentation</a></p></div><p>Our new generator is quite simple: it inherits from <code>Rails::Generators::Base</code> and has one method definition. When a generator is invoked, each public method in the generator is executed sequentially in the order that it is defined. Finally, we invoke the <code>create_file</code> method that will create a file at the given destination with the given content. If you are familiar with the Rails Application Templates API, you'll feel right at home with the new generators API.</p><p>To invoke our new generator, we just need to do:</p><div class="code_container">
|
245
|
-
<pre class="brush: plain; gutter: false; toolbar: false">
|
246
|
-
$ bin/rails generate initializer
|
247
|
-
|
248
|
-
</pre>
|
249
|
-
</div>
|
250
|
-
<p>Before we go on, let's see our brand new generator description:</p><div class="code_container">
|
251
|
-
<pre class="brush: plain; gutter: false; toolbar: false">
|
252
|
-
$ bin/rails generate initializer --help
|
253
|
-
|
254
|
-
</pre>
|
255
|
-
</div>
|
256
|
-
<p>Rails is usually able to generate good descriptions if a generator is namespaced, as <code>ActiveRecord::Generators::ModelGenerator</code>, but not in this particular case. We can solve this problem in two ways. The first one is calling <code>desc</code> inside our generator:</p><div class="code_container">
|
257
|
-
<pre class="brush: ruby; gutter: false; toolbar: false">
|
258
|
-
class InitializerGenerator < Rails::Generators::Base
|
259
|
-
desc "This generator creates an initializer file at config/initializers"
|
260
|
-
def create_initializer_file
|
261
|
-
create_file "config/initializers/initializer.rb", "# Add initialization content here"
|
262
|
-
end
|
263
|
-
end
|
264
|
-
|
265
|
-
</pre>
|
266
|
-
</div>
|
267
|
-
<p>Now we can see the new description by invoking <code>--help</code> on the new generator. The second way to add a description is by creating a file named <code>USAGE</code> in the same directory as our generator. We are going to do that in the next step.</p><h3 id="creating-generators-with-generators">3 Creating Generators with Generators</h3><p>Generators themselves have a generator:</p><div class="code_container">
|
268
|
-
<pre class="brush: plain; gutter: false; toolbar: false">
|
269
|
-
$ bin/rails generate generator initializer
|
270
|
-
create lib/generators/initializer
|
271
|
-
create lib/generators/initializer/initializer_generator.rb
|
272
|
-
create lib/generators/initializer/USAGE
|
273
|
-
create lib/generators/initializer/templates
|
274
|
-
|
275
|
-
</pre>
|
276
|
-
</div>
|
277
|
-
<p>This is the generator just created:</p><div class="code_container">
|
278
|
-
<pre class="brush: ruby; gutter: false; toolbar: false">
|
279
|
-
class InitializerGenerator < Rails::Generators::NamedBase
|
280
|
-
source_root File.expand_path("../templates", __FILE__)
|
281
|
-
end
|
282
|
-
|
283
|
-
</pre>
|
284
|
-
</div>
|
285
|
-
<p>First, notice that we are inheriting from <code>Rails::Generators::NamedBase</code> instead of <code>Rails::Generators::Base</code>. This means that our generator expects at least one argument, which will be the name of the initializer, and will be available in our code in the variable <code>name</code>.</p><p>We can see that by invoking the description of this new generator (don't forget to delete the old generator file):</p><div class="code_container">
|
286
|
-
<pre class="brush: plain; gutter: false; toolbar: false">
|
287
|
-
$ bin/rails generate initializer --help
|
288
|
-
Usage:
|
289
|
-
rails generate initializer NAME [options]
|
290
|
-
|
291
|
-
</pre>
|
292
|
-
</div>
|
293
|
-
<p>We can also see that our new generator has a class method called <code>source_root</code>. This method points to where our generator templates will be placed, if any, and by default it points to the created directory <code>lib/generators/initializer/templates</code>.</p><p>In order to understand what a generator template means, let's create the file <code>lib/generators/initializer/templates/initializer.rb</code> with the following content:</p><div class="code_container">
|
294
|
-
<pre class="brush: ruby; gutter: false; toolbar: false">
|
295
|
-
# Add initialization content here
|
296
|
-
|
297
|
-
</pre>
|
298
|
-
</div>
|
299
|
-
<p>And now let's change the generator to copy this template when invoked:</p><div class="code_container">
|
300
|
-
<pre class="brush: ruby; gutter: false; toolbar: false">
|
301
|
-
class InitializerGenerator < Rails::Generators::NamedBase
|
302
|
-
source_root File.expand_path("../templates", __FILE__)
|
303
|
-
|
304
|
-
def copy_initializer_file
|
305
|
-
copy_file "initializer.rb", "config/initializers/#{file_name}.rb"
|
306
|
-
end
|
307
|
-
end
|
308
|
-
|
309
|
-
</pre>
|
310
|
-
</div>
|
311
|
-
<p>And let's execute our generator:</p><div class="code_container">
|
312
|
-
<pre class="brush: plain; gutter: false; toolbar: false">
|
313
|
-
$ bin/rails generate initializer core_extensions
|
314
|
-
|
315
|
-
</pre>
|
316
|
-
</div>
|
317
|
-
<p>We can see that now an initializer named core_extensions was created at <code>config/initializers/core_extensions.rb</code> with the contents of our template. That means that <code>copy_file</code> copied a file in our source root to the destination path we gave. The method <code>file_name</code> is automatically created when we inherit from <code>Rails::Generators::NamedBase</code>.</p><p>The methods that are available for generators are covered in the <a href="#generator-methods">final section</a> of this guide.</p><h3 id="generators-lookup">4 Generators Lookup</h3><p>When you run <code>rails generate initializer core_extensions</code> Rails requires these files in turn until one is found:</p><div class="code_container">
|
318
|
-
<pre class="brush: plain; gutter: false; toolbar: false">
|
319
|
-
rails/generators/initializer/initializer_generator.rb
|
320
|
-
generators/initializer/initializer_generator.rb
|
321
|
-
rails/generators/initializer_generator.rb
|
322
|
-
generators/initializer_generator.rb
|
323
|
-
|
324
|
-
</pre>
|
325
|
-
</div>
|
326
|
-
<p>If none is found you get an error message.</p><div class="info"><p>The examples above put files under the application's <code>lib</code> because said directory belongs to <code>$LOAD_PATH</code>.</p></div><h3 id="customizing-your-workflow">5 Customizing Your Workflow</h3><p>Rails own generators are flexible enough to let you customize scaffolding. They can be configured in <code>config/application.rb</code>, these are some defaults:</p><div class="code_container">
|
327
|
-
<pre class="brush: ruby; gutter: false; toolbar: false">
|
328
|
-
config.generators do |g|
|
329
|
-
g.orm :active_record
|
330
|
-
g.template_engine :erb
|
331
|
-
g.test_framework :test_unit, fixture: true
|
332
|
-
end
|
333
|
-
|
334
|
-
</pre>
|
335
|
-
</div>
|
336
|
-
<p>Before we customize our workflow, let's first see what our scaffold looks like:</p><div class="code_container">
|
337
|
-
<pre class="brush: plain; gutter: false; toolbar: false">
|
338
|
-
$ bin/rails generate scaffold User name:string
|
339
|
-
invoke active_record
|
340
|
-
create db/migrate/20130924151154_create_users.rb
|
341
|
-
create app/models/user.rb
|
342
|
-
invoke test_unit
|
343
|
-
create test/models/user_test.rb
|
344
|
-
create test/fixtures/users.yml
|
345
|
-
invoke resource_route
|
346
|
-
route resources :users
|
347
|
-
invoke scaffold_controller
|
348
|
-
create app/controllers/users_controller.rb
|
349
|
-
invoke erb
|
350
|
-
create app/views/users
|
351
|
-
create app/views/users/index.html.erb
|
352
|
-
create app/views/users/edit.html.erb
|
353
|
-
create app/views/users/show.html.erb
|
354
|
-
create app/views/users/new.html.erb
|
355
|
-
create app/views/users/_form.html.erb
|
356
|
-
invoke test_unit
|
357
|
-
create test/controllers/users_controller_test.rb
|
358
|
-
invoke helper
|
359
|
-
create app/helpers/users_helper.rb
|
360
|
-
invoke jbuilder
|
361
|
-
create app/views/users/index.json.jbuilder
|
362
|
-
create app/views/users/show.json.jbuilder
|
363
|
-
invoke assets
|
364
|
-
invoke coffee
|
365
|
-
create app/assets/javascripts/users.js.coffee
|
366
|
-
invoke scss
|
367
|
-
create app/assets/stylesheets/users.css.scss
|
368
|
-
invoke scss
|
369
|
-
create app/assets/stylesheets/scaffolds.css.scss
|
370
|
-
|
371
|
-
</pre>
|
372
|
-
</div>
|
373
|
-
<p>Looking at this output, it's easy to understand how generators work in Rails 3.0 and above. The scaffold generator doesn't actually generate anything, it just invokes others to do the work. This allows us to add/replace/remove any of those invocations. For instance, the scaffold generator invokes the scaffold_controller generator, which invokes erb, test_unit and helper generators. Since each generator has a single responsibility, they are easy to reuse, avoiding code duplication.</p><p>Our first customization on the workflow will be to stop generating stylesheet, JavaScript and test fixture files for scaffolds. We can achieve that by changing our configuration to the following:</p><div class="code_container">
|
374
|
-
<pre class="brush: ruby; gutter: false; toolbar: false">
|
375
|
-
config.generators do |g|
|
376
|
-
g.orm :active_record
|
377
|
-
g.template_engine :erb
|
378
|
-
g.test_framework :test_unit, fixture: false
|
379
|
-
g.stylesheets false
|
380
|
-
g.javascripts false
|
381
|
-
end
|
382
|
-
|
383
|
-
</pre>
|
384
|
-
</div>
|
385
|
-
<p>If we generate another resource with the scaffold generator, we can see that stylesheet, JavaScript and fixture files are not created anymore. If you want to customize it further, for example to use DataMapper and RSpec instead of Active Record and TestUnit, it's just a matter of adding their gems to your application and configuring your generators.</p><p>To demonstrate this, we are going to create a new helper generator that simply adds some instance variable readers. First, we create a generator within the rails namespace, as this is where rails searches for generators used as hooks:</p><div class="code_container">
|
386
|
-
<pre class="brush: plain; gutter: false; toolbar: false">
|
387
|
-
$ bin/rails generate generator rails/my_helper
|
388
|
-
create lib/generators/rails/my_helper
|
389
|
-
create lib/generators/rails/my_helper/my_helper_generator.rb
|
390
|
-
create lib/generators/rails/my_helper/USAGE
|
391
|
-
create lib/generators/rails/my_helper/templates
|
392
|
-
|
393
|
-
</pre>
|
394
|
-
</div>
|
395
|
-
<p>After that, we can delete both the <code>templates</code> directory and the <code>source_root</code>
|
396
|
-
class method call from our new generator, because we are not going to need them.
|
397
|
-
Add the method below, so our generator looks like the following:</p><div class="code_container">
|
398
|
-
<pre class="brush: ruby; gutter: false; toolbar: false">
|
399
|
-
# lib/generators/rails/my_helper/my_helper_generator.rb
|
400
|
-
class Rails::MyHelperGenerator < Rails::Generators::NamedBase
|
401
|
-
def create_helper_file
|
402
|
-
create_file "app/helpers/#{file_name}_helper.rb", <<-FILE
|
403
|
-
module #{class_name}Helper
|
404
|
-
attr_reader :#{plural_name}, :#{plural_name.singularize}
|
405
|
-
end
|
406
|
-
FILE
|
407
|
-
end
|
408
|
-
end
|
409
|
-
|
410
|
-
</pre>
|
411
|
-
</div>
|
412
|
-
<p>We can try out our new generator by creating a helper for products:</p><div class="code_container">
|
413
|
-
<pre class="brush: plain; gutter: false; toolbar: false">
|
414
|
-
$ bin/rails generate my_helper products
|
415
|
-
create app/helpers/products_helper.rb
|
416
|
-
|
417
|
-
</pre>
|
418
|
-
</div>
|
419
|
-
<p>And it will generate the following helper file in <code>app/helpers</code>:</p><div class="code_container">
|
420
|
-
<pre class="brush: ruby; gutter: false; toolbar: false">
|
421
|
-
module ProductsHelper
|
422
|
-
attr_reader :products, :product
|
423
|
-
end
|
424
|
-
|
425
|
-
</pre>
|
426
|
-
</div>
|
427
|
-
<p>Which is what we expected. We can now tell scaffold to use our new helper generator by editing <code>config/application.rb</code> once again:</p><div class="code_container">
|
428
|
-
<pre class="brush: ruby; gutter: false; toolbar: false">
|
429
|
-
config.generators do |g|
|
430
|
-
g.orm :active_record
|
431
|
-
g.template_engine :erb
|
432
|
-
g.test_framework :test_unit, fixture: false
|
433
|
-
g.stylesheets false
|
434
|
-
g.javascripts false
|
435
|
-
g.helper :my_helper
|
436
|
-
end
|
437
|
-
|
438
|
-
</pre>
|
439
|
-
</div>
|
440
|
-
<p>and see it in action when invoking the generator:</p><div class="code_container">
|
441
|
-
<pre class="brush: plain; gutter: false; toolbar: false">
|
442
|
-
$ bin/rails generate scaffold Article body:text
|
443
|
-
[...]
|
444
|
-
invoke my_helper
|
445
|
-
create app/helpers/articles_helper.rb
|
446
|
-
|
447
|
-
</pre>
|
448
|
-
</div>
|
449
|
-
<p>We can notice on the output that our new helper was invoked instead of the Rails default. However one thing is missing, which is tests for our new generator and to do that, we are going to reuse old helpers test generators.</p><p>Since Rails 3.0, this is easy to do due to the hooks concept. Our new helper does not need to be focused in one specific test framework, it can simply provide a hook and a test framework just needs to implement this hook in order to be compatible.</p><p>To do that, we can change the generator this way:</p><div class="code_container">
|
450
|
-
<pre class="brush: ruby; gutter: false; toolbar: false">
|
451
|
-
# lib/generators/rails/my_helper/my_helper_generator.rb
|
452
|
-
class Rails::MyHelperGenerator < Rails::Generators::NamedBase
|
453
|
-
def create_helper_file
|
454
|
-
create_file "app/helpers/#{file_name}_helper.rb", <<-FILE
|
455
|
-
module #{class_name}Helper
|
456
|
-
attr_reader :#{plural_name}, :#{plural_name.singularize}
|
457
|
-
end
|
458
|
-
FILE
|
459
|
-
end
|
460
|
-
|
461
|
-
hook_for :test_framework
|
462
|
-
end
|
463
|
-
|
464
|
-
</pre>
|
465
|
-
</div>
|
466
|
-
<p>Now, when the helper generator is invoked and TestUnit is configured as the test framework, it will try to invoke both <code>Rails::TestUnitGenerator</code> and <code>TestUnit::MyHelperGenerator</code>. Since none of those are defined, we can tell our generator to invoke <code>TestUnit::Generators::HelperGenerator</code> instead, which is defined since it's a Rails generator. To do that, we just need to add:</p><div class="code_container">
|
467
|
-
<pre class="brush: ruby; gutter: false; toolbar: false">
|
468
|
-
# Search for :helper instead of :my_helper
|
469
|
-
hook_for :test_framework, as: :helper
|
470
|
-
|
471
|
-
</pre>
|
472
|
-
</div>
|
473
|
-
<p>And now you can re-run scaffold for another resource and see it generating tests as well!</p><h3 id="customizing-your-workflow-by-changing-generators-templates">6 Customizing Your Workflow by Changing Generators Templates</h3><p>In the step above we simply wanted to add a line to the generated helper, without adding any extra functionality. There is a simpler way to do that, and it's by replacing the templates of already existing generators, in that case <code>Rails::Generators::HelperGenerator</code>.</p><p>In Rails 3.0 and above, generators don't just look in the source root for templates, they also search for templates in other paths. And one of them is <code>lib/templates</code>. Since we want to customize <code>Rails::Generators::HelperGenerator</code>, we can do that by simply making a template copy inside <code>lib/templates/rails/helper</code> with the name <code>helper.rb</code>. So let's create that file with the following content:</p><div class="code_container">
|
474
|
-
<pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
|
475
|
-
module <%= class_name %>Helper
|
476
|
-
attr_reader :<%= plural_name %>, :<%= plural_name.singularize %>
|
477
|
-
end
|
478
|
-
|
479
|
-
</pre>
|
480
|
-
</div>
|
481
|
-
<p>and revert the last change in <code>config/application.rb</code>:</p><div class="code_container">
|
482
|
-
<pre class="brush: ruby; gutter: false; toolbar: false">
|
483
|
-
config.generators do |g|
|
484
|
-
g.orm :active_record
|
485
|
-
g.template_engine :erb
|
486
|
-
g.test_framework :test_unit, fixture: false
|
487
|
-
g.stylesheets false
|
488
|
-
g.javascripts false
|
489
|
-
end
|
490
|
-
|
491
|
-
</pre>
|
492
|
-
</div>
|
493
|
-
<p>If you generate another resource, you can see that we get exactly the same result! This is useful if you want to customize your scaffold templates and/or layout by just creating <code>edit.html.erb</code>, <code>index.html.erb</code> and so on inside <code>lib/templates/erb/scaffold</code>.</p><p>Many scaffold templates in Rails are written in ERB tags which need to be escaped, so that the output is valid ERB code. For example,</p><div class="code_container">
|
494
|
-
<pre class="brush: ruby; gutter: false; toolbar: false">
|
495
|
-
<%%= stylesheet_include_tag :application %>
|
496
|
-
|
497
|
-
</pre>
|
498
|
-
</div>
|
499
|
-
<p>when passed through the generator, would generate the following output.</p><div class="code_container">
|
500
|
-
<pre class="brush: ruby; gutter: false; toolbar: false">
|
501
|
-
<%= stylesheet_include_tag :application %>
|
502
|
-
|
503
|
-
</pre>
|
504
|
-
</div>
|
505
|
-
<h3 id="adding-generators-fallbacks">7 Adding Generators Fallbacks</h3><p>One last feature about generators which is quite useful for plugin generators is fallbacks. For example, imagine that you want to add a feature on top of TestUnit like <a href="https://github.com/thoughtbot/shoulda">shoulda</a> does. Since TestUnit already implements all generators required by Rails and shoulda just wants to overwrite part of it, there is no need for shoulda to reimplement some generators again, it can simply tell Rails to use a <code>TestUnit</code> generator if none was found under the <code>Shoulda</code> namespace.</p><p>We can easily simulate this behavior by changing our <code>config/application.rb</code> once again:</p><div class="code_container">
|
506
|
-
<pre class="brush: ruby; gutter: false; toolbar: false">
|
507
|
-
config.generators do |g|
|
508
|
-
g.orm :active_record
|
509
|
-
g.template_engine :erb
|
510
|
-
g.test_framework :shoulda, fixture: false
|
511
|
-
g.stylesheets false
|
512
|
-
g.javascripts false
|
513
|
-
|
514
|
-
# Add a fallback!
|
515
|
-
g.fallbacks[:shoulda] = :test_unit
|
516
|
-
end
|
517
|
-
|
518
|
-
</pre>
|
519
|
-
</div>
|
520
|
-
<p>Now, if you create a Comment scaffold, you will see that the shoulda generators are being invoked, and at the end, they are just falling back to TestUnit generators:</p><div class="code_container">
|
521
|
-
<pre class="brush: plain; gutter: false; toolbar: false">
|
522
|
-
$ bin/rails generate scaffold Comment body:text
|
523
|
-
invoke active_record
|
524
|
-
create db/migrate/20130924143118_create_comments.rb
|
525
|
-
create app/models/comment.rb
|
526
|
-
invoke shoulda
|
527
|
-
create test/models/comment_test.rb
|
528
|
-
create test/fixtures/comments.yml
|
529
|
-
invoke resource_route
|
530
|
-
route resources :comments
|
531
|
-
invoke scaffold_controller
|
532
|
-
create app/controllers/comments_controller.rb
|
533
|
-
invoke erb
|
534
|
-
create app/views/comments
|
535
|
-
create app/views/comments/index.html.erb
|
536
|
-
create app/views/comments/edit.html.erb
|
537
|
-
create app/views/comments/show.html.erb
|
538
|
-
create app/views/comments/new.html.erb
|
539
|
-
create app/views/comments/_form.html.erb
|
540
|
-
invoke shoulda
|
541
|
-
create test/controllers/comments_controller_test.rb
|
542
|
-
invoke my_helper
|
543
|
-
create app/helpers/comments_helper.rb
|
544
|
-
invoke jbuilder
|
545
|
-
create app/views/comments/index.json.jbuilder
|
546
|
-
create app/views/comments/show.json.jbuilder
|
547
|
-
invoke assets
|
548
|
-
invoke coffee
|
549
|
-
create app/assets/javascripts/comments.js.coffee
|
550
|
-
invoke scss
|
551
|
-
|
552
|
-
</pre>
|
553
|
-
</div>
|
554
|
-
<p>Fallbacks allow your generators to have a single responsibility, increasing code reuse and reducing the amount of duplication.</p><h3 id="application-templates">8 Application Templates</h3><p>Now that you've seen how generators can be used <em>inside</em> an application, did you know they can also be used to <em>generate</em> applications too? This kind of generator is referred as a "template". This is a brief overview of the Templates API. For detailed documentation see the <a href="rails_application_templates.html">Rails Application Templates guide</a>.</p><div class="code_container">
|
555
|
-
<pre class="brush: ruby; gutter: false; toolbar: false">
|
556
|
-
gem "rspec-rails", group: "test"
|
557
|
-
gem "cucumber-rails", group: "test"
|
558
|
-
|
559
|
-
if yes?("Would you like to install Devise?")
|
560
|
-
gem "devise"
|
561
|
-
generate "devise:install"
|
562
|
-
model_name = ask("What would you like the user model to be called? [user]")
|
563
|
-
model_name = "user" if model_name.blank?
|
564
|
-
generate "devise", model_name
|
565
|
-
end
|
566
|
-
|
567
|
-
</pre>
|
568
|
-
</div>
|
569
|
-
<p>In the above template we specify that the application relies on the <code>rspec-rails</code> and <code>cucumber-rails</code> gem so these two will be added to the <code>test</code> group in the <code>Gemfile</code>. Then we pose a question to the user about whether or not they would like to install Devise. If the user replies "y" or "yes" to this question, then the template will add Devise to the <code>Gemfile</code> outside of any group and then runs the <code>devise:install</code> generator. This template then takes the users input and runs the <code>devise</code> generator, with the user's answer from the last question being passed to this generator.</p><p>Imagine that this template was in a file called <code>template.rb</code>. We can use it to modify the outcome of the <code>rails new</code> command by using the <code>-m</code> option and passing in the filename:</p><div class="code_container">
|
570
|
-
<pre class="brush: plain; gutter: false; toolbar: false">
|
571
|
-
$ rails new thud -m template.rb
|
572
|
-
|
573
|
-
</pre>
|
574
|
-
</div>
|
575
|
-
<p>This command will generate the <code>Thud</code> application, and then apply the template to the generated output.</p><p>Templates don't have to be stored on the local system, the <code>-m</code> option also supports online templates:</p><div class="code_container">
|
576
|
-
<pre class="brush: plain; gutter: false; toolbar: false">
|
577
|
-
$ rails new thud -m https://gist.github.com/radar/722911/raw/
|
578
|
-
|
579
|
-
</pre>
|
580
|
-
</div>
|
581
|
-
<p>Whilst the final section of this guide doesn't cover how to generate the most awesome template known to man, it will take you through the methods available at your disposal so that you can develop it yourself. These same methods are also available for generators.</p><h3 id="generator-methods">9 Generator methods</h3><p>The following are methods available for both generators and templates for Rails.</p><div class="note"><p>Methods provided by Thor are not covered this guide and can be found in <a href="http://rdoc.info/github/erikhuda/thor/master/Thor/Actions.html">Thor's documentation</a></p></div><h4 id="gem">9.1 <code>gem</code>
|
582
|
-
</h4><p>Specifies a gem dependency of the application.</p><div class="code_container">
|
583
|
-
<pre class="brush: ruby; gutter: false; toolbar: false">
|
584
|
-
gem "rspec", group: "test", version: "2.1.0"
|
585
|
-
gem "devise", "1.1.5"
|
586
|
-
|
587
|
-
</pre>
|
588
|
-
</div>
|
589
|
-
<p>Available options are:</p>
|
590
|
-
<ul>
|
591
|
-
<li>
|
592
|
-
<code>:group</code> - The group in the <code>Gemfile</code> where this gem should go.</li>
|
593
|
-
<li>
|
594
|
-
<code>:version</code> - The version string of the gem you want to use. Can also be specified as the second argument to the method.</li>
|
595
|
-
<li>
|
596
|
-
<code>:git</code> - The URL to the git repository for this gem.</li>
|
597
|
-
</ul>
|
598
|
-
<p>Any additional options passed to this method are put on the end of the line:</p><div class="code_container">
|
599
|
-
<pre class="brush: ruby; gutter: false; toolbar: false">
|
600
|
-
gem "devise", git: "git://github.com/plataformatec/devise", branch: "master"
|
601
|
-
|
602
|
-
</pre>
|
603
|
-
</div>
|
604
|
-
<p>The above code will put the following line into <code>Gemfile</code>:</p><div class="code_container">
|
605
|
-
<pre class="brush: ruby; gutter: false; toolbar: false">
|
606
|
-
gem "devise", git: "git://github.com/plataformatec/devise", branch: "master"
|
607
|
-
|
608
|
-
</pre>
|
609
|
-
</div>
|
610
|
-
<h4 id="gem_group">9.2 <code>gem_group</code>
|
611
|
-
</h4><p>Wraps gem entries inside a group:</p><div class="code_container">
|
612
|
-
<pre class="brush: ruby; gutter: false; toolbar: false">
|
613
|
-
gem_group :development, :test do
|
614
|
-
gem "rspec-rails"
|
615
|
-
end
|
616
|
-
|
617
|
-
</pre>
|
618
|
-
</div>
|
619
|
-
<h4 id="add_source">9.3 <code>add_source</code>
|
620
|
-
</h4><p>Adds a specified source to <code>Gemfile</code>:</p><div class="code_container">
|
621
|
-
<pre class="brush: ruby; gutter: false; toolbar: false">
|
622
|
-
add_source "http://gems.github.com"
|
623
|
-
|
624
|
-
</pre>
|
625
|
-
</div>
|
626
|
-
<h4 id="inject_into_file">9.4 <code>inject_into_file</code>
|
627
|
-
</h4><p>Injects a block of code into a defined position in your file.</p><div class="code_container">
|
628
|
-
<pre class="brush: ruby; gutter: false; toolbar: false">
|
629
|
-
inject_into_file 'name_of_file.rb', after: "#The code goes below this line. Don't forget the Line break at the end\n" do <<-'RUBY'
|
630
|
-
puts "Hello World"
|
631
|
-
RUBY
|
632
|
-
end
|
633
|
-
|
634
|
-
</pre>
|
635
|
-
</div>
|
636
|
-
<h4 id="gsub_file">9.5 <code>gsub_file</code>
|
637
|
-
</h4><p>Replaces text inside a file.</p><div class="code_container">
|
638
|
-
<pre class="brush: ruby; gutter: false; toolbar: false">
|
639
|
-
gsub_file 'name_of_file.rb', 'method.to_be_replaced', 'method.the_replacing_code'
|
640
|
-
|
641
|
-
</pre>
|
642
|
-
</div>
|
643
|
-
<p>Regular Expressions can be used to make this method more precise. You can also use <code>append_file</code> and <code>prepend_file</code> in the same way to place code at the beginning and end of a file respectively.</p><h4 id="application">9.6 <code>application</code>
|
644
|
-
</h4><p>Adds a line to <code>config/application.rb</code> directly after the application class definition.</p><div class="code_container">
|
645
|
-
<pre class="brush: ruby; gutter: false; toolbar: false">
|
646
|
-
application "config.asset_host = 'http://example.com'"
|
647
|
-
|
648
|
-
</pre>
|
649
|
-
</div>
|
650
|
-
<p>This method can also take a block:</p><div class="code_container">
|
651
|
-
<pre class="brush: ruby; gutter: false; toolbar: false">
|
652
|
-
application do
|
653
|
-
"config.asset_host = 'http://example.com'"
|
654
|
-
end
|
655
|
-
|
656
|
-
</pre>
|
657
|
-
</div>
|
658
|
-
<p>Available options are:</p>
|
659
|
-
<ul>
|
660
|
-
<li>
|
661
|
-
<code>:env</code> - Specify an environment for this configuration option. If you wish to use this option with the block syntax the recommended syntax is as follows:</li>
|
662
|
-
</ul>
|
663
|
-
<div class="code_container">
|
664
|
-
<pre class="brush: ruby; gutter: false; toolbar: false">
|
665
|
-
application(nil, env: "development") do
|
666
|
-
"config.asset_host = 'http://localhost:3000'"
|
667
|
-
end
|
668
|
-
|
669
|
-
</pre>
|
670
|
-
</div>
|
671
|
-
<h4 id="git">9.7 <code>git</code>
|
672
|
-
</h4><p>Runs the specified git command:</p><div class="code_container">
|
673
|
-
<pre class="brush: ruby; gutter: false; toolbar: false">
|
674
|
-
git :init
|
675
|
-
git add: "."
|
676
|
-
git commit: "-m First commit!"
|
677
|
-
git add: "onefile.rb", rm: "badfile.cxx"
|
678
|
-
|
679
|
-
</pre>
|
680
|
-
</div>
|
681
|
-
<p>The values of the hash here being the arguments or options passed to the specific git command. As per the final example shown here, multiple git commands can be specified at a time, but the order of their running is not guaranteed to be the same as the order that they were specified in.</p><h4 id="vendor">9.8 <code>vendor</code>
|
682
|
-
</h4><p>Places a file into <code>vendor</code> which contains the specified code.</p><div class="code_container">
|
683
|
-
<pre class="brush: ruby; gutter: false; toolbar: false">
|
684
|
-
vendor "sekrit.rb", '#top secret stuff'
|
685
|
-
|
686
|
-
</pre>
|
687
|
-
</div>
|
688
|
-
<p>This method also takes a block:</p><div class="code_container">
|
689
|
-
<pre class="brush: ruby; gutter: false; toolbar: false">
|
690
|
-
vendor "seeds.rb" do
|
691
|
-
"puts 'in your app, seeding your database'"
|
692
|
-
end
|
693
|
-
|
694
|
-
</pre>
|
695
|
-
</div>
|
696
|
-
<h4 id="lib">9.9 <code>lib</code>
|
697
|
-
</h4><p>Places a file into <code>lib</code> which contains the specified code.</p><div class="code_container">
|
698
|
-
<pre class="brush: ruby; gutter: false; toolbar: false">
|
699
|
-
lib "special.rb", "p Rails.root"
|
700
|
-
|
701
|
-
</pre>
|
702
|
-
</div>
|
703
|
-
<p>This method also takes a block:</p><div class="code_container">
|
704
|
-
<pre class="brush: ruby; gutter: false; toolbar: false">
|
705
|
-
lib "super_special.rb" do
|
706
|
-
puts "Super special!"
|
707
|
-
end
|
708
|
-
|
709
|
-
</pre>
|
710
|
-
</div>
|
711
|
-
<h4 id="rakefile">9.10 <code>rakefile</code>
|
712
|
-
</h4><p>Creates a Rake file in the <code>lib/tasks</code> directory of the application.</p><div class="code_container">
|
713
|
-
<pre class="brush: ruby; gutter: false; toolbar: false">
|
714
|
-
rakefile "test.rake", "hello there"
|
715
|
-
|
716
|
-
</pre>
|
717
|
-
</div>
|
718
|
-
<p>This method also takes a block:</p><div class="code_container">
|
719
|
-
<pre class="brush: ruby; gutter: false; toolbar: false">
|
720
|
-
rakefile "test.rake" do
|
721
|
-
%Q{
|
722
|
-
task rock: :environment do
|
723
|
-
puts "Rockin'"
|
724
|
-
end
|
725
|
-
}
|
726
|
-
end
|
727
|
-
|
728
|
-
</pre>
|
729
|
-
</div>
|
730
|
-
<h4 id="initializer">9.11 <code>initializer</code>
|
731
|
-
</h4><p>Creates an initializer in the <code>config/initializers</code> directory of the application:</p><div class="code_container">
|
732
|
-
<pre class="brush: ruby; gutter: false; toolbar: false">
|
733
|
-
initializer "begin.rb", "puts 'this is the beginning'"
|
734
|
-
|
735
|
-
</pre>
|
736
|
-
</div>
|
737
|
-
<p>This method also takes a block, expected to return a string:</p><div class="code_container">
|
738
|
-
<pre class="brush: ruby; gutter: false; toolbar: false">
|
739
|
-
initializer "begin.rb" do
|
740
|
-
"puts 'this is the beginning'"
|
741
|
-
end
|
742
|
-
|
743
|
-
</pre>
|
744
|
-
</div>
|
745
|
-
<h4 id="generate">9.12 <code>generate</code>
|
746
|
-
</h4><p>Runs the specified generator where the first argument is the generator name and the remaining arguments are passed directly to the generator.</p><div class="code_container">
|
747
|
-
<pre class="brush: ruby; gutter: false; toolbar: false">
|
748
|
-
generate "scaffold", "forums title:string description:text"
|
749
|
-
|
750
|
-
</pre>
|
751
|
-
</div>
|
752
|
-
<h4 id="rake">9.13 <code>rake</code>
|
753
|
-
</h4><p>Runs the specified Rake task.</p><div class="code_container">
|
754
|
-
<pre class="brush: ruby; gutter: false; toolbar: false">
|
755
|
-
rake "db:migrate"
|
756
|
-
|
757
|
-
</pre>
|
758
|
-
</div>
|
759
|
-
<p>Available options are:</p>
|
760
|
-
<ul>
|
761
|
-
<li>
|
762
|
-
<code>:env</code> - Specifies the environment in which to run this rake task.</li>
|
763
|
-
<li>
|
764
|
-
<code>:sudo</code> - Whether or not to run this task using <code>sudo</code>. Defaults to <code>false</code>.</li>
|
765
|
-
</ul>
|
766
|
-
<h4 id="capify-bang">9.14 <code>capify!</code>
|
767
|
-
</h4><p>Runs the <code>capify</code> command from Capistrano at the root of the application which generates Capistrano configuration.</p><div class="code_container">
|
768
|
-
<pre class="brush: ruby; gutter: false; toolbar: false">
|
769
|
-
capify!
|
770
|
-
|
771
|
-
</pre>
|
772
|
-
</div>
|
773
|
-
<h4 id="route">9.15 <code>route</code>
|
774
|
-
</h4><p>Adds text to the <code>config/routes.rb</code> file:</p><div class="code_container">
|
775
|
-
<pre class="brush: ruby; gutter: false; toolbar: false">
|
776
|
-
route "resources :people"
|
777
|
-
|
778
|
-
</pre>
|
779
|
-
</div>
|
780
|
-
<h4 id="readme">9.16 <code>readme</code>
|
781
|
-
</h4><p>Output the contents of a file in the template's <code>source_path</code>, usually a README.</p><div class="code_container">
|
782
|
-
<pre class="brush: ruby; gutter: false; toolbar: false">
|
783
|
-
readme "README"
|
784
|
-
|
785
|
-
</pre>
|
786
|
-
</div>
|
787
|
-
|
788
|
-
|
789
|
-
<h3>Feedback</h3>
|
790
|
-
<p>
|
791
|
-
You're encouraged to help improve the quality of this guide.
|
792
|
-
</p>
|
793
|
-
<p>
|
794
|
-
Please contribute if you see any typos or factual errors.
|
795
|
-
To get started, you can read our <a href="http://edgeguides.rubyonrails.org/contributing_to_ruby_on_rails.html#contributing-to-the-rails-documentation">documentation contributions</a> section.
|
796
|
-
</p>
|
797
|
-
<p>
|
798
|
-
You may also find incomplete content, or stuff that is not up to date.
|
799
|
-
Please do add any missing documentation for master. Make sure to check
|
800
|
-
<a href="http://edgeguides.rubyonrails.org">Edge Guides</a> first to verify
|
801
|
-
if the issues are already fixed or not on the master branch.
|
802
|
-
Check the <a href="ruby_on_rails_guides_guidelines.html">Ruby on Rails Guides Guidelines</a>
|
803
|
-
for style and conventions.
|
804
|
-
</p>
|
805
|
-
<p>
|
806
|
-
If for whatever reason you spot something to fix but cannot patch it yourself, please
|
807
|
-
<a href="https://github.com/rails/rails/issues">open an issue</a>.
|
808
|
-
</p>
|
809
|
-
<p>And last but not least, any kind of discussion regarding Ruby on Rails
|
810
|
-
documentation is very welcome in the <a href="http://groups.google.com/group/rubyonrails-docs">rubyonrails-docs mailing list</a>.
|
811
|
-
</p>
|
812
|
-
</div>
|
813
|
-
</div>
|
814
|
-
</div>
|
815
|
-
|
816
|
-
<hr class="hide" />
|
817
|
-
<div id="footer">
|
818
|
-
<div class="wrapper">
|
819
|
-
<p>This work is licensed under a <a href="https://creativecommons.org/licenses/by-sa/4.0/">Creative Commons Attribution-ShareAlike 4.0 International</a> License</p>
|
820
|
-
<p>"Rails", "Ruby on Rails", and the Rails logo are trademarks of David Heinemeier Hansson. All rights reserved.</p>
|
821
|
-
|
822
|
-
</div>
|
823
|
-
</div>
|
824
|
-
|
825
|
-
<script type="text/javascript" src="javascripts/jquery.min.js"></script>
|
826
|
-
<script type="text/javascript" src="javascripts/responsive-tables.js"></script>
|
827
|
-
<script type="text/javascript" src="javascripts/guides.js"></script>
|
828
|
-
<script type="text/javascript" src="javascripts/syntaxhighlighter/shCore.js"></script>
|
829
|
-
<script type="text/javascript" src="javascripts/syntaxhighlighter/shBrushRuby.js"></script>
|
830
|
-
<script type="text/javascript" src="javascripts/syntaxhighlighter/shBrushXml.js"></script>
|
831
|
-
<script type="text/javascript" src="javascripts/syntaxhighlighter/shBrushSql.js"></script>
|
832
|
-
<script type="text/javascript" src="javascripts/syntaxhighlighter/shBrushPlain.js"></script>
|
833
|
-
<script type="text/javascript">
|
834
|
-
SyntaxHighlighter.all();
|
835
|
-
$(guidesIndex.bind);
|
836
|
-
</script>
|
837
|
-
</body>
|
838
|
-
</html>
|