rails 4.2.0.beta3 → 4.2.0.beta4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +9 -7
- data/guides/output/2_2_release_notes.html +724 -0
- data/guides/output/2_3_release_notes.html +870 -0
- data/guides/output/3_0_release_notes.html +773 -0
- data/guides/output/3_1_release_notes.html +740 -0
- data/guides/output/3_2_release_notes.html +797 -0
- data/guides/output/4_0_release_notes.html +523 -0
- data/guides/output/4_1_release_notes.html +806 -0
- data/guides/output/4_2_release_notes.html +728 -0
- data/guides/output/Gemfile +6 -0
- data/guides/output/_license.html +226 -0
- data/guides/output/_welcome.html +233 -0
- data/guides/output/action_controller_overview.html +1335 -0
- data/guides/output/action_mailer_basics.html +928 -0
- data/guides/output/action_view_overview.html +1509 -0
- data/guides/output/active_job_basics.html +546 -0
- data/guides/output/active_model_basics.html +438 -0
- data/guides/output/active_record_basics.html +594 -0
- data/guides/output/active_record_callbacks.html +592 -0
- data/guides/output/active_record_migrations.html +1123 -0
- data/guides/output/active_record_postgresql.html +675 -0
- data/guides/output/active_record_querying.html +1796 -0
- data/guides/output/active_record_validations.html +1301 -0
- data/guides/output/active_support_core_extensions.html +3450 -0
- data/guides/output/active_support_instrumentation.html +1121 -0
- data/guides/output/api_documentation_guidelines.html +498 -0
- data/guides/output/asset_pipeline.html +1167 -0
- data/guides/output/association_basics.html +2086 -0
- data/guides/output/caching_with_rails.html +553 -0
- data/guides/output/command_line.html +791 -0
- data/guides/output/configuring.html +1055 -0
- data/guides/output/contributing_to_ruby_on_rails.html +657 -0
- data/guides/output/credits.html +284 -0
- data/guides/output/debugging_rails_applications.html +1014 -0
- data/guides/output/development_dependencies_install.html +478 -0
- data/guides/output/engines.html +1438 -0
- data/guides/output/form_helpers.html +1074 -0
- data/guides/output/generators.html +838 -0
- data/guides/output/getting_started.html +2092 -0
- data/guides/output/i18n.html +1198 -0
- 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 +5 -0
- 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 +354 -0
- data/guides/output/initialization.html +876 -0
- data/guides/output/javascripts/guides.js +59 -0
- data/guides/output/javascripts/jquery.min.js +4 -0
- data/guides/output/javascripts/responsive-tables.js +43 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushAS3.js +59 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushAppleScript.js +75 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushBash.js +59 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushCSharp.js +65 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushColdFusion.js +100 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushCpp.js +97 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushCss.js +91 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushDelphi.js +55 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushDiff.js +41 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushErlang.js +52 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushGroovy.js +67 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushJScript.js +52 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushJava.js +57 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushJavaFX.js +58 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushPerl.js +72 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushPhp.js +88 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushPlain.js +33 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushPowerShell.js +74 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushPython.js +64 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushRuby.js +55 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushSass.js +94 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushScala.js +51 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushSql.js +66 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushVb.js +56 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushXml.js +69 -0
- data/guides/output/javascripts/syntaxhighlighter/shCore.js +17 -0
- data/guides/output/layout.html +448 -0
- data/guides/output/layouts_and_rendering.html +1541 -0
- data/guides/output/maintenance_policy.html +257 -0
- data/guides/output/migrations.html +1360 -0
- data/guides/output/nested_model_forms.html +412 -0
- data/guides/output/plugins.html +644 -0
- data/guides/output/rails_application_templates.html +450 -0
- data/guides/output/rails_on_rack.html +547 -0
- data/guides/output/routing.html +1631 -0
- data/guides/output/ruby_on_rails_guides_guidelines.html +329 -0
- data/guides/output/security.html +935 -0
- data/guides/output/stylesheets/fixes.css +16 -0
- data/guides/output/stylesheets/kindle.css +11 -0
- data/guides/output/stylesheets/main.css +713 -0
- data/guides/output/stylesheets/print.css +52 -0
- data/guides/output/stylesheets/reset.css +43 -0
- data/guides/output/stylesheets/responsive-tables.css +50 -0
- data/guides/output/stylesheets/style.css +13 -0
- data/guides/output/stylesheets/syntaxhighlighter/shCore.css +226 -0
- data/guides/output/stylesheets/syntaxhighlighter/shCoreDefault.css +328 -0
- data/guides/output/stylesheets/syntaxhighlighter/shCoreDjango.css +331 -0
- data/guides/output/stylesheets/syntaxhighlighter/shCoreEclipse.css +339 -0
- data/guides/output/stylesheets/syntaxhighlighter/shCoreEmacs.css +324 -0
- data/guides/output/stylesheets/syntaxhighlighter/shCoreFadeToGrey.css +328 -0
- data/guides/output/stylesheets/syntaxhighlighter/shCoreMDUltra.css +324 -0
- data/guides/output/stylesheets/syntaxhighlighter/shCoreMidnight.css +324 -0
- data/guides/output/stylesheets/syntaxhighlighter/shCoreRDark.css +324 -0
- data/guides/output/stylesheets/syntaxhighlighter/shThemeDefault.css +117 -0
- data/guides/output/stylesheets/syntaxhighlighter/shThemeDjango.css +120 -0
- data/guides/output/stylesheets/syntaxhighlighter/shThemeEclipse.css +128 -0
- data/guides/output/stylesheets/syntaxhighlighter/shThemeEmacs.css +113 -0
- data/guides/output/stylesheets/syntaxhighlighter/shThemeFadeToGrey.css +117 -0
- data/guides/output/stylesheets/syntaxhighlighter/shThemeMDUltra.css +113 -0
- data/guides/output/stylesheets/syntaxhighlighter/shThemeMidnight.css +113 -0
- data/guides/output/stylesheets/syntaxhighlighter/shThemeRDark.css +113 -0
- data/guides/output/stylesheets/syntaxhighlighter/shThemeRailsGuides.css +116 -0
- data/guides/output/testing.html +1350 -0
- data/guides/output/upgrading_ruby_on_rails.html +1135 -0
- data/guides/output/working_with_javascript_in_rails.html +587 -0
- data/guides/source/4_0_release_notes.md +2 -2
- data/guides/source/4_2_release_notes.md +9 -2
- data/guides/source/action_controller_overview.md +3 -1
- data/guides/source/action_mailer_basics.md +1 -2
- data/guides/source/active_job_basics.md +25 -2
- data/guides/source/active_model_basics.md +350 -19
- data/guides/source/active_record_basics.md +2 -2
- data/guides/source/active_record_migrations.md +1 -1
- data/guides/source/active_record_validations.md +12 -4
- data/guides/source/active_support_core_extensions.md +7 -9
- data/guides/source/active_support_instrumentation.md +2 -0
- data/guides/source/association_basics.md +7 -7
- data/guides/source/configuring.md +24 -0
- data/guides/source/contributing_to_ruby_on_rails.md +1 -1
- data/guides/source/credits.html.erb +1 -1
- data/guides/source/documents.yaml +5 -0
- data/guides/source/engines.md +2 -2
- data/guides/source/form_helpers.md +6 -0
- data/guides/source/i18n.md +6 -9
- data/guides/source/layouts_and_rendering.md +1 -1
- data/guides/source/plugins.md +2 -2
- data/guides/source/rails_on_rack.md +4 -0
- data/guides/source/testing.md +1 -0
- data/guides/source/upgrading_ruby_on_rails.md +5 -5
- metadata +227 -27
@@ -0,0 +1,412 @@
|
|
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>Rails nested model forms — 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>Rails nested model forms</h2><p>Creating a form for a model <em>and</em> its associations can become quite tedious. Therefore Rails provides helpers to assist in dealing with the complexities of generating these forms <em>and</em> the required CRUD operations to create, update, and destroy associations.</p><p>After reading this guide, you will know:</p>
|
165
|
+
<ul>
|
166
|
+
<li>do stuff.</li>
|
167
|
+
</ul>
|
168
|
+
|
169
|
+
|
170
|
+
<div id="subCol">
|
171
|
+
<h3 class="chapter"><img src="images/chapters_icon.gif" alt="" />Chapters</h3>
|
172
|
+
<ol class="chapters">
|
173
|
+
<li>
|
174
|
+
<a href="#model-setup">Model setup</a>
|
175
|
+
|
176
|
+
<ul>
|
177
|
+
<li><a href="#activerecord::base-model">ActiveRecord::Base model</a></li>
|
178
|
+
<li><a href="#custom-model">Custom model</a></li>
|
179
|
+
</ul>
|
180
|
+
</li>
|
181
|
+
<li>
|
182
|
+
<a href="#views">Views</a>
|
183
|
+
|
184
|
+
<ul>
|
185
|
+
<li><a href="#controller-code">Controller code</a></li>
|
186
|
+
<li><a href="#form-code">Form code</a></li>
|
187
|
+
</ul>
|
188
|
+
</li>
|
189
|
+
</ol>
|
190
|
+
|
191
|
+
</div>
|
192
|
+
|
193
|
+
</div>
|
194
|
+
</div>
|
195
|
+
|
196
|
+
<div id="container">
|
197
|
+
<div class="wrapper">
|
198
|
+
<div id="mainCol">
|
199
|
+
<div class="note"><p>This guide assumes the user knows how to use the <a href="form_helpers.html">Rails form helpers</a> in general. Also, it's <strong>not</strong> an API reference. For a complete reference please visit <a href="http://api.rubyonrails.org/">the Rails API documentation</a>.</p></div><h3 id="model-setup">1 Model setup</h3><p>To be able to use the nested model functionality in your forms, the model will need to support some basic operations.</p><p>First of all, it needs to define a writer method for the attribute that corresponds to the association you are building a nested model form for. The <code>fields_for</code> form helper will look for this method to decide whether or not a nested model form should be built.</p><p>If the associated object is an array, a form builder will be yielded for each object, else only a single form builder will be yielded.</p><p>Consider a Person model with an associated Address. When asked to yield a nested FormBuilder for the <code>:address</code> attribute, the <code>fields_for</code> form helper will look for a method on the Person instance named <code>address_attributes=</code>.</p><h4 id="activerecord::base-model">1.1 ActiveRecord::Base model</h4><p>For an ActiveRecord::Base model and association this writer method is commonly defined with the <code>accepts_nested_attributes_for</code> class method:</p><h5 id="has_one">1.1.1 has_one</h5><div class="code_container">
|
200
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
201
|
+
class Person < ActiveRecord::Base
|
202
|
+
has_one :address
|
203
|
+
accepts_nested_attributes_for :address
|
204
|
+
end
|
205
|
+
|
206
|
+
</pre>
|
207
|
+
</div>
|
208
|
+
<h5 id="belongs_to">1.1.2 belongs_to</h5><div class="code_container">
|
209
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
210
|
+
class Person < ActiveRecord::Base
|
211
|
+
belongs_to :firm
|
212
|
+
accepts_nested_attributes_for :firm
|
213
|
+
end
|
214
|
+
|
215
|
+
</pre>
|
216
|
+
</div>
|
217
|
+
<h5 id="has_many-/-has_and_belongs_to_many">1.1.3 has_many / has_and_belongs_to_many</h5><div class="code_container">
|
218
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
219
|
+
class Person < ActiveRecord::Base
|
220
|
+
has_many :projects
|
221
|
+
accepts_nested_attributes_for :projects
|
222
|
+
end
|
223
|
+
|
224
|
+
</pre>
|
225
|
+
</div>
|
226
|
+
<h4 id="custom-model">1.2 Custom model</h4><p>As you might have inflected from this explanation, you <em>don't</em> necessarily need an ActiveRecord::Base model to use this functionality. The following examples are sufficient to enable the nested model form behavior:</p><h5 id="single-associated-object">1.2.1 Single associated object</h5><div class="code_container">
|
227
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
228
|
+
class Person
|
229
|
+
def address
|
230
|
+
Address.new
|
231
|
+
end
|
232
|
+
|
233
|
+
def address_attributes=(attributes)
|
234
|
+
# ...
|
235
|
+
end
|
236
|
+
end
|
237
|
+
|
238
|
+
</pre>
|
239
|
+
</div>
|
240
|
+
<h5 id="association-collection">1.2.2 Association collection</h5><div class="code_container">
|
241
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
242
|
+
class Person
|
243
|
+
def projects
|
244
|
+
[Project.new, Project.new]
|
245
|
+
end
|
246
|
+
|
247
|
+
def projects_attributes=(attributes)
|
248
|
+
# ...
|
249
|
+
end
|
250
|
+
end
|
251
|
+
|
252
|
+
</pre>
|
253
|
+
</div>
|
254
|
+
<div class="note"><p>See (TODO) in the advanced section for more information on how to deal with the CRUD operations in your custom model.</p></div><h3 id="views">2 Views</h3><h4 id="controller-code">2.1 Controller code</h4><p>A nested model form will <em>only</em> be built if the associated object(s) exist. This means that for a new model instance you would probably want to build the associated object(s) first.</p><p>Consider the following typical RESTful controller which will prepare a new Person instance and its <code>address</code> and <code>projects</code> associations before rendering the <code>new</code> template:</p><div class="code_container">
|
255
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
256
|
+
class PeopleController < ApplicationController
|
257
|
+
def new
|
258
|
+
@person = Person.new
|
259
|
+
@person.built_address
|
260
|
+
2.times { @person.projects.build }
|
261
|
+
end
|
262
|
+
|
263
|
+
def create
|
264
|
+
@person = Person.new(params[:person])
|
265
|
+
if @person.save
|
266
|
+
# ...
|
267
|
+
end
|
268
|
+
end
|
269
|
+
end
|
270
|
+
|
271
|
+
</pre>
|
272
|
+
</div>
|
273
|
+
<div class="note"><p>Obviously the instantiation of the associated object(s) can become tedious and not DRY, so you might want to move that into the model itself. ActiveRecord::Base provides an <code>after_initialize</code> callback which is a good way to refactor this.</p></div><h4 id="form-code">2.2 Form code</h4><p>Now that you have a model instance, with the appropriate methods and associated object(s), you can start building the nested model form.</p><h5 id="standard-form">2.2.1 Standard form</h5><p>Start out with a regular RESTful form:</p><div class="code_container">
|
274
|
+
<pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
|
275
|
+
<%= form_for @person do |f| %>
|
276
|
+
<%= f.text_field :name %>
|
277
|
+
<% end %>
|
278
|
+
|
279
|
+
</pre>
|
280
|
+
</div>
|
281
|
+
<p>This will generate the following html:</p><div class="code_container">
|
282
|
+
<pre class="brush: xml; gutter: false; toolbar: false">
|
283
|
+
<form action="/people" class="new_person" id="new_person" method="post">
|
284
|
+
<input id="person_name" name="person[name]" type="text" />
|
285
|
+
</form>
|
286
|
+
|
287
|
+
</pre>
|
288
|
+
</div>
|
289
|
+
<h5 id="nested-form-for-a-single-associated-object">2.2.2 Nested form for a single associated object</h5><p>Now add a nested form for the <code>address</code> association:</p><div class="code_container">
|
290
|
+
<pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
|
291
|
+
<%= form_for @person do |f| %>
|
292
|
+
<%= f.text_field :name %>
|
293
|
+
|
294
|
+
<%= f.fields_for :address do |af| %>
|
295
|
+
<%= af.text_field :street %>
|
296
|
+
<% end %>
|
297
|
+
<% end %>
|
298
|
+
|
299
|
+
</pre>
|
300
|
+
</div>
|
301
|
+
<p>This generates:</p><div class="code_container">
|
302
|
+
<pre class="brush: xml; gutter: false; toolbar: false">
|
303
|
+
<form action="/people" class="new_person" id="new_person" method="post">
|
304
|
+
<input id="person_name" name="person[name]" type="text" />
|
305
|
+
|
306
|
+
<input id="person_address_attributes_street" name="person[address_attributes][street]" type="text" />
|
307
|
+
</form>
|
308
|
+
|
309
|
+
</pre>
|
310
|
+
</div>
|
311
|
+
<p>Notice that <code>fields_for</code> recognized the <code>address</code> as an association for which a nested model form should be built by the way it has namespaced the <code>name</code> attribute.</p><p>When this form is posted the Rails parameter parser will construct a hash like the following:</p><div class="code_container">
|
312
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
313
|
+
{
|
314
|
+
"person" => {
|
315
|
+
"name" => "Eloy Duran",
|
316
|
+
"address_attributes" => {
|
317
|
+
"street" => "Nieuwe Prinsengracht"
|
318
|
+
}
|
319
|
+
}
|
320
|
+
}
|
321
|
+
|
322
|
+
</pre>
|
323
|
+
</div>
|
324
|
+
<p>That's it. The controller will simply pass this hash on to the model from the <code>create</code> action. The model will then handle building the <code>address</code> association for you and automatically save it when the parent (<code>person</code>) is saved.</p><h5 id="nested-form-for-a-collection-of-associated-objects">2.2.3 Nested form for a collection of associated objects</h5><p>The form code for an association collection is pretty similar to that of a single associated object:</p><div class="code_container">
|
325
|
+
<pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
|
326
|
+
<%= form_for @person do |f| %>
|
327
|
+
<%= f.text_field :name %>
|
328
|
+
|
329
|
+
<%= f.fields_for :projects do |pf| %>
|
330
|
+
<%= pf.text_field :name %>
|
331
|
+
<% end %>
|
332
|
+
<% end %>
|
333
|
+
|
334
|
+
</pre>
|
335
|
+
</div>
|
336
|
+
<p>Which generates:</p><div class="code_container">
|
337
|
+
<pre class="brush: xml; gutter: false; toolbar: false">
|
338
|
+
<form action="/people" class="new_person" id="new_person" method="post">
|
339
|
+
<input id="person_name" name="person[name]" type="text" />
|
340
|
+
|
341
|
+
<input id="person_projects_attributes_0_name" name="person[projects_attributes][0][name]" type="text" />
|
342
|
+
<input id="person_projects_attributes_1_name" name="person[projects_attributes][1][name]" type="text" />
|
343
|
+
</form>
|
344
|
+
|
345
|
+
</pre>
|
346
|
+
</div>
|
347
|
+
<p>As you can see it has generated 2 <code>project name</code> inputs, one for each new <code>project</code> that was built in the controller's <code>new</code> action. Only this time the <code>name</code> attribute of the input contains a digit as an extra namespace. This will be parsed by the Rails parameter parser as:</p><div class="code_container">
|
348
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
349
|
+
{
|
350
|
+
"person" => {
|
351
|
+
"name" => "Eloy Duran",
|
352
|
+
"projects_attributes" => {
|
353
|
+
"0" => { "name" => "Project 1" },
|
354
|
+
"1" => { "name" => "Project 2" }
|
355
|
+
}
|
356
|
+
}
|
357
|
+
}
|
358
|
+
|
359
|
+
</pre>
|
360
|
+
</div>
|
361
|
+
<p>You can basically see the <code>projects_attributes</code> hash as an array of attribute hashes, one for each model instance.</p><div class="note"><p>The reason that <code>fields_for</code> constructed a hash instead of an array is that it won't work for any form nested deeper than one level deep.</p></div><div class="info"><p>You <em>can</em> however pass an array to the writer method generated by <code>accepts_nested_attributes_for</code> if you're using plain Ruby or some other API access. See (TODO) for more info and example.</p></div>
|
362
|
+
|
363
|
+
<h3>Feedback</h3>
|
364
|
+
<p>
|
365
|
+
You're encouraged to help improve the quality of this guide.
|
366
|
+
</p>
|
367
|
+
<p>
|
368
|
+
Please contribute if you see any typos or factual errors.
|
369
|
+
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.
|
370
|
+
</p>
|
371
|
+
<p>
|
372
|
+
You may also find incomplete content, or stuff that is not up to date.
|
373
|
+
Please do add any missing documentation for master. Make sure to check
|
374
|
+
<a href="http://edgeguides.rubyonrails.org">Edge Guides</a> first to verify
|
375
|
+
if the issues are already fixed or not on the master branch.
|
376
|
+
Check the <a href="ruby_on_rails_guides_guidelines.html">Ruby on Rails Guides Guidelines</a>
|
377
|
+
for style and conventions.
|
378
|
+
</p>
|
379
|
+
<p>
|
380
|
+
If for whatever reason you spot something to fix but cannot patch it yourself, please
|
381
|
+
<a href="https://github.com/rails/rails/issues">open an issue</a>.
|
382
|
+
</p>
|
383
|
+
<p>And last but not least, any kind of discussion regarding Ruby on Rails
|
384
|
+
documentation is very welcome in the <a href="http://groups.google.com/group/rubyonrails-docs">rubyonrails-docs mailing list</a>.
|
385
|
+
</p>
|
386
|
+
</div>
|
387
|
+
</div>
|
388
|
+
</div>
|
389
|
+
|
390
|
+
<hr class="hide" />
|
391
|
+
<div id="footer">
|
392
|
+
<div class="wrapper">
|
393
|
+
<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>
|
394
|
+
<p>"Rails", "Ruby on Rails", and the Rails logo are trademarks of David Heinemeier Hansson. All rights reserved.</p>
|
395
|
+
|
396
|
+
</div>
|
397
|
+
</div>
|
398
|
+
|
399
|
+
<script type="text/javascript" src="javascripts/jquery.min.js"></script>
|
400
|
+
<script type="text/javascript" src="javascripts/responsive-tables.js"></script>
|
401
|
+
<script type="text/javascript" src="javascripts/guides.js"></script>
|
402
|
+
<script type="text/javascript" src="javascripts/syntaxhighlighter/shCore.js"></script>
|
403
|
+
<script type="text/javascript" src="javascripts/syntaxhighlighter/shBrushRuby.js"></script>
|
404
|
+
<script type="text/javascript" src="javascripts/syntaxhighlighter/shBrushXml.js"></script>
|
405
|
+
<script type="text/javascript" src="javascripts/syntaxhighlighter/shBrushSql.js"></script>
|
406
|
+
<script type="text/javascript" src="javascripts/syntaxhighlighter/shBrushPlain.js"></script>
|
407
|
+
<script type="text/javascript">
|
408
|
+
SyntaxHighlighter.all();
|
409
|
+
$(guidesIndex.bind);
|
410
|
+
</script>
|
411
|
+
</body>
|
412
|
+
</html>
|
@@ -0,0 +1,644 @@
|
|
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>The Basics of Creating Rails Plugins — 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>The Basics of Creating Rails Plugins</h2><p>A Rails plugin is either an extension or a modification of the core framework. Plugins provide:</p>
|
165
|
+
<ul>
|
166
|
+
<li>A way for developers to share bleeding-edge ideas without hurting the stable code base.</li>
|
167
|
+
<li>A segmented architecture so that units of code can be fixed or updated on their own release schedule.</li>
|
168
|
+
<li>An outlet for the core developers so that they don't have to include every cool new feature under the sun.</li>
|
169
|
+
</ul>
|
170
|
+
<p>After reading this guide, you will know:</p>
|
171
|
+
<ul>
|
172
|
+
<li>How to create a plugin from scratch.</li>
|
173
|
+
<li>How to write and run tests for the plugin.</li>
|
174
|
+
</ul>
|
175
|
+
<p>This guide describes how to build a test-driven plugin that will:</p>
|
176
|
+
<ul>
|
177
|
+
<li>Extend core Ruby classes like Hash and String.</li>
|
178
|
+
<li>Add methods to <code>ActiveRecord::Base</code> in the tradition of the <code>acts_as</code> plugins.</li>
|
179
|
+
<li>Give you information about where to put generators in your plugin.</li>
|
180
|
+
</ul>
|
181
|
+
<p>For the purpose of this guide pretend for a moment that you are an avid bird watcher.
|
182
|
+
Your favorite bird is the Yaffle, and you want to create a plugin that allows other developers to share in the Yaffle
|
183
|
+
goodness.</p>
|
184
|
+
|
185
|
+
<div id="subCol">
|
186
|
+
<h3 class="chapter"><img src="images/chapters_icon.gif" alt="" />Chapters</h3>
|
187
|
+
<ol class="chapters">
|
188
|
+
<li>
|
189
|
+
<a href="#setup">Setup</a>
|
190
|
+
|
191
|
+
<ul>
|
192
|
+
<li><a href="#generate-a-gemified-plugin.">Generate a gemified plugin.</a></li>
|
193
|
+
</ul>
|
194
|
+
</li>
|
195
|
+
<li><a href="#testing-your-newly-generated-plugin">Testing Your Newly Generated Plugin</a></li>
|
196
|
+
<li><a href="#extending-core-classes">Extending Core Classes</a></li>
|
197
|
+
<li>
|
198
|
+
<a href="#add-an-%22acts_as%22-method-to-active-record">Add an "acts_as" Method to Active Record</a>
|
199
|
+
|
200
|
+
<ul>
|
201
|
+
<li><a href="#add-a-class-method">Add a Class Method</a></li>
|
202
|
+
<li><a href="#add-an-instance-method">Add an Instance Method</a></li>
|
203
|
+
</ul>
|
204
|
+
</li>
|
205
|
+
<li><a href="#generators">Generators</a></li>
|
206
|
+
<li><a href="#publishing-your-gem">Publishing Your Gem</a></li>
|
207
|
+
<li>
|
208
|
+
<a href="#rdoc-documentation">RDoc Documentation</a>
|
209
|
+
|
210
|
+
<ul>
|
211
|
+
<li><a href="#references">References</a></li>
|
212
|
+
</ul>
|
213
|
+
</li>
|
214
|
+
</ol>
|
215
|
+
|
216
|
+
</div>
|
217
|
+
|
218
|
+
</div>
|
219
|
+
</div>
|
220
|
+
|
221
|
+
<div id="container">
|
222
|
+
<div class="wrapper">
|
223
|
+
<div id="mainCol">
|
224
|
+
<h3 id="setup">1 Setup</h3><p>Currently, Rails plugins are built as gems, <em>gemified plugins</em>. They can be shared across
|
225
|
+
different rails applications using RubyGems and Bundler if desired.</p><h4 id="generate-a-gemified-plugin.">1.1 Generate a gemified plugin.</h4><p>Rails ships with a <code>rails plugin new</code> command which creates a
|
226
|
+
skeleton for developing any kind of Rails extension with the ability
|
227
|
+
to run integration tests using a dummy Rails application. Create your
|
228
|
+
plugin with the command:</p><div class="code_container">
|
229
|
+
<pre class="brush: plain; gutter: false; toolbar: false">
|
230
|
+
$ bin/rails plugin new yaffle
|
231
|
+
|
232
|
+
</pre>
|
233
|
+
</div>
|
234
|
+
<p>See usage and options by asking for help:</p><div class="code_container">
|
235
|
+
<pre class="brush: plain; gutter: false; toolbar: false">
|
236
|
+
$ bin/rails plugin new --help
|
237
|
+
|
238
|
+
</pre>
|
239
|
+
</div>
|
240
|
+
<h3 id="testing-your-newly-generated-plugin">2 Testing Your Newly Generated Plugin</h3><p>You can navigate to the directory that contains the plugin, run the <code>bundle install</code> command
|
241
|
+
and run the one generated test using the <code>rake</code> command.</p><p>You should see:</p><div class="code_container">
|
242
|
+
<pre class="brush: plain; gutter: false; toolbar: false">
|
243
|
+
1 runs, 1 assertions, 0 failures, 0 errors, 0 skips
|
244
|
+
|
245
|
+
</pre>
|
246
|
+
</div>
|
247
|
+
<p>This will tell you that everything got generated properly and you are ready to start adding functionality.</p><h3 id="extending-core-classes">3 Extending Core Classes</h3><p>This section will explain how to add a method to String that will be available anywhere in your rails application.</p><p>In this example you will add a method to String named <code>to_squawk</code>. To begin, create a new test file with a few assertions:</p><div class="code_container">
|
248
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
249
|
+
# yaffle/test/core_ext_test.rb
|
250
|
+
|
251
|
+
require 'test_helper'
|
252
|
+
|
253
|
+
class CoreExtTest < ActiveSupport::TestCase
|
254
|
+
def test_to_squawk_prepends_the_word_squawk
|
255
|
+
assert_equal "squawk! Hello World", "Hello World".to_squawk
|
256
|
+
end
|
257
|
+
end
|
258
|
+
|
259
|
+
</pre>
|
260
|
+
</div>
|
261
|
+
<p>Run <code>rake</code> to run the test. This test should fail because we haven't implemented the <code>to_squawk</code> method:</p><div class="code_container">
|
262
|
+
<pre class="brush: plain; gutter: false; toolbar: false">
|
263
|
+
1) Error:
|
264
|
+
CoreExtTest#test_to_squawk_prepends_the_word_squawk:
|
265
|
+
NoMethodError: undefined method `to_squawk' for "Hello World":String
|
266
|
+
/path/to/yaffle/test/core_ext_test.rb:5:in `test_to_squawk_prepends_the_word_squawk'
|
267
|
+
|
268
|
+
</pre>
|
269
|
+
</div>
|
270
|
+
<p>Great - now you are ready to start development.</p><p>In <code>lib/yaffle.rb</code>, add <code>require 'yaffle/core_ext'</code>:</p><div class="code_container">
|
271
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
272
|
+
# yaffle/lib/yaffle.rb
|
273
|
+
|
274
|
+
require 'yaffle/core_ext'
|
275
|
+
|
276
|
+
module Yaffle
|
277
|
+
end
|
278
|
+
|
279
|
+
</pre>
|
280
|
+
</div>
|
281
|
+
<p>Finally, create the <code>core_ext.rb</code> file and add the <code>to_squawk</code> method:</p><div class="code_container">
|
282
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
283
|
+
# yaffle/lib/yaffle/core_ext.rb
|
284
|
+
|
285
|
+
String.class_eval do
|
286
|
+
def to_squawk
|
287
|
+
"squawk! #{self}".strip
|
288
|
+
end
|
289
|
+
end
|
290
|
+
|
291
|
+
</pre>
|
292
|
+
</div>
|
293
|
+
<p>To test that your method does what it says it does, run the unit tests with <code>rake</code> from your plugin directory.</p><div class="code_container">
|
294
|
+
<pre class="brush: plain; gutter: false; toolbar: false">
|
295
|
+
2 runs, 2 assertions, 0 failures, 0 errors, 0 skips
|
296
|
+
|
297
|
+
</pre>
|
298
|
+
</div>
|
299
|
+
<p>To see this in action, change to the test/dummy directory, fire up a console and start squawking:</p><div class="code_container">
|
300
|
+
<pre class="brush: plain; gutter: false; toolbar: false">
|
301
|
+
$ bin/rails console
|
302
|
+
>> "Hello World".to_squawk
|
303
|
+
=> "squawk! Hello World"
|
304
|
+
|
305
|
+
</pre>
|
306
|
+
</div>
|
307
|
+
<h3 id='add-an-"acts_as"-method-to-active-record'>4 Add an "acts_as" Method to Active Record</h3><p>A common pattern in plugins is to add a method called <code>acts_as_something</code> to models. In this case, you
|
308
|
+
want to write a method called <code>acts_as_yaffle</code> that adds a <code>squawk</code> method to your Active Record models.</p><p>To begin, set up your files so that you have:</p><div class="code_container">
|
309
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
310
|
+
# yaffle/test/acts_as_yaffle_test.rb
|
311
|
+
|
312
|
+
require 'test_helper'
|
313
|
+
|
314
|
+
class ActsAsYaffleTest < ActiveSupport::TestCase
|
315
|
+
end
|
316
|
+
|
317
|
+
</pre>
|
318
|
+
</div>
|
319
|
+
<div class="code_container">
|
320
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
321
|
+
# yaffle/lib/yaffle.rb
|
322
|
+
|
323
|
+
require 'yaffle/core_ext'
|
324
|
+
require 'yaffle/acts_as_yaffle'
|
325
|
+
|
326
|
+
module Yaffle
|
327
|
+
end
|
328
|
+
|
329
|
+
</pre>
|
330
|
+
</div>
|
331
|
+
<div class="code_container">
|
332
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
333
|
+
# yaffle/lib/yaffle/acts_as_yaffle.rb
|
334
|
+
|
335
|
+
module Yaffle
|
336
|
+
module ActsAsYaffle
|
337
|
+
# your code will go here
|
338
|
+
end
|
339
|
+
end
|
340
|
+
|
341
|
+
</pre>
|
342
|
+
</div>
|
343
|
+
<h4 id="add-a-class-method">4.1 Add a Class Method</h4><p>This plugin will expect that you've added a method to your model named <code>last_squawk</code>. However, the
|
344
|
+
plugin users might have already defined a method on their model named <code>last_squawk</code> that they use
|
345
|
+
for something else. This plugin will allow the name to be changed by adding a class method called <code>yaffle_text_field</code>.</p><p>To start out, write a failing test that shows the behavior you'd like:</p><div class="code_container">
|
346
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
347
|
+
# yaffle/test/acts_as_yaffle_test.rb
|
348
|
+
|
349
|
+
require 'test_helper'
|
350
|
+
|
351
|
+
class ActsAsYaffleTest < ActiveSupport::TestCase
|
352
|
+
|
353
|
+
def test_a_hickwalls_yaffle_text_field_should_be_last_squawk
|
354
|
+
assert_equal "last_squawk", Hickwall.yaffle_text_field
|
355
|
+
end
|
356
|
+
|
357
|
+
def test_a_wickwalls_yaffle_text_field_should_be_last_tweet
|
358
|
+
assert_equal "last_tweet", Wickwall.yaffle_text_field
|
359
|
+
end
|
360
|
+
|
361
|
+
end
|
362
|
+
|
363
|
+
</pre>
|
364
|
+
</div>
|
365
|
+
<p>When you run <code>rake</code>, you should see the following:</p><div class="code_container">
|
366
|
+
<pre class="brush: plain; gutter: false; toolbar: false">
|
367
|
+
1) Error:
|
368
|
+
ActsAsYaffleTest#test_a_hickwalls_yaffle_text_field_should_be_last_squawk:
|
369
|
+
NameError: uninitialized constant ActsAsYaffleTest::Hickwall
|
370
|
+
/path/to/yaffle/test/acts_as_yaffle_test.rb:6:in `test_a_hickwalls_yaffle_text_field_should_be_last_squawk'
|
371
|
+
|
372
|
+
2) Error:
|
373
|
+
ActsAsYaffleTest#test_a_wickwalls_yaffle_text_field_should_be_last_tweet:
|
374
|
+
NameError: uninitialized constant ActsAsYaffleTest::Wickwall
|
375
|
+
/path/to/yaffle/test/acts_as_yaffle_test.rb:10:in `test_a_wickwalls_yaffle_text_field_should_be_last_tweet'
|
376
|
+
|
377
|
+
4 runs, 2 assertions, 0 failures, 2 errors, 0 skips
|
378
|
+
|
379
|
+
</pre>
|
380
|
+
</div>
|
381
|
+
<p>This tells us that we don't have the necessary models (Hickwall and Wickwall) that we are trying to test.
|
382
|
+
We can easily generate these models in our "dummy" Rails application by running the following commands from the
|
383
|
+
test/dummy directory:</p><div class="code_container">
|
384
|
+
<pre class="brush: plain; gutter: false; toolbar: false">
|
385
|
+
$ cd test/dummy
|
386
|
+
$ bin/rails generate model Hickwall last_squawk:string
|
387
|
+
$ bin/rails generate model Wickwall last_squawk:string last_tweet:string
|
388
|
+
|
389
|
+
</pre>
|
390
|
+
</div>
|
391
|
+
<p>Now you can create the necessary database tables in your testing database by navigating to your dummy app
|
392
|
+
and migrating the database. First, run:</p><div class="code_container">
|
393
|
+
<pre class="brush: plain; gutter: false; toolbar: false">
|
394
|
+
$ cd test/dummy
|
395
|
+
$ bin/rake db:migrate
|
396
|
+
|
397
|
+
</pre>
|
398
|
+
</div>
|
399
|
+
<p>While you are here, change the Hickwall and Wickwall models so that they know that they are supposed to act
|
400
|
+
like yaffles.</p><div class="code_container">
|
401
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
402
|
+
# test/dummy/app/models/hickwall.rb
|
403
|
+
|
404
|
+
class Hickwall < ActiveRecord::Base
|
405
|
+
acts_as_yaffle
|
406
|
+
end
|
407
|
+
|
408
|
+
# test/dummy/app/models/wickwall.rb
|
409
|
+
|
410
|
+
class Wickwall < ActiveRecord::Base
|
411
|
+
acts_as_yaffle yaffle_text_field: :last_tweet
|
412
|
+
end
|
413
|
+
|
414
|
+
|
415
|
+
</pre>
|
416
|
+
</div>
|
417
|
+
<p>We will also add code to define the <code>acts_as_yaffle</code> method.</p><div class="code_container">
|
418
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
419
|
+
# yaffle/lib/yaffle/acts_as_yaffle.rb
|
420
|
+
module Yaffle
|
421
|
+
module ActsAsYaffle
|
422
|
+
extend ActiveSupport::Concern
|
423
|
+
|
424
|
+
included do
|
425
|
+
end
|
426
|
+
|
427
|
+
module ClassMethods
|
428
|
+
def acts_as_yaffle(options = {})
|
429
|
+
# your code will go here
|
430
|
+
end
|
431
|
+
end
|
432
|
+
end
|
433
|
+
end
|
434
|
+
|
435
|
+
ActiveRecord::Base.send :include, Yaffle::ActsAsYaffle
|
436
|
+
|
437
|
+
</pre>
|
438
|
+
</div>
|
439
|
+
<p>You can then return to the root directory (<code>cd ../..</code>) of your plugin and rerun the tests using <code>rake</code>.</p><div class="code_container">
|
440
|
+
<pre class="brush: plain; gutter: false; toolbar: false">
|
441
|
+
1) Error:
|
442
|
+
ActsAsYaffleTest#test_a_hickwalls_yaffle_text_field_should_be_last_squawk:
|
443
|
+
NoMethodError: undefined method `yaffle_text_field' for #<Class:0x007fd105e3b218>
|
444
|
+
activerecord (4.1.5) lib/active_record/dynamic_matchers.rb:26:in `method_missing'
|
445
|
+
/path/to/yaffle/test/acts_as_yaffle_test.rb:6:in `test_a_hickwalls_yaffle_text_field_should_be_last_squawk'
|
446
|
+
|
447
|
+
2) Error:
|
448
|
+
ActsAsYaffleTest#test_a_wickwalls_yaffle_text_field_should_be_last_tweet:
|
449
|
+
NoMethodError: undefined method `yaffle_text_field' for #<Class:0x007fd105e409c0>
|
450
|
+
activerecord (4.1.5) lib/active_record/dynamic_matchers.rb:26:in `method_missing'
|
451
|
+
/path/to/yaffle/test/acts_as_yaffle_test.rb:10:in `test_a_wickwalls_yaffle_text_field_should_be_last_tweet'
|
452
|
+
|
453
|
+
4 runs, 2 assertions, 0 failures, 2 errors, 0 skips
|
454
|
+
|
455
|
+
|
456
|
+
</pre>
|
457
|
+
</div>
|
458
|
+
<p>Getting closer... Now we will implement the code of the <code>acts_as_yaffle</code> method to make the tests pass.</p><div class="code_container">
|
459
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
460
|
+
# yaffle/lib/yaffle/acts_as_yaffle.rb
|
461
|
+
|
462
|
+
module Yaffle
|
463
|
+
module ActsAsYaffle
|
464
|
+
extend ActiveSupport::Concern
|
465
|
+
|
466
|
+
included do
|
467
|
+
end
|
468
|
+
|
469
|
+
module ClassMethods
|
470
|
+
def acts_as_yaffle(options = {})
|
471
|
+
cattr_accessor :yaffle_text_field
|
472
|
+
self.yaffle_text_field = (options[:yaffle_text_field] || :last_squawk).to_s
|
473
|
+
end
|
474
|
+
end
|
475
|
+
end
|
476
|
+
end
|
477
|
+
|
478
|
+
ActiveRecord::Base.send :include, Yaffle::ActsAsYaffle
|
479
|
+
|
480
|
+
</pre>
|
481
|
+
</div>
|
482
|
+
<p>When you run <code>rake</code>, you should see the tests all pass:</p><div class="code_container">
|
483
|
+
<pre class="brush: plain; gutter: false; toolbar: false">
|
484
|
+
4 runs, 4 assertions, 0 failures, 0 errors, 0 skips
|
485
|
+
|
486
|
+
</pre>
|
487
|
+
</div>
|
488
|
+
<h4 id="add-an-instance-method">4.2 Add an Instance Method</h4><p>This plugin will add a method named 'squawk' to any Active Record object that calls 'acts_as_yaffle'. The 'squawk'
|
489
|
+
method will simply set the value of one of the fields in the database.</p><p>To start out, write a failing test that shows the behavior you'd like:</p><div class="code_container">
|
490
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
491
|
+
# yaffle/test/acts_as_yaffle_test.rb
|
492
|
+
require 'test_helper'
|
493
|
+
|
494
|
+
class ActsAsYaffleTest < ActiveSupport::TestCase
|
495
|
+
|
496
|
+
def test_a_hickwalls_yaffle_text_field_should_be_last_squawk
|
497
|
+
assert_equal "last_squawk", Hickwall.yaffle_text_field
|
498
|
+
end
|
499
|
+
|
500
|
+
def test_a_wickwalls_yaffle_text_field_should_be_last_tweet
|
501
|
+
assert_equal "last_tweet", Wickwall.yaffle_text_field
|
502
|
+
end
|
503
|
+
|
504
|
+
def test_hickwalls_squawk_should_populate_last_squawk
|
505
|
+
hickwall = Hickwall.new
|
506
|
+
hickwall.squawk("Hello World")
|
507
|
+
assert_equal "squawk! Hello World", hickwall.last_squawk
|
508
|
+
end
|
509
|
+
|
510
|
+
def test_wickwalls_squawk_should_populate_last_tweet
|
511
|
+
wickwall = Wickwall.new
|
512
|
+
wickwall.squawk("Hello World")
|
513
|
+
assert_equal "squawk! Hello World", wickwall.last_tweet
|
514
|
+
end
|
515
|
+
end
|
516
|
+
|
517
|
+
</pre>
|
518
|
+
</div>
|
519
|
+
<p>Run the test to make sure the last two tests fail with an error that contains "NoMethodError: undefined method `squawk'",
|
520
|
+
then update 'acts_as_yaffle.rb' to look like this:</p><div class="code_container">
|
521
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
522
|
+
# yaffle/lib/yaffle/acts_as_yaffle.rb
|
523
|
+
|
524
|
+
module Yaffle
|
525
|
+
module ActsAsYaffle
|
526
|
+
extend ActiveSupport::Concern
|
527
|
+
|
528
|
+
included do
|
529
|
+
end
|
530
|
+
|
531
|
+
module ClassMethods
|
532
|
+
def acts_as_yaffle(options = {})
|
533
|
+
cattr_accessor :yaffle_text_field
|
534
|
+
self.yaffle_text_field = (options[:yaffle_text_field] || :last_squawk).to_s
|
535
|
+
|
536
|
+
include Yaffle::ActsAsYaffle::LocalInstanceMethods
|
537
|
+
end
|
538
|
+
end
|
539
|
+
|
540
|
+
module LocalInstanceMethods
|
541
|
+
def squawk(string)
|
542
|
+
write_attribute(self.class.yaffle_text_field, string.to_squawk)
|
543
|
+
end
|
544
|
+
end
|
545
|
+
end
|
546
|
+
end
|
547
|
+
|
548
|
+
ActiveRecord::Base.send :include, Yaffle::ActsAsYaffle
|
549
|
+
|
550
|
+
</pre>
|
551
|
+
</div>
|
552
|
+
<p>Run <code>rake</code> one final time and you should see:</p><div class="code_container">
|
553
|
+
<pre class="brush: plain; gutter: false; toolbar: false">
|
554
|
+
6 runs, 6 assertions, 0 failures, 0 errors, 0 skips
|
555
|
+
|
556
|
+
</pre>
|
557
|
+
</div>
|
558
|
+
<div class="note"><p>The use of <code>write_attribute</code> to write to the field in model is just one example of how a plugin can interact with the model, and will not always be the right method to use. For example, you could also use:</p></div><div class="code_container">
|
559
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
560
|
+
send("#{self.class.yaffle_text_field}=", string.to_squawk)
|
561
|
+
|
562
|
+
</pre>
|
563
|
+
</div>
|
564
|
+
<h3 id="generators">5 Generators</h3><p>Generators can be included in your gem simply by creating them in a lib/generators directory of your plugin. More information about
|
565
|
+
the creation of generators can be found in the <a href="generators.html">Generators Guide</a></p><h3 id="publishing-your-gem">6 Publishing Your Gem</h3><p>Gem plugins currently in development can easily be shared from any Git repository. To share the Yaffle gem with others, simply
|
566
|
+
commit the code to a Git repository (like GitHub) and add a line to the Gemfile of the application in question:</p><div class="code_container">
|
567
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
568
|
+
gem 'yaffle', git: 'git://github.com/yaffle_watcher/yaffle.git'
|
569
|
+
|
570
|
+
</pre>
|
571
|
+
</div>
|
572
|
+
<p>After running <code>bundle install</code>, your gem functionality will be available to the application.</p><p>When the gem is ready to be shared as a formal release, it can be published to <a href="http://www.rubygems.org">RubyGems</a>.
|
573
|
+
For more information about publishing gems to RubyGems, see: <a href="http://blog.thepete.net/2010/11/creating-and-publishing-your-first-ruby.html">Creating and Publishing Your First Ruby Gem</a>.</p><h3 id="rdoc-documentation">7 RDoc Documentation</h3><p>Once your plugin is stable and you are ready to deploy, do everyone else a favor and document it! Luckily, writing documentation for your plugin is easy.</p><p>The first step is to update the README file with detailed information about how to use your plugin. A few key things to include are:</p>
|
574
|
+
<ul>
|
575
|
+
<li>Your name</li>
|
576
|
+
<li>How to install</li>
|
577
|
+
<li>How to add the functionality to the app (several examples of common use cases)</li>
|
578
|
+
<li>Warnings, gotchas or tips that might help users and save them time</li>
|
579
|
+
</ul>
|
580
|
+
<p>Once your README is solid, go through and add rdoc comments to all of the methods that developers will use. It's also customary to add '#:nodoc:' comments to those parts of the code that are not included in the public API.</p><p>Once your comments are good to go, navigate to your plugin directory and run:</p><div class="code_container">
|
581
|
+
<pre class="brush: plain; gutter: false; toolbar: false">
|
582
|
+
$ bin/rake rdoc
|
583
|
+
|
584
|
+
</pre>
|
585
|
+
</div>
|
586
|
+
<h4 id="references">7.1 References</h4>
|
587
|
+
<ul>
|
588
|
+
<li><a href="https://github.com/radar/guides/blob/master/gem-development.md">Developing a RubyGem using Bundler</a></li>
|
589
|
+
<li><a href="http://yehudakatz.com/2010/04/02/using-gemspecs-as-intended/">Using .gemspecs as Intended</a></li>
|
590
|
+
<li><a href="http://guides.rubygems.org/specification-reference/">Gemspec Reference</a></li>
|
591
|
+
<li><a href="http://www.intridea.com/blog/2008/6/11/gemplugins-a-brief-introduction-to-the-future-of-rails-plugins">GemPlugins: A Brief Introduction to the Future of Rails Plugins</a></li>
|
592
|
+
</ul>
|
593
|
+
|
594
|
+
|
595
|
+
<h3>Feedback</h3>
|
596
|
+
<p>
|
597
|
+
You're encouraged to help improve the quality of this guide.
|
598
|
+
</p>
|
599
|
+
<p>
|
600
|
+
Please contribute if you see any typos or factual errors.
|
601
|
+
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.
|
602
|
+
</p>
|
603
|
+
<p>
|
604
|
+
You may also find incomplete content, or stuff that is not up to date.
|
605
|
+
Please do add any missing documentation for master. Make sure to check
|
606
|
+
<a href="http://edgeguides.rubyonrails.org">Edge Guides</a> first to verify
|
607
|
+
if the issues are already fixed or not on the master branch.
|
608
|
+
Check the <a href="ruby_on_rails_guides_guidelines.html">Ruby on Rails Guides Guidelines</a>
|
609
|
+
for style and conventions.
|
610
|
+
</p>
|
611
|
+
<p>
|
612
|
+
If for whatever reason you spot something to fix but cannot patch it yourself, please
|
613
|
+
<a href="https://github.com/rails/rails/issues">open an issue</a>.
|
614
|
+
</p>
|
615
|
+
<p>And last but not least, any kind of discussion regarding Ruby on Rails
|
616
|
+
documentation is very welcome in the <a href="http://groups.google.com/group/rubyonrails-docs">rubyonrails-docs mailing list</a>.
|
617
|
+
</p>
|
618
|
+
</div>
|
619
|
+
</div>
|
620
|
+
</div>
|
621
|
+
|
622
|
+
<hr class="hide" />
|
623
|
+
<div id="footer">
|
624
|
+
<div class="wrapper">
|
625
|
+
<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>
|
626
|
+
<p>"Rails", "Ruby on Rails", and the Rails logo are trademarks of David Heinemeier Hansson. All rights reserved.</p>
|
627
|
+
|
628
|
+
</div>
|
629
|
+
</div>
|
630
|
+
|
631
|
+
<script type="text/javascript" src="javascripts/jquery.min.js"></script>
|
632
|
+
<script type="text/javascript" src="javascripts/responsive-tables.js"></script>
|
633
|
+
<script type="text/javascript" src="javascripts/guides.js"></script>
|
634
|
+
<script type="text/javascript" src="javascripts/syntaxhighlighter/shCore.js"></script>
|
635
|
+
<script type="text/javascript" src="javascripts/syntaxhighlighter/shBrushRuby.js"></script>
|
636
|
+
<script type="text/javascript" src="javascripts/syntaxhighlighter/shBrushXml.js"></script>
|
637
|
+
<script type="text/javascript" src="javascripts/syntaxhighlighter/shBrushSql.js"></script>
|
638
|
+
<script type="text/javascript" src="javascripts/syntaxhighlighter/shBrushPlain.js"></script>
|
639
|
+
<script type="text/javascript">
|
640
|
+
SyntaxHighlighter.all();
|
641
|
+
$(guidesIndex.bind);
|
642
|
+
</script>
|
643
|
+
</body>
|
644
|
+
</html>
|