railties 3.0.0.beta → 3.0.0.beta2
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +15 -1
- data/README +9 -9
- data/bin/rails +28 -9
- data/guides/images/challenge.png +0 -0
- data/guides/images/edge_badge.png +0 -0
- data/guides/images/posts_index.png +0 -0
- data/guides/images/rails_welcome.png +0 -0
- data/guides/rails_guides.rb +9 -22
- data/guides/rails_guides/generator.rb +79 -50
- data/guides/rails_guides/textile_extensions.rb +3 -3
- data/guides/source/2_2_release_notes.textile +1 -1
- data/guides/source/2_3_release_notes.textile +1 -1
- data/guides/source/3_0_release_notes.textile +46 -38
- data/guides/source/action_controller_overview.textile +2 -2
- data/guides/source/action_mailer_basics.textile +4 -4
- data/guides/source/action_view_overview.textile +2 -2
- data/guides/source/active_record_querying.textile +73 -95
- data/guides/source/active_support_core_extensions.textile +993 -85
- data/guides/source/activerecord_validations_callbacks.textile +3 -3
- data/guides/source/caching_with_rails.textile +1 -1
- data/guides/source/command_line.textile +90 -88
- data/guides/source/configuring.textile +10 -10
- data/guides/source/contribute.textile +2 -2
- data/guides/source/contributing_to_rails.textile +14 -7
- data/guides/source/credits.textile.erb +8 -0
- data/guides/source/debugging_rails_applications.textile +6 -6
- data/guides/source/form_helpers.textile +1 -1
- data/guides/source/generators.textile +14 -14
- data/guides/source/getting_started.textile +634 -500
- data/guides/source/index.textile.erb +16 -1
- data/guides/source/layout.html.erb +7 -1
- data/guides/source/layouts_and_rendering.textile +299 -71
- data/guides/source/migrations.textile +5 -5
- data/guides/source/performance_testing.textile +8 -8
- data/guides/source/plugins.textile +26 -24
- data/guides/source/rails_on_rack.textile +5 -5
- data/guides/source/routing.textile +119 -117
- data/guides/source/security.textile +1 -1
- data/guides/source/testing.textile +4 -4
- data/lib/rails.rb +4 -1
- data/lib/rails/application.rb +44 -7
- data/lib/rails/application/bootstrap.rb +2 -14
- data/lib/rails/application/configuration.rb +69 -5
- data/lib/rails/application/finisher.rb +2 -3
- data/lib/rails/application/metal_loader.rb +1 -1
- data/lib/rails/application/routes_reloader.rb +1 -1
- data/lib/rails/backtrace_cleaner.rb +0 -11
- data/lib/rails/commands.rb +7 -6
- data/lib/rails/commands/application.rb +1 -1
- data/lib/rails/commands/console.rb +1 -1
- data/lib/rails/commands/dbconsole.rb +12 -0
- data/lib/rails/commands/destroy.rb +2 -2
- data/lib/rails/commands/generate.rb +2 -2
- data/lib/rails/commands/performance/benchmarker.rb +2 -2
- data/lib/rails/commands/performance/profiler.rb +2 -2
- data/lib/rails/commands/plugin.rb +6 -6
- data/lib/rails/commands/runner.rb +2 -0
- data/lib/rails/commands/server.rb +23 -8
- data/lib/rails/configuration.rb +2 -84
- data/lib/rails/console/app.rb +4 -3
- data/lib/rails/console/helpers.rb +3 -1
- data/lib/rails/engine.rb +107 -12
- data/lib/rails/engine/configuration.rb +8 -2
- data/lib/rails/generators.rb +22 -7
- data/lib/rails/generators/actions.rb +16 -6
- data/lib/rails/generators/base.rb +15 -8
- data/lib/rails/generators/erb.rb +21 -0
- data/lib/{generators → rails/generators}/erb/controller/controller_generator.rb +4 -5
- data/lib/{generators → rails/generators}/erb/controller/templates/view.html.erb +0 -0
- data/lib/rails/generators/erb/mailer/mailer_generator.rb +13 -0
- data/lib/{generators → rails/generators}/erb/mailer/templates/view.text.erb +0 -0
- data/lib/{generators → rails/generators}/erb/scaffold/scaffold_generator.rb +14 -26
- data/lib/{generators → rails/generators}/erb/scaffold/templates/_form.html.erb +1 -1
- data/lib/{generators → rails/generators}/erb/scaffold/templates/edit.html.erb +0 -0
- data/lib/{generators → rails/generators}/erb/scaffold/templates/index.html.erb +1 -1
- data/lib/{generators → rails/generators}/erb/scaffold/templates/layout.html.erb +1 -0
- data/lib/{generators → rails/generators}/erb/scaffold/templates/new.html.erb +0 -0
- data/lib/{generators → rails/generators}/erb/scaffold/templates/show.html.erb +0 -0
- data/lib/rails/generators/named_base.rb +4 -0
- data/lib/{generators → rails/generators}/rails/app/USAGE +0 -0
- data/lib/{generators → rails/generators}/rails/app/app_generator.rb +21 -7
- data/lib/rails/generators/rails/app/templates/Gemfile +34 -0
- data/lib/{generators → rails/generators}/rails/app/templates/README +9 -8
- data/lib/{generators → rails/generators}/rails/app/templates/Rakefile +0 -0
- data/lib/{generators → rails/generators}/rails/app/templates/app/controllers/application_controller.rb +0 -0
- data/lib/{generators → rails/generators}/rails/app/templates/app/helpers/application_helper.rb +0 -0
- data/lib/{generators → rails/generators}/rails/app/templates/app/models/.empty_directory +0 -0
- data/lib/{generators → rails/generators}/rails/app/templates/app/views/layouts/.empty_directory +0 -0
- data/lib/{generators → rails/generators}/rails/app/templates/config.ru +0 -0
- data/lib/{generators → rails/generators}/rails/app/templates/config/application.rb +4 -3
- data/lib/rails/generators/rails/app/templates/config/boot.rb +14 -0
- data/lib/{generators → rails/generators}/rails/app/templates/config/databases/frontbase.yml +0 -0
- data/lib/{generators → rails/generators}/rails/app/templates/config/databases/ibm_db.yml +0 -0
- data/lib/{generators → rails/generators}/rails/app/templates/config/databases/mysql.yml +0 -0
- data/lib/{generators → rails/generators}/rails/app/templates/config/databases/oracle.yml +0 -0
- data/lib/{generators → rails/generators}/rails/app/templates/config/databases/postgresql.yml +0 -0
- data/lib/{generators → rails/generators}/rails/app/templates/config/databases/sqlite3.yml +0 -0
- data/lib/{generators → rails/generators}/rails/app/templates/config/environment.rb +0 -0
- data/lib/{generators → rails/generators}/rails/app/templates/config/environments/development.rb.tt +0 -0
- data/lib/{generators → rails/generators}/rails/app/templates/config/environments/production.rb.tt +9 -0
- data/lib/{generators → rails/generators}/rails/app/templates/config/environments/test.rb.tt +0 -0
- data/lib/{generators → rails/generators}/rails/app/templates/config/initializers/backtrace_silencers.rb +0 -0
- data/lib/{generators → rails/generators}/rails/app/templates/config/initializers/cookie_verification_secret.rb.tt +1 -1
- data/lib/{generators → rails/generators}/rails/app/templates/config/initializers/inflections.rb +0 -0
- data/lib/{generators → rails/generators}/rails/app/templates/config/initializers/mime_types.rb +0 -0
- data/lib/rails/generators/rails/app/templates/config/initializers/session_store.rb.tt +10 -0
- data/lib/{generators → rails/generators}/rails/app/templates/config/locales/en.yml +0 -0
- data/lib/{generators → rails/generators}/rails/app/templates/config/routes.rb +0 -0
- data/lib/{generators → rails/generators}/rails/app/templates/db/seeds.rb +0 -0
- data/lib/{generators → rails/generators}/rails/app/templates/doc/README_FOR_APP +0 -0
- data/lib/{generators → rails/generators}/rails/app/templates/gitignore +0 -0
- data/lib/{generators → rails/generators}/rails/app/templates/public/404.html +0 -0
- data/lib/{generators → rails/generators}/rails/app/templates/public/422.html +0 -0
- data/lib/{generators → rails/generators}/rails/app/templates/public/500.html +0 -0
- data/lib/{generators → rails/generators}/rails/app/templates/public/favicon.ico +0 -0
- data/lib/{generators → rails/generators}/rails/app/templates/public/images/rails.png +0 -0
- data/lib/{generators → rails/generators}/rails/app/templates/public/index.html +17 -17
- data/lib/{generators → rails/generators}/rails/app/templates/public/javascripts/application.js +0 -0
- data/lib/{generators → rails/generators}/rails/app/templates/public/javascripts/controls.js +5 -3
- data/lib/{generators → rails/generators}/rails/app/templates/public/javascripts/dragdrop.js +7 -6
- data/lib/{generators → rails/generators}/rails/app/templates/public/javascripts/effects.js +8 -13
- data/lib/{generators → rails/generators}/rails/app/templates/public/javascripts/prototype.js +1573 -1019
- data/lib/{generators → rails/generators}/rails/app/templates/public/javascripts/rails.js +1 -2
- data/lib/{generators → rails/generators}/rails/app/templates/public/robots.txt +0 -0
- data/lib/{generators → rails/generators}/rails/app/templates/public/stylesheets/.empty_directory +0 -0
- data/lib/{generators → rails/generators}/rails/app/templates/script/rails +0 -1
- data/lib/{generators → rails/generators}/rails/app/templates/test/fixtures/.empty_directory +0 -0
- data/lib/{generators → rails/generators}/rails/app/templates/test/functional/.empty_directory +0 -0
- data/lib/{generators → rails/generators}/rails/app/templates/test/integration/.empty_directory +0 -0
- data/lib/{generators → rails/generators}/rails/app/templates/test/performance/browsing_test.rb +1 -1
- data/lib/{generators → rails/generators}/rails/app/templates/test/test_helper.rb +1 -1
- data/lib/{generators → rails/generators}/rails/app/templates/test/unit/.empty_directory +0 -0
- data/lib/{generators → rails/generators}/rails/controller/USAGE +1 -1
- data/lib/{generators → rails/generators}/rails/controller/controller_generator.rb +6 -0
- data/lib/{generators → rails/generators}/rails/controller/templates/controller.rb +0 -0
- data/lib/{generators → rails/generators}/rails/generator/USAGE +1 -1
- data/lib/{generators → rails/generators}/rails/generator/generator_generator.rb +0 -0
- data/lib/{generators → rails/generators}/rails/generator/templates/%file_name%_generator.rb.tt +0 -0
- data/lib/{generators → rails/generators}/rails/generator/templates/USAGE.tt +1 -1
- data/lib/{generators → rails/generators}/rails/generator/templates/templates/.empty_directory +0 -0
- data/lib/{generators → rails/generators}/rails/helper/USAGE +1 -1
- data/lib/{generators → rails/generators}/rails/helper/helper_generator.rb +0 -0
- data/lib/{generators → rails/generators}/rails/helper/templates/helper.rb +0 -0
- data/lib/{generators → rails/generators}/rails/integration_test/USAGE +1 -1
- data/lib/{generators → rails/generators}/rails/integration_test/integration_test_generator.rb +0 -0
- data/lib/{generators → rails/generators}/rails/mailer/USAGE +1 -1
- data/lib/{generators → rails/generators}/rails/mailer/mailer_generator.rb +0 -0
- data/lib/{generators → rails/generators}/rails/mailer/templates/mailer.rb +0 -0
- data/lib/{generators → rails/generators}/rails/metal/USAGE +1 -1
- data/lib/{generators → rails/generators}/rails/metal/metal_generator.rb +0 -0
- data/lib/{generators → rails/generators}/rails/metal/templates/metal.rb +0 -0
- data/lib/{generators → rails/generators}/rails/migration/USAGE +2 -2
- data/lib/{generators → rails/generators}/rails/migration/migration_generator.rb +0 -0
- data/lib/{generators → rails/generators}/rails/model/USAGE +2 -2
- data/lib/{generators → rails/generators}/rails/model/model_generator.rb +0 -0
- data/lib/{generators → rails/generators}/rails/observer/USAGE +1 -1
- data/lib/{generators → rails/generators}/rails/observer/observer_generator.rb +0 -0
- data/lib/{generators → rails/generators}/rails/performance_test/USAGE +1 -1
- data/lib/{generators → rails/generators}/rails/performance_test/performance_test_generator.rb +0 -0
- data/lib/{generators → rails/generators}/rails/plugin/USAGE +1 -1
- data/lib/{generators → rails/generators}/rails/plugin/plugin_generator.rb +1 -1
- data/lib/{generators → rails/generators}/rails/plugin/templates/MIT-LICENSE.tt +0 -0
- data/lib/{generators → rails/generators}/rails/plugin/templates/README.tt +0 -0
- data/lib/rails/generators/rails/plugin/templates/Rakefile.tt +23 -0
- data/lib/{generators → rails/generators}/rails/plugin/templates/init.rb +0 -0
- data/lib/{generators → rails/generators}/rails/plugin/templates/install.rb +0 -0
- data/lib/{generators → rails/generators}/rails/plugin/templates/lib/%file_name%.rb.tt +0 -0
- data/lib/{generators → rails/generators}/rails/plugin/templates/lib/tasks/%file_name%_tasks.rake.tt +0 -0
- data/lib/{generators → rails/generators}/rails/plugin/templates/uninstall.rb +0 -0
- data/lib/{generators → rails/generators}/rails/resource/USAGE +3 -3
- data/lib/{generators → rails/generators}/rails/resource/resource_generator.rb +3 -1
- data/lib/{generators → rails/generators}/rails/scaffold/USAGE +4 -4
- data/lib/{generators → rails/generators}/rails/scaffold/scaffold_generator.rb +1 -1
- data/lib/{generators → rails/generators}/rails/scaffold_controller/USAGE +1 -1
- data/lib/{generators → rails/generators}/rails/scaffold_controller/scaffold_controller_generator.rb +0 -0
- data/lib/{generators → rails/generators}/rails/scaffold_controller/templates/controller.rb +2 -2
- data/lib/{generators → rails/generators}/rails/session_migration/USAGE +1 -1
- data/lib/{generators → rails/generators}/rails/session_migration/session_migration_generator.rb +0 -0
- data/lib/{generators → rails/generators}/rails/stylesheets/USAGE +1 -1
- data/lib/{generators → rails/generators}/rails/stylesheets/stylesheets_generator.rb +0 -0
- data/lib/{generators → rails/generators}/rails/stylesheets/templates/scaffold.css +4 -0
- data/lib/{generators → rails/generators}/test_unit.rb +0 -0
- data/lib/{generators → rails/generators}/test_unit/controller/controller_generator.rb +2 -1
- data/lib/rails/generators/test_unit/controller/templates/functional_test.rb +18 -0
- data/lib/{generators → rails/generators}/test_unit/helper/helper_generator.rb +1 -1
- data/lib/{generators → rails/generators}/test_unit/helper/templates/helper_test.rb +0 -0
- data/lib/{generators → rails/generators}/test_unit/integration/integration_generator.rb +1 -1
- data/lib/{generators → rails/generators}/test_unit/integration/templates/integration_test.rb +0 -0
- data/lib/{generators → rails/generators}/test_unit/mailer/mailer_generator.rb +1 -8
- data/lib/rails/generators/test_unit/mailer/templates/functional_test.rb +20 -0
- data/lib/{generators → rails/generators}/test_unit/model/model_generator.rb +1 -1
- data/lib/{generators → rails/generators}/test_unit/model/templates/fixtures.yml +0 -0
- data/lib/{generators → rails/generators}/test_unit/model/templates/unit_test.rb +0 -0
- data/lib/{generators → rails/generators}/test_unit/observer/observer_generator.rb +1 -1
- data/lib/{generators → rails/generators}/test_unit/observer/templates/unit_test.rb +0 -0
- data/lib/{generators → rails/generators}/test_unit/performance/performance_generator.rb +1 -1
- data/lib/{generators → rails/generators}/test_unit/performance/templates/performance_test.rb +0 -0
- data/lib/{generators → rails/generators}/test_unit/plugin/plugin_generator.rb +1 -1
- data/lib/{generators → rails/generators}/test_unit/plugin/templates/%file_name%_test.rb.tt +0 -0
- data/lib/{generators → rails/generators}/test_unit/plugin/templates/test_helper.rb +0 -0
- data/lib/{generators → rails/generators}/test_unit/scaffold/scaffold_generator.rb +1 -1
- data/lib/{generators → rails/generators}/test_unit/scaffold/templates/functional_test.rb +9 -5
- data/{builtin/rails_info → lib}/rails/info.rb +0 -0
- data/{builtin/rails_info → lib}/rails/info_controller.rb +0 -0
- data/{builtin/routes.rb → lib/rails/info_routes.rb} +2 -2
- data/lib/rails/{subscriber.rb → log_subscriber.rb} +27 -27
- data/lib/rails/{subscriber → log_subscriber}/test_helper.rb +15 -16
- data/lib/rails/plugin.rb +31 -8
- data/lib/rails/rack/debugger.rb +3 -1
- data/lib/rails/rack/logger.rb +4 -4
- data/lib/rails/railtie.rb +179 -16
- data/lib/rails/railtie/configuration.rb +56 -1
- data/lib/rails/tasks/documentation.rake +38 -20
- data/lib/rails/tasks/framework.rake +16 -9
- data/lib/rails/tasks/misc.rake +3 -5
- data/lib/rails/tasks/routes.rake +2 -2
- data/lib/rails/test_help.rb +21 -1
- data/lib/rails/test_unit/railtie.rb +1 -3
- data/lib/rails/version.rb +3 -2
- metadata +199 -171
- data/builtin/rails_info/rails/info_helper.rb +0 -2
- data/lib/generators/erb.rb +0 -8
- data/lib/generators/erb/mailer/mailer_generator.rb +0 -20
- data/lib/generators/rails/app/templates/Gemfile +0 -34
- data/lib/generators/rails/app/templates/config/boot.rb +0 -17
- data/lib/generators/rails/app/templates/config/initializers/session_store.rb.tt +0 -15
- data/lib/generators/rails/model_subclass/model_subclass_generator.rb +0 -12
- data/lib/generators/rails/plugin/templates/Rakefile.tt +0 -10
- data/lib/generators/test_unit/controller/templates/functional_test.rb +0 -8
- data/lib/generators/test_unit/mailer/templates/fixture +0 -3
- data/lib/generators/test_unit/mailer/templates/functional_test.rb +0 -22
- data/lib/rails/railties_path.rb +0 -1
@@ -11,8 +11,8 @@ h3. How to Contribute?
|
|
11
11
|
* Guides are written in Textile, and reside at railties/guides/source in the docrails project.
|
12
12
|
* All images are in the railties/guides/images directory.
|
13
13
|
* Sample format : "Active Record Associations":http://github.com/lifo/docrails/blob/3e56a3832415476fdd1cb963980d0ae390ac1ed3/railties/guides/source/association_basics.textile
|
14
|
-
* Sample output : "Active Record Associations":
|
15
|
-
* You can build the Guides during testing by running +rake
|
14
|
+
* Sample output : "Active Record Associations":association_basics.html
|
15
|
+
* You can build the Guides during testing by running +rake generate_guides+ in the +railties+ directory.
|
16
16
|
|
17
17
|
h3. What to Contribute?
|
18
18
|
|
@@ -76,21 +76,28 @@ TIP: You may want to "put your git branch name in your shell prompt":http://gith
|
|
76
76
|
h4. Set up and Run the Tests
|
77
77
|
|
78
78
|
All of the Rails tests must pass with any code you submit, otherwise you have no chance of getting code accepted. This means you need to be able to run the tests. Rails needs the +mocha+ gem for running some tests, so install it with:
|
79
|
+
|
79
80
|
<shell>
|
80
81
|
gem install mocha
|
81
82
|
</shell>
|
82
83
|
|
83
|
-
For the tests that touch the database, this means creating
|
84
|
+
For the tests that touch the database, this means creating test databases. If you're using MySQL, create a user named +rails+ with privileges on the test databases.
|
85
|
+
|
86
|
+
<shell>
|
87
|
+
mysql> GRANT ALL PRIVILEGES ON activerecord_unittest.*
|
88
|
+
to 'rails'@'localhost';
|
89
|
+
mysql> GRANT ALL PRIVILEGES ON activerecord_unittest2.*
|
90
|
+
to 'rails'@'localhost';
|
91
|
+
</shell>
|
92
|
+
|
93
|
+
Enter this from the +activerecord+ directory to create the test databases:
|
84
94
|
|
85
95
|
<shell>
|
86
|
-
mysql
|
87
|
-
mysql> create database activerecord_unittest2;
|
88
|
-
mysql> GRANT ALL PRIVILEGES ON activerecord_unittest.*
|
89
|
-
to 'rails'@'localhost';
|
90
|
-
mysql> GRANT ALL PRIVILEGES ON activerecord_unittest2.*
|
91
|
-
to 'rails'@'localhost';
|
96
|
+
rake mysql:build_databases
|
92
97
|
</shell>
|
93
98
|
|
99
|
+
NOTE: Using the rake task to create the test databases ensures they have the correct character set and collation.
|
100
|
+
|
94
101
|
If you’re using another database, check the files under +activerecord/test/connections+ in the Rails source code for default connection information. You can edit these files if you _must_ on your machine to provide different credentials, but obviously you should not push any such changes back to Rails.
|
95
102
|
|
96
103
|
Now if you go back to the root of the Rails source on your machine and run +rake+ with no parameters, you should see every test in all of the Rails components pass. If you want to run the all ActiveRecord tests (or just a single one) with another database adapter, enter this from the +activerecord+ directory:
|
@@ -43,6 +43,10 @@ p. We'd like to thank the following people for their tireless contributions to t
|
|
43
43
|
Cássio Marques is a Brazilian software developer working with different programming languages such as Ruby, JavaScript, CPP and Java, as an independent consultant. He blogs at "/* CODIFICANDO */":http://cassiomarques.wordpress.com, which is mainly written in Portuguese, but will soon get a new section for posts with English translation.
|
44
44
|
<% end %>
|
45
45
|
|
46
|
+
<% author('James Miller', 'bensie') do %>
|
47
|
+
James Miller is a software developer for "JK Tech":http://www.jk-tech.com in San Diego, CA. Find me on GitHub, Gmail, Twitter, and Freenode as bensie.
|
48
|
+
<% end %>
|
49
|
+
|
46
50
|
<% author('Emilio Tagua', 'miloops') do %>
|
47
51
|
Emilio Tagua -- a.k.a. miloops -- is an Argentinian entrepreneur, developer, open source contributor and Rails evangelist. Cofounder of "Eventioz":http://eventioz.com. He has been using Rails since 2006 and contributing since early 2008. Can be found at gmail, twitter, freenode, everywhere as miloops.
|
48
52
|
<% end %>
|
@@ -50,3 +54,7 @@ p. We'd like to thank the following people for their tireless contributions to t
|
|
50
54
|
<% author('Heiko Webers', 'hawe') do %>
|
51
55
|
Heiko Webers is the founder of "bauland42":http://www.bauland42.de, a German web application security consulting and development company focused on Ruby on Rails. He blogs at the "Ruby on Rails Security Project":http://www.rorsecurity.info. After 10 years of desktop application development, Heiko has rarely looked back.
|
52
56
|
<% end %>
|
57
|
+
|
58
|
+
<% author('Mikel Lindsaar', 'raasdnil') do %>
|
59
|
+
Mikel Lindsaar has been working with Rails since 2006 and is the author of the Ruby Mail gem and core contributor (he helped re-write ActionMailer's API). Mikel has a "blog":http://lindsaar.net/ and "tweets":http://twitter.com/raasdnil.
|
60
|
+
<% end %>
|
@@ -247,9 +247,9 @@ If you see the message in the console or logs:
|
|
247
247
|
Make sure you have started your web server with the option +--debugger+:
|
248
248
|
|
249
249
|
<shell>
|
250
|
-
~/PathTo/rails_project$
|
251
|
-
=> Booting Mongrel (use '
|
252
|
-
=> Rails
|
250
|
+
~/PathTo/rails_project$ rails server --debugger
|
251
|
+
=> Booting Mongrel (use 'rails server webrick' to force WEBrick)
|
252
|
+
=> Rails 3.0.0 application starting on http://0.0.0.0:3000
|
253
253
|
=> Debugger enabled
|
254
254
|
...
|
255
255
|
</shell>
|
@@ -472,10 +472,10 @@ class Author < ActiveRecord::Base
|
|
472
472
|
end
|
473
473
|
</ruby>
|
474
474
|
|
475
|
-
TIP: You can use ruby-debug while using
|
475
|
+
TIP: You can use ruby-debug while using +rails console+. Just remember to +require "ruby-debug"+ before calling the +debugger+ method.
|
476
476
|
|
477
477
|
<shell>
|
478
|
-
/PathTo/project $
|
478
|
+
/PathTo/project $ rails console
|
479
479
|
Loading development environment (Rails 2.1.0)
|
480
480
|
>> require "ruby-debug"
|
481
481
|
=> []
|
@@ -636,7 +636,7 @@ require 'bleak_house' if ENV['BLEAK_HOUSE']
|
|
636
636
|
Start a server instance with BleakHouse integration:
|
637
637
|
|
638
638
|
<shell>
|
639
|
-
RAILS_ENV=production BLEAK_HOUSE=1 ruby-bleak-house
|
639
|
+
RAILS_ENV=production BLEAK_HOUSE=1 ruby-bleak-house rails server
|
640
640
|
</shell>
|
641
641
|
|
642
642
|
Make sure to run a couple hundred requests to get better data samples, then press +CTRL-C+. The server will stop and Bleak House will produce a dumpfile in +/tmp+:
|
@@ -293,7 +293,7 @@ form_for(@article)
|
|
293
293
|
|
294
294
|
## Editing an existing article
|
295
295
|
# long-style:
|
296
|
-
form_for(:article, @article, :url => article_path(@article), :method => "put")
|
296
|
+
form_for(:article, @article, :url => article_path(@article), :html => { :method => "put" })
|
297
297
|
# short-style:
|
298
298
|
form_for(@article)
|
299
299
|
</ruby>
|
@@ -17,18 +17,18 @@ NOTE: This guide is about Rails generators for versions >= 3.0. Rails generators
|
|
17
17
|
|
18
18
|
h3. First contact
|
19
19
|
|
20
|
-
When you create an application using the +rails+ command, you are in fact using a Rails generator. After that, you can get a list of all available generators by just invoking +
|
20
|
+
When you create an application using the +rails+ command, you are in fact using a Rails generator. After that, you can get a list of all available generators by just invoking +rails generate+:
|
21
21
|
|
22
22
|
<shell>
|
23
23
|
$ rails myapp
|
24
24
|
$ cd myapp
|
25
|
-
$
|
25
|
+
$ rails generate
|
26
26
|
</shell>
|
27
27
|
|
28
28
|
You will get a list of all generators that comes with Rails. If you need a detailed description, for instance about the helper generator, you can simply do:
|
29
29
|
|
30
30
|
<shell>
|
31
|
-
$
|
31
|
+
$ rails generate helper --help
|
32
32
|
</shell>
|
33
33
|
|
34
34
|
h3. Creating your first generator
|
@@ -50,13 +50,13 @@ Our new generator is quite simple: it inherits from +Rails::Generators::Base+ an
|
|
50
50
|
To invoke our new generator, we just need to do:
|
51
51
|
|
52
52
|
<shell>
|
53
|
-
$
|
53
|
+
$ rails generate initializer
|
54
54
|
</shell>
|
55
55
|
|
56
56
|
Before we go on, let's see our brand new generator description:
|
57
57
|
|
58
58
|
<shell>
|
59
|
-
$
|
59
|
+
$ rails generate initializer --help
|
60
60
|
</shell>
|
61
61
|
|
62
62
|
Rails usually is able to generate good descriptions if a generator is namespaced, as +ActiveRecord::Generators::ModelGenerator+, but not in this particular case. We can solve this problem in two ways. The first one is calling +desc+ inside our generator:
|
@@ -77,7 +77,7 @@ h3. Creating generators with generators
|
|
77
77
|
A faster way to create a generator is using the generator's generator:
|
78
78
|
|
79
79
|
<shell>
|
80
|
-
$
|
80
|
+
$ rails generate generator initializer
|
81
81
|
create lib/generators/initializer
|
82
82
|
create lib/generators/initializer/initializer_generator.rb
|
83
83
|
create lib/generators/initializer/USAGE
|
@@ -99,9 +99,9 @@ At first, we can notice that we are inheriting from +Rails::Generators::NamedBas
|
|
99
99
|
We can see that by invoking the description of this new generator (don't forget to delete the old generator file):
|
100
100
|
|
101
101
|
<shell>
|
102
|
-
$
|
102
|
+
$ rails generate initializer --help
|
103
103
|
Usage:
|
104
|
-
|
104
|
+
rails generate initializer NAME [options]
|
105
105
|
</shell>
|
106
106
|
|
107
107
|
We can also see in our new generator that it has a class method called +source_root+. This method points to where our generator templates will be placed and by default it points to the created directory under +RAILS_APP/lib/generators/initializer/templates+. In order to understand what a generator template means, let's create a file at +RAILS_APP/lib/generators/initializer/templates/initializer.rb+ with the following content:
|
@@ -128,7 +128,7 @@ end
|
|
128
128
|
And let's execute our generator:
|
129
129
|
|
130
130
|
<shell>
|
131
|
-
$
|
131
|
+
$ rails generate initializer foo
|
132
132
|
</shell>
|
133
133
|
|
134
134
|
We can see that now a initializer named foo was created at +config/initializers/foo.rb+ with the contents of our template. That means that copy_file copied a file in our source root to the destination path we gave. The method +file_name+ is automatically created when we inherit from +Rails::Generators::NamedBase+.
|
@@ -166,7 +166,7 @@ end
|
|
166
166
|
Before we customize our workflow, let's first see how our scaffold looks like:
|
167
167
|
|
168
168
|
<shell>
|
169
|
-
$
|
169
|
+
$ rails generate scaffold User name:string
|
170
170
|
invoke active_record
|
171
171
|
create db/migrate/20091120125558_create_users.rb
|
172
172
|
create app/models/user.rb
|
@@ -212,7 +212,7 @@ If we generate another resource on scaffold, we can notice that neither styleshe
|
|
212
212
|
To show that, we are going to create a new helper generator that simply adds some instance variable readers. First, we create a generator:
|
213
213
|
|
214
214
|
<shell>
|
215
|
-
$
|
215
|
+
$ rails generate generator my_helper
|
216
216
|
</shell>
|
217
217
|
|
218
218
|
After that, we can delete both templates directory and the +source_root+ class method from our new generators, because we are not going to need them. So our new generator looks like the following:
|
@@ -232,7 +232,7 @@ end
|
|
232
232
|
We can try out our new generator by creating a helper for users:
|
233
233
|
|
234
234
|
<shell>
|
235
|
-
$
|
235
|
+
$ rails generate my_helper users
|
236
236
|
</shell>
|
237
237
|
|
238
238
|
And it will generate the following helper file in app/helpers:
|
@@ -258,7 +258,7 @@ end
|
|
258
258
|
And see it in action when invoking generator once again:
|
259
259
|
|
260
260
|
<shell>
|
261
|
-
$
|
261
|
+
$ rails generate scaffold Post body:text
|
262
262
|
[...]
|
263
263
|
invoke my_helper
|
264
264
|
create app/helpers/posts_helper.rb
|
@@ -343,7 +343,7 @@ Rails::Generators.fallbacks[:shoulda] = :test_unit
|
|
343
343
|
Now, if create a Comment scaffold, you will see that shoulda generators are being invoked, and at the end, they are just falling back to test unit generators:
|
344
344
|
|
345
345
|
<shell>
|
346
|
-
$
|
346
|
+
$ rails generate scaffold Comment body:text
|
347
347
|
invoke active_record
|
348
348
|
create db/migrate/20091120151323_create_comments.rb
|
349
349
|
create app/models/comment.rb
|
@@ -9,17 +9,17 @@ This guide covers getting up and running with Ruby on Rails. After reading it, y
|
|
9
9
|
|
10
10
|
endprologue.
|
11
11
|
|
12
|
-
WARNING. This Guide is based on Rails
|
12
|
+
WARNING. This Guide is based on Rails 3.0. Some of the code shown here will not work in earlier versions of Rails.
|
13
13
|
|
14
|
-
h3.
|
14
|
+
h3. Guide Assumptions
|
15
15
|
|
16
16
|
This guide is designed for beginners who want to get started with a Rails application from scratch. It does not assume that you have any prior experience with Rails. However, to get the most out of it, you need to have some prerequisites installed:
|
17
17
|
|
18
|
-
* The "Ruby":http://www.ruby-lang.org/en/downloads language
|
18
|
+
* The "Ruby":http://www.ruby-lang.org/en/downloads language version 1.8.7 or higher
|
19
19
|
* The "RubyGems":http://rubyforge.org/frs/?group_id=126 packaging system
|
20
|
-
* A working installation of
|
20
|
+
* A working installation of the "SQLite3 Database":http://www.sqlite.org
|
21
21
|
|
22
|
-
|
22
|
+
Rails is a web application framework running on the Ruby programming language. If you have no prior experience with Ruby, you will find a very steep learning curve diving straight into Rails. There are some good free resources on the internet for learning Ruby, including:
|
23
23
|
|
24
24
|
* "Mr. Neighborly's Humble Little Ruby Book":http://www.humblelittlerubybook.com
|
25
25
|
* "Programming Ruby":http://www.ruby-doc.org/docs/ProgrammingRuby/
|
@@ -27,19 +27,19 @@ It is highly recommended that you *familiarize yourself with Ruby before diving
|
|
27
27
|
|
28
28
|
h3. What is Rails?
|
29
29
|
|
30
|
-
Rails is a web development framework written in the Ruby language. It is designed to make programming web applications easier by making assumptions about what every developer needs to get started. It allows you to write less code while accomplishing more than many other languages and frameworks.
|
30
|
+
Rails is a web application development framework written in the Ruby language. It is designed to make programming web applications easier by making assumptions about what every developer needs to get started. It allows you to write less code while accomplishing more than many other languages and frameworks. Experienced Rails developers also report that it makes web application development more fun.
|
31
31
|
|
32
|
-
Rails is opinionated software.
|
32
|
+
Rails is opinionated software. It makes the assumption that there is a "best" way to do things, and it's designed to encourage that way - and in some cases to discourage alternatives. If you learn "The Rails Way" you'll probably discover a tremendous increase in productivity. If you persist in bringing old habits from other languages to your Rails development, and trying to use patterns you learned elsewhere, you may have a less happy experience.
|
33
33
|
|
34
34
|
The Rails philosophy includes several guiding principles:
|
35
35
|
|
36
36
|
* DRY - "Don't Repeat Yourself" - suggests that writing the same code over and over again is a bad thing.
|
37
|
-
* Convention Over Configuration - means that Rails makes assumptions about what you want to do and how you're going to do it, rather than
|
37
|
+
* Convention Over Configuration - means that Rails makes assumptions about what you want to do and how you're going to do it, rather than requiring you to specify every little thing through endless configuration files.
|
38
38
|
* REST is the best pattern for web applications - organizing your application around resources and standard HTTP verbs is the fastest way to go.
|
39
39
|
|
40
40
|
h4. The MVC Architecture
|
41
41
|
|
42
|
-
Rails is
|
42
|
+
At the core of Rails is the Model, View, Controller architecture, usually just called MVC. MVC benefits include:
|
43
43
|
|
44
44
|
* Isolation of business logic from the user interface
|
45
45
|
* Ease of keeping code DRY
|
@@ -59,15 +59,23 @@ Controllers provide the "glue" between models and views. In Rails, controllers a
|
|
59
59
|
|
60
60
|
h4. The Components of Rails
|
61
61
|
|
62
|
-
Rails
|
62
|
+
Rails ships as many individual components.
|
63
63
|
|
64
|
-
* Action
|
65
|
-
|
66
|
-
|
64
|
+
* Action Pack
|
65
|
+
** Action Controller
|
66
|
+
** Action Dispatch
|
67
|
+
** Action View
|
67
68
|
* Action Mailer
|
69
|
+
* Active Model
|
70
|
+
* Active Record
|
68
71
|
* Active Resource
|
69
|
-
* Railties
|
70
72
|
* Active Support
|
73
|
+
* Railties
|
74
|
+
|
75
|
+
|
76
|
+
h5. Action Pack
|
77
|
+
|
78
|
+
Action Pack is a single gem that contains Action Controller, Action View and Action Dispatch. The "VC" part of "MVC".
|
71
79
|
|
72
80
|
h5. Action Controller
|
73
81
|
|
@@ -77,38 +85,46 @@ h5. Action View
|
|
77
85
|
|
78
86
|
Action View manages the views of your Rails application. It can create both HTML and XML output by default. Action View manages rendering templates, including nested and partial templates, and includes built-in AJAX support.
|
79
87
|
|
80
|
-
h5.
|
88
|
+
h5. Action Dispatch
|
81
89
|
|
82
|
-
|
90
|
+
Action Dispatch handles routing of web requests and dispatches them as you want, either to your application, any other Rack application.
|
83
91
|
|
84
92
|
h5. Action Mailer
|
85
93
|
|
86
94
|
Action Mailer is a framework for building e-mail services. You can use Action Mailer to send emails based on flexible templates, or to receive and process incoming email.
|
87
95
|
|
88
|
-
h5. Active
|
96
|
+
h5. Active Model
|
89
97
|
|
90
|
-
Active
|
98
|
+
Active Model provides a defined interface between the Action Pack gem services and Object Relationship Mapping gems such as Active Record. Active Model allows Rails to utilize other ORM frameworks in place of Active Record if your application needs this.
|
91
99
|
|
92
|
-
h5.
|
100
|
+
h5. Active Record
|
101
|
+
|
102
|
+
Active Record is the base for the models in a Rails application. It provides database independence, basic CRUD functionality, advanced finding capabilities, and the ability to relate models to one another, among other services.
|
93
103
|
|
94
|
-
|
104
|
+
h5. Active Resource
|
105
|
+
|
106
|
+
Active Resource provides a framework for managing the connection between business objects and RESTful web services. It implements a way to map web-based resources to local objects with CRUD semantics.
|
95
107
|
|
96
108
|
h5. Active Support
|
97
109
|
|
98
110
|
Active Support is an extensive collection of utility classes and standard Ruby library extensions that are used in the Rails, both by the core code and by your applications.
|
99
111
|
|
112
|
+
h5. Railties
|
113
|
+
|
114
|
+
Railties is the core Rails code that builds new Rails applications and glues the various frameworks and plugins together in any Rails application.
|
115
|
+
|
100
116
|
h4. REST
|
101
117
|
|
102
|
-
|
118
|
+
Rest stands for Representational State Transfer and is the foundation of the RESTful architecture. This is generally considered to be Roy Fielding's doctoral thesis, "Architectural Styles and the Design of Network-based Software Architectures":http://www.ics.uci.edu/~fielding/pubs/dissertation/top.htm. While you can read through the thesis, REST in terms of Rails boils down to two main principles:
|
103
119
|
|
104
|
-
* Using resource identifiers
|
120
|
+
* Using resource identifiers such as URLs to represent resources.
|
105
121
|
* Transferring representations of the state of that resource between system components.
|
106
122
|
|
107
123
|
For example, to a Rails application a request such as this:
|
108
124
|
|
109
125
|
<tt>DELETE /photos/17</tt>
|
110
126
|
|
111
|
-
would be understood to refer to a photo resource with the ID of 17, and to indicate a desired action - deleting that resource. REST is a natural style for the architecture of web applications, and Rails
|
127
|
+
would be understood to refer to a photo resource with the ID of 17, and to indicate a desired action - deleting that resource. REST is a natural style for the architecture of web applications, and Rails hooks into this shielding you from many of the RESTful complexities and browser quirks.
|
112
128
|
|
113
129
|
If you'd like more details on REST as an architectural style, these resources are more approachable than Fielding's thesis:
|
114
130
|
|
@@ -126,35 +142,25 @@ h4. Installing Rails
|
|
126
142
|
In most cases, the easiest way to install Rails is to take advantage of RubyGems:
|
127
143
|
|
128
144
|
<shell>
|
129
|
-
|
145
|
+
Usually run this as the root user:
|
146
|
+
# gem install rails
|
130
147
|
</shell>
|
131
148
|
|
132
|
-
NOTE.
|
133
|
-
|
134
|
-
* If you're working on Windows, you may find it easier to install Instant Rails. Be aware, though, that "Instant Rails":http://instantrails.rubyforge.org/wiki/wiki.pl releases tend to lag seriously behind the actual Rails version. Also, you will find that Rails development on Windows is overall less pleasant than on other operating systems. If at all possible, we suggest that you install a Linux virtual machine and use that for Rails development, instead of using Windows.
|
135
|
-
* If you want to keep up with cutting-edge changes to Rails, you'll want to clone the "Rails source code":http://github.com/rails/rails/tree/master from github. This is not recommended as an option for beginners, though.
|
149
|
+
NOTE. In the Rails 3.0.0-beta, you will need to manually install the dependencies for Rails itself as a bug in rubygems will cause these to not be installed, see the "3.0 Release Notes":3_0_release_notes.html for the commands to run.
|
136
150
|
|
137
|
-
|
151
|
+
TIP. If you're working on Windows, you may find it easier to install "Instant Rails":http://instantrails.rubyforge.org/wiki/wiki.pl. Be aware, though, that Instant Rails releases tend to lag seriously behind the actual Rails version. Also, you will find that Rails development on Windows is overall less pleasant than on other operating systems. If at all possible, we suggest that you install a Linux virtual machine and use that for Rails development, instead of using Windows.
|
138
152
|
|
139
153
|
h4. Creating the Blog Application
|
140
154
|
|
141
|
-
|
155
|
+
The best way to use this guide is to follow each step as it happens, no code or step needed to make this example application has been left out, so you can literally follow along step by step. If you need to see the completed code, you can download it from "Getting Started Code":http://github.com/mikel/getting-started-code.
|
142
156
|
|
143
|
-
|
144
|
-
$ rails blog
|
145
|
-
</shell>
|
146
|
-
|
147
|
-
This will create a Rails application that uses a SQLite database for data storage. If you prefer to use MySQL, run this command instead:
|
157
|
+
To begin, open a terminal, navigate to a folder where you have rights to create files, and type:
|
148
158
|
|
149
159
|
<shell>
|
150
|
-
$ rails blog
|
160
|
+
$ rails blog
|
151
161
|
</shell>
|
152
162
|
|
153
|
-
|
154
|
-
|
155
|
-
<shell>
|
156
|
-
$ rails blog -d postgresql
|
157
|
-
</shell>
|
163
|
+
This will create a Rails application called Blog in a directory called blog.
|
158
164
|
|
159
165
|
TIP: You can see all of the switches that the Rails application builder accepts by running <tt>rails -h</tt>.
|
160
166
|
|
@@ -167,52 +173,62 @@ $ cd blog
|
|
167
173
|
In any case, Rails will create a folder in your working directory called <tt>blog</tt>. Open up that folder and explore its contents. Most of the work in this tutorial will happen in the <tt>app/</tt> folder, but here's a basic rundown on the function of each folder that Rails creates in a new application by default:
|
168
174
|
|
169
175
|
|_.File/Folder|_.Purpose|
|
170
|
-
|
|
176
|
+
|Gemfile|This file allows you to specify what gem dependencies are needed for your Rails application.|
|
177
|
+
|README.rdoc|This is a brief instruction manual for your application. Use it to tell others what your application does, how to set it up, and so on.|
|
171
178
|
|Rakefile|This file contains batch jobs that can be run from the terminal.|
|
172
179
|
|app/|Contains the controllers, models, and views for your application. You'll focus on this folder for the remainder of this guide.|
|
173
180
|
|config/|Configure your application's runtime rules, routes, database, and more.|
|
181
|
+
|config.ru|Rack configuration for Rack based servers used to start the application.|
|
174
182
|
|db/|Shows your current database schema, as well as the database migrations. You'll learn about migrations shortly.|
|
175
183
|
|doc/|In-depth documentation for your application.|
|
176
184
|
|lib/|Extended modules for your application (not covered in this guide).|
|
177
185
|
|log/|Application log files.|
|
178
186
|
|public/|The only folder seen to the world as-is. This is where your images, javascript, stylesheets (CSS), and other static files go.|
|
179
|
-
|script/|
|
187
|
+
|script/|Contains the rails script that starts your app and can contain other scripts you use to deploy or run your application.|
|
180
188
|
|test/|Unit tests, fixtures, and other test apparatus. These are covered in "Testing Rails Applications":testing.html|
|
181
189
|
|tmp/|Temporary files|
|
182
|
-
|vendor/|A place for third-party code. In a typical Rails application, this includes Ruby Gems, the Rails source code (if you install it into your project) and plugins containing additional prepackaged functionality.|
|
190
|
+
|vendor/|A place for all third-party code. In a typical Rails application, this includes Ruby Gems, the Rails source code (if you install it into your project) and plugins containing additional prepackaged functionality.|
|
191
|
+
|
192
|
+
h4. Installing the Required Gems
|
193
|
+
|
194
|
+
Rails uses the "Bundler":http://www.github.com/carlhuda/bundler gem to populate the +vendor+ directory with all the gems your application depends on. As we don't need any special gems beyond the default, we just need to do the following:
|
195
|
+
|
196
|
+
<shell>
|
197
|
+
As the root user:
|
198
|
+
# gem install bundler
|
199
|
+
# bundle install
|
200
|
+
</shell>
|
201
|
+
|
202
|
+
This will copy down the versions of all the gems you need to start a rails application.
|
183
203
|
|
184
204
|
h4. Configuring a Database
|
185
205
|
|
186
206
|
Just about every Rails application will interact with a database. The database to use is specified in a configuration file, +config/database.yml+.
|
187
|
-
If you open this file in a new Rails application, you'll see a default database configuration using
|
207
|
+
If you open this file in a new Rails application, you'll see a default database configuration using SQLite3. The file contains sections for three different environments in which Rails can run by default:
|
188
208
|
|
189
209
|
* The +development+ environment is used on your development computer as you interact manually with the application
|
190
210
|
* The +test+ environment is used to run automated tests
|
191
211
|
* The +production+ environment is used when you deploy your application for the world to use.
|
192
212
|
|
193
|
-
h5. Configuring a
|
213
|
+
h5. Configuring a SQLite3 Database
|
194
214
|
|
195
|
-
Rails comes with built-in support for "
|
215
|
+
Rails comes with built-in support for "SQLite3":http://www.sqlite.org, which is a lightweight serverless database application. While a busy production environment may overload SQLite, it works well for development and testing. Rails defaults to using a SQLite database when creating a new project, but you can always change it later.
|
196
216
|
|
197
|
-
Here's the section of the default configuration file with connection information for the development environment:
|
217
|
+
Here's the section of the default configuration file (<tt>config/database.yml</tt>) with connection information for the development environment:
|
198
218
|
|
199
219
|
<yaml>
|
200
220
|
development:
|
201
|
-
adapter: sqlite3
|
202
|
-
database: db/development.sqlite3
|
203
|
-
pool: 5
|
204
|
-
timeout: 5000
|
221
|
+
adapter: sqlite3
|
222
|
+
database: db/development.sqlite3
|
223
|
+
pool: 5
|
224
|
+
timeout: 5000
|
205
225
|
</yaml>
|
206
226
|
|
207
|
-
|
208
|
-
|
209
|
-
<shell>
|
210
|
-
$ gem install sqlite3-ruby
|
211
|
-
</shell>
|
227
|
+
NOTE: In this guide we are using an SQLite3 database for data storage, this is because it is a zero configuration database that just works. Rails also supports MySQL and PostgreSQL "out of the box", and has plugins for many database systems, if you are using a database in a production environment, Rails most likely has an adapter for it.
|
212
228
|
|
213
229
|
h5. Configuring a MySQL Database
|
214
230
|
|
215
|
-
If you choose to use MySQL, your +config/database.yml+ will look a little different. Here's the development section:
|
231
|
+
If you choose to use MySQL instead of the shipped Sqlite3 database, your +config/database.yml+ will look a little different. Here's the development section:
|
216
232
|
|
217
233
|
<yaml>
|
218
234
|
development:
|
@@ -229,7 +245,7 @@ If your development computer's MySQL installation includes a root user with an e
|
|
229
245
|
|
230
246
|
h5. Configuring a PostgreSQL Database
|
231
247
|
|
232
|
-
|
248
|
+
Finally if you choose to use PostgreSQL, your +config/database.yml+ will be customized to use PostgreSQL databases:
|
233
249
|
|
234
250
|
<yaml>
|
235
251
|
development:
|
@@ -251,66 +267,73 @@ Now that you have your database configured, it's time to have Rails create an em
|
|
251
267
|
$ rake db:create
|
252
268
|
</shell>
|
253
269
|
|
254
|
-
|
270
|
+
This will create your development and test SQLite3 databases inside the <tt>db/</tt> folder.
|
271
|
+
|
272
|
+
TIP: Rake is a general-purpose command-runner that Rails uses for many things. You can see the list of available rake commands in your application by running +rake -T+.
|
255
273
|
|
256
274
|
h3. Hello, Rails!
|
257
275
|
|
258
|
-
One of the traditional places to start with a new language is by getting some text up on screen quickly
|
276
|
+
One of the traditional places to start with a new language is by getting some text up on screen quickly, to do this, you need to get your Rails application server running.
|
277
|
+
|
278
|
+
h4. Starting up the Web Server
|
279
|
+
|
280
|
+
You actually have a functional Rails application already. To see it, you need to start a web server on your development machine. You can do this by running:
|
259
281
|
|
260
282
|
<shell>
|
261
|
-
$
|
283
|
+
$ rails server
|
262
284
|
</shell>
|
263
285
|
|
264
|
-
|
286
|
+
This will fire up an instance of the Mongrel web server by default (Rails can also use several other web servers). To see your application in action, open a browser window and navigate to "http://localhost:3000":http://localhost:3000. You should see Rails' default information page:
|
265
287
|
|
266
|
-
|
288
|
+
!images/rails_welcome.png(Welcome Aboard screenshot)!
|
267
289
|
|
268
|
-
|
269
|
-
<h1>Hello, Rails!</h1>
|
270
|
-
</code>
|
290
|
+
TIP: To stop the web server, hit Ctrl+C in the terminal window where it's running. In development mode, Rails does not generally require you to stop the server; changes you make in files will be automatically picked up by the server.
|
271
291
|
|
272
|
-
|
292
|
+
The "Welcome Aboard" page is the _smoke test_ for a new Rails application: it makes sure that you have your software configured correctly enough to serve a page. You can also click on the _About your application’s environment_ link to see a summary of your Application's environment.
|
273
293
|
|
274
|
-
|
294
|
+
h4. Say "Hello", Rails
|
295
|
+
|
296
|
+
To get Rails saying "Hello", you need to create at minimum a controller and a view. Fortunately, you can do that in a single command. Enter this command in your terminal:
|
275
297
|
|
276
298
|
<shell>
|
277
|
-
$
|
299
|
+
$ rails generate controller home index
|
278
300
|
</shell>
|
279
301
|
|
280
|
-
|
281
|
-
|
282
|
-
!images/rails_welcome.png(Welcome Aboard screenshot)!
|
302
|
+
TIP: If you're on Windows, or your Ruby is set up in some non-standard fashion, you may need to explicitly pass Rails +rails+ commands to Ruby: +ruby \path\to\rails controller home index+.
|
283
303
|
|
284
|
-
|
304
|
+
Rails will create several files for you, including +app/views/home/index.html.erb+. This is the template that will be used to display the results of the +index+ action (method) in the +home+ controller. Open this file in your text editor and edit it to contain a single line of code:
|
285
305
|
|
286
|
-
|
306
|
+
<code class="html">
|
307
|
+
<h1>Hello, Rails!</h1>
|
308
|
+
</code>
|
287
309
|
|
288
310
|
h4. Setting the Application Home Page
|
289
311
|
|
290
|
-
|
312
|
+
Now that we have made the controller and view, we need to tell Rails when we want "Hello Rails" to show up. In our case, we want it to show up when we navigate to the root URL of our site, "http://localhost:3000":http://localhost:3000, instead of the "Welcome Aboard" smoke test.
|
313
|
+
|
314
|
+
The first step to doing this is to delete the default page from your application:
|
291
315
|
|
292
316
|
<shell>
|
293
317
|
$ rm public/index.html
|
294
318
|
</shell>
|
295
319
|
|
296
|
-
|
320
|
+
We need to do this as Rails will deliver any static file in the +public+ directory in preference to any dynamic contact we generate from the controllers.
|
297
321
|
|
298
|
-
|
299
|
-
map.connect ':controller/:action/:id'
|
300
|
-
map.connect ':controller/:action/:id.:format'
|
301
|
-
</ruby>
|
322
|
+
Now, you have to tell Rails where your actual home page is located. Open the file +config/routes.rb+ in your editor. This is your application's _routing file_ which holds entries in a special DSL (domain-specific language) that tells Rails how to connect incoming requests to controllers and actions. There are only comments in this file, so we need to add at the top the following:
|
302
323
|
|
303
|
-
|
324
|
+
<ruby>
|
325
|
+
Blog::Application.routes.draw do |map|
|
304
326
|
|
305
|
-
|
327
|
+
root :to => "home#index"
|
306
328
|
|
307
|
-
|
308
|
-
|
329
|
+
# The priority is based upon order of creation:
|
330
|
+
# first created -> highest priority.
|
331
|
+
#...
|
309
332
|
</ruby>
|
310
333
|
|
311
|
-
|
334
|
+
The +root :to => "home#index"+ tells Rails to map the root action to the home controller's index action.
|
312
335
|
|
313
|
-
Now if you navigate to
|
336
|
+
Now if you navigate to "http://localhost:3000":http://localhost:3000 in your browser, you'll see +Hello, Rails!+.
|
314
337
|
|
315
338
|
NOTE. For more information about routing, refer to "Rails Routing from the Outside In":routing.html.
|
316
339
|
|
@@ -323,35 +346,36 @@ h3. Creating a Resource
|
|
323
346
|
In the case of the blog application, you can start by generating a scaffolded Post resource: this will represent a single blog posting. To do this, enter this command in your terminal:
|
324
347
|
|
325
348
|
<shell>
|
326
|
-
$
|
349
|
+
$ rails generate scaffold Post name:string title:string content:text
|
327
350
|
</shell>
|
328
351
|
|
329
352
|
NOTE. While scaffolding will get you up and running quickly, the "one size fits all" code that it generates is unlikely to be a perfect fit for your application. In most cases, you'll need to customize the generated code. Many experienced Rails developers avoid scaffolding entirely, preferring to write all or most of their source code from scratch.
|
330
353
|
|
331
|
-
The scaffold generator will build
|
332
|
-
|
333
|
-
|_.File
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|app/
|
339
|
-
|app/views/posts/
|
340
|
-
|app/views/
|
341
|
-
|
|
342
|
-
|app/
|
343
|
-
|
|
344
|
-
|app/
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|test/unit/helpers/posts_helper_test.rb
|
354
|
+
The scaffold generator will build 15 files in your application, along with some folders, and edit one more. Here's a quick overview of what it creates:
|
355
|
+
|
356
|
+
|_.File |_.Purpose|
|
357
|
+
|db/migrate/20100207214725_create_posts.rb.rb |Migration to create the posts table in your database (your name will include a different timestamp)|
|
358
|
+
|app/models/post.rb |The Post model|
|
359
|
+
|test/unit/post_test.rb |Unit testing harness for the posts model|
|
360
|
+
|test/fixtures/posts.yml |Dummy posts for use in testing|
|
361
|
+
|app/controllers/posts_controller.rb |The Posts controller|
|
362
|
+
|app/views/posts/index.html.erb |A view to display an index of all posts |
|
363
|
+
|app/views/posts/edit.html.erb |A view to edit an existing post|
|
364
|
+
|app/views/posts/show.html.erb |A view to display a single post|
|
365
|
+
|app/views/posts/new.html.erb |A view to create a new post|
|
366
|
+
|app/views/posts/_form.html.erb |A view to control the overall look and feel of the other posts views|
|
367
|
+
|app/views/layouts/posts.html.erb |A view to control the overall look and feel of the other posts views|
|
368
|
+
|test/functional/posts_controller_test.rb |Functional testing harness for the posts controller|
|
369
|
+
|app/helpers/posts_helper.rb |Helper functions to be used from the posts views|
|
370
|
+
|config/routes.rb |Edited to include routing information for posts|
|
371
|
+
|test/unit/helpers/posts_helper_test.rb |Unit testing harness for the posts helper|
|
372
|
+
|public/stylesheets/scaffold.css |Cascading style sheet to make the scaffolded views look better|
|
349
373
|
|
350
374
|
h4. Running a Migration
|
351
375
|
|
352
|
-
One of the products of the +
|
376
|
+
One of the products of the +rails generate scaffold+ command is a _database migration_. Migrations are Ruby classes that are designed to make it simple to create and modify database tables. Rails uses rake commands to run migrations, and it's possible to undo a migration after it's been applied to your database. Migration filenames include a timestamp to ensure that they're processed in the order that they were created.
|
353
377
|
|
354
|
-
If you look in the +db/migrate/
|
378
|
+
If you look in the +db/migrate/20100207214725_create_posts.rb+ file (remember, yours will have a slightly different name), here's what you'll find:
|
355
379
|
|
356
380
|
<ruby>
|
357
381
|
class CreatePosts < ActiveRecord::Migration
|
@@ -371,7 +395,7 @@ class CreatePosts < ActiveRecord::Migration
|
|
371
395
|
end
|
372
396
|
</ruby>
|
373
397
|
|
374
|
-
|
398
|
+
The above migration creates two methods, +up+, called when you run this migration into the database, and +down+ in case you need to reverse the changes made by this migration at a later date. The +up+ command in this case creates a +posts+ table with two string columns and a text column. It also is creating two timestamp fields to track record creation and updating. More information about Rails migrations can be found in the "Rails Database Migrations":migrations.html guide.
|
375
399
|
|
376
400
|
At this point, you can use a rake command to run the migration:
|
377
401
|
|
@@ -379,7 +403,14 @@ At this point, you can use a rake command to run the migration:
|
|
379
403
|
$ rake db:migrate
|
380
404
|
</shell>
|
381
405
|
|
382
|
-
|
406
|
+
Rails will execute this migration command and tell you it created the Posts table.
|
407
|
+
|
408
|
+
<shell>
|
409
|
+
== CreatePosts: migrating ====================================================
|
410
|
+
-- create_table(:posts)
|
411
|
+
-> 0.0019s
|
412
|
+
== CreatePosts: migrated (0.0020s) ===========================================
|
413
|
+
</shell>
|
383
414
|
|
384
415
|
NOTE. Because you're working in the development environment by default, this command will apply to the database defined in the +development+ section of your +config/database.yml+ file.
|
385
416
|
|
@@ -396,11 +427,11 @@ The +link_to+ method is one of Rails' built-in view helpers. It creates a hyperl
|
|
396
427
|
|
397
428
|
h4. Working with Posts in the Browser
|
398
429
|
|
399
|
-
Now you're ready to start working with posts. To do that, navigate to
|
430
|
+
Now you're ready to start working with posts. To do that, navigate to "http://localhost:3000":http://localhost:3000/ and then click the "My Blog" link:
|
400
431
|
|
401
432
|
!images/posts_index.png(Posts Index screenshot)!
|
402
433
|
|
403
|
-
This is the result of Rails rendering the +index+ view of your posts. There aren't currently any posts in the database, but if you click the +New Post+ link you can create one. After that, you'll find that you can edit posts, look at their details, or destroy them. All of the logic and HTML to handle this was built by the single +
|
434
|
+
This is the result of Rails rendering the +index+ view of your posts. There aren't currently any posts in the database, but if you click the +New Post+ link you can create one. After that, you'll find that you can edit posts, look at their details, or destroy them. All of the logic and HTML to handle this was built by the single +rails generate scaffold+ command.
|
404
435
|
|
405
436
|
TIP: In development mode (which is what you're working in by default), Rails reloads your application with every browser request, so there's no need to stop and restart the web server.
|
406
437
|
|
@@ -423,8 +454,9 @@ Rails includes methods to help you validate the data that you send to models. Op
|
|
423
454
|
|
424
455
|
<ruby>
|
425
456
|
class Post < ActiveRecord::Base
|
426
|
-
|
427
|
-
|
457
|
+
validates :name, :presence => true
|
458
|
+
validates :title, :presence => true,
|
459
|
+
:length => { :minimum => 5 }
|
428
460
|
end
|
429
461
|
</ruby>
|
430
462
|
|
@@ -435,22 +467,22 @@ h4. Using the Console
|
|
435
467
|
To see your validations in action, you can use the console. The console is a command-line tool that lets you execute Ruby code in the context of your application:
|
436
468
|
|
437
469
|
<shell>
|
438
|
-
$
|
470
|
+
$ rails console
|
439
471
|
</shell>
|
440
472
|
|
441
473
|
After the console loads, you can use it to work with your application's models:
|
442
474
|
|
443
475
|
<shell>
|
444
476
|
>> p = Post.create(:content => "A new post")
|
445
|
-
=> #<Post id: nil, name: nil, title: nil,
|
446
|
-
|
477
|
+
=> #<Post id: nil, name: nil, title: nil,
|
478
|
+
content: "A new post", created_at: nil,
|
479
|
+
updated_at: nil>
|
447
480
|
>> p.save
|
448
481
|
=> false
|
449
482
|
>> p.errors
|
450
|
-
=> #<
|
451
|
-
|
452
|
-
|
453
|
-
"is too short (minimum is 5 characters)"]}>
|
483
|
+
=> #<OrderedHash { :title=>["can't be blank",
|
484
|
+
"is too short (minimum is 5 characters)"],
|
485
|
+
:name=>["can't be blank"] }>
|
454
486
|
</shell>
|
455
487
|
|
456
488
|
This code shows creating a new +Post+ instance, attempting to save it and getting +false+ for a return value (indicating that the save failed), and inspecting the +errors+ of the post.
|
@@ -472,11 +504,11 @@ def index
|
|
472
504
|
end
|
473
505
|
</ruby>
|
474
506
|
|
475
|
-
This code sets the +@posts+ instance variable to an array of all posts in the database. +Post.
|
507
|
+
This code sets the +@posts+ instance variable to an array of all posts in the database. +Post.all+ calls the +Post+ model to return all of the posts that are currently in the database, with no limiting conditions.
|
476
508
|
|
477
509
|
TIP: For more information on finding records with Active Record, see "Active Record Query Interface":active_record_querying.html.
|
478
510
|
|
479
|
-
The +respond_to+ block handles both HTML and XML calls to this action. If you browse to
|
511
|
+
The +respond_to+ block handles both HTML and XML calls to this action. If you browse to "http://localhost:3000/posts.xml":http://localhost:3000/posts.xml, you'll see all of the posts in XML format. The HTML format looks for a view in +app/views/posts/+ with a name that corresponds to the action name. Rails makes all of the instance variables from the action available to the view. Here's +app/views/posts/index.html.erb+:
|
480
512
|
|
481
513
|
<erb>
|
482
514
|
<h1>Listing posts</h1>
|
@@ -486,17 +518,19 @@ The +respond_to+ block handles both HTML and XML calls to this action. If you br
|
|
486
518
|
<th>Name</th>
|
487
519
|
<th>Title</th>
|
488
520
|
<th>Content</th>
|
521
|
+
<th></th>
|
522
|
+
<th></th>
|
523
|
+
<th></th>
|
489
524
|
</tr>
|
490
525
|
|
491
526
|
<% @posts.each do |post| %>
|
492
527
|
<tr>
|
493
|
-
<td><%=
|
494
|
-
<td><%=
|
495
|
-
<td><%=
|
528
|
+
<td><%= post.name %></td>
|
529
|
+
<td><%= post.title %></td>
|
530
|
+
<td><%= post.content %></td>
|
496
531
|
<td><%= link_to 'Show', post %></td>
|
497
532
|
<td><%= link_to 'Edit', edit_post_path(post) %></td>
|
498
|
-
<td><%= link_to 'Destroy', post, :confirm => 'Are you sure?',
|
499
|
-
:method => :delete %></td>
|
533
|
+
<td><%= link_to 'Destroy', post, :confirm => 'Are you sure?', :method => :delete %></td>
|
500
534
|
</tr>
|
501
535
|
<% end %>
|
502
536
|
</table>
|
@@ -508,32 +542,29 @@ The +respond_to+ block handles both HTML and XML calls to this action. If you br
|
|
508
542
|
|
509
543
|
This view iterates over the contents of the +@posts+ array to display content and links. A few things to note in the view:
|
510
544
|
|
511
|
-
* +h+ is a Rails helper method to sanitize displayed data, preventing cross-site scripting attacks
|
512
545
|
* +link_to+ builds a hyperlink to a particular destination
|
513
546
|
* +edit_post_path+ is a helper that Rails provides as part of RESTful routing. You'll see a variety of these helpers for the different actions that the controller includes.
|
514
547
|
|
548
|
+
NOTE. In previous versions of Rails, you had to use +<%=h post.name %>+ so that any HTML would be escaped before being inserted into the page. In Rails 3.0, this is now the default. To get unescaped HTML, you now use +<%= raw post.name %>+.
|
549
|
+
|
515
550
|
TIP: For more details on the rendering process, see "Layouts and Rendering in Rails":layouts_and_rendering.html.
|
516
551
|
|
517
552
|
h4. Customizing the Layout
|
518
553
|
|
519
|
-
The view is only part of the story of how HTML is displayed in your web browser. Rails also has the concept of +layouts+, which are containers for views. When Rails renders a view to the browser, it does so by putting the view's HTML into a layout's HTML. The +
|
554
|
+
The view is only part of the story of how HTML is displayed in your web browser. Rails also has the concept of +layouts+, which are containers for views. When Rails renders a view to the browser, it does so by putting the view's HTML into a layout's HTML. The +rails generate scaffold+ command automatically created a default layout, +app/views/layouts/posts.html.erb+, for the posts. Open this layout in your editor and modify the +body+ tag:
|
520
555
|
|
521
556
|
<erb>
|
522
|
-
<!DOCTYPE html
|
523
|
-
|
524
|
-
|
525
|
-
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
|
557
|
+
<!DOCTYPE html>
|
558
|
+
<html>
|
526
559
|
<head>
|
527
|
-
<meta http-equiv="content-type"
|
528
|
-
content="text/html;charset=UTF-8" />
|
529
560
|
<title>Posts: <%= controller.action_name %></title>
|
530
561
|
<%= stylesheet_link_tag 'scaffold' %>
|
531
562
|
</head>
|
532
563
|
<body style="background: #EEEEEE;">
|
533
564
|
|
534
|
-
<p
|
565
|
+
<p class="notice"><%= notice %></p>
|
535
566
|
|
536
|
-
<%= yield
|
567
|
+
<%= yield %>
|
537
568
|
|
538
569
|
</body>
|
539
570
|
</html>
|
@@ -561,34 +592,48 @@ The +new.html.erb+ view displays this empty Post to the user:
|
|
561
592
|
<erb>
|
562
593
|
<h1>New post</h1>
|
563
594
|
|
595
|
+
<%= render 'form' %>
|
596
|
+
|
597
|
+
<%= link_to 'Back', posts_path %>
|
598
|
+
</erb>
|
599
|
+
|
600
|
+
The +<%= render 'form' %>+ line is our first introduction to _partials_ in Rails. A partial is a snippet of HTML and Ruby code that can be reused in multiple locations. In this case, the form used to make a new post, is basically identical to a form used to edit a post, both have text fields for the name and title and a text area for the content with a button to make a new post or update the existing post.
|
601
|
+
|
602
|
+
If you take a look at +views/posts/_form.html.erb+ file, you will see the following:
|
603
|
+
|
604
|
+
<erb>
|
564
605
|
<% form_for(@post) do |f| %>
|
565
606
|
<%= f.error_messages %>
|
566
607
|
|
567
|
-
<
|
608
|
+
<div class="field">
|
568
609
|
<%= f.label :name %><br />
|
569
610
|
<%= f.text_field :name %>
|
570
|
-
</
|
571
|
-
<
|
611
|
+
</div>
|
612
|
+
<div class="field">
|
572
613
|
<%= f.label :title %><br />
|
573
614
|
<%= f.text_field :title %>
|
574
|
-
</
|
575
|
-
<
|
615
|
+
</div>
|
616
|
+
<div class="field">
|
576
617
|
<%= f.label :content %><br />
|
577
618
|
<%= f.text_area :content %>
|
578
|
-
</
|
579
|
-
<
|
580
|
-
<%= f.submit
|
581
|
-
</
|
619
|
+
</div>
|
620
|
+
<div class="actions">
|
621
|
+
<%= f.submit %>
|
622
|
+
</div>
|
582
623
|
<% end %>
|
583
|
-
|
584
|
-
<%= link_to 'Back', posts_path %>
|
585
624
|
</erb>
|
586
625
|
|
626
|
+
This partial receives all the instance variables defined in the calling view file, so in this case, the controller assigned the new Post object to +@post+ and so, this is available in both the view and partial as +@post+.
|
627
|
+
|
628
|
+
For more information on partials, refer to the "Layouts and Rendering in Rails":layouts_and_rendering.html#using-partials guide.
|
629
|
+
|
587
630
|
The +form_for+ block is used to create an HTML form. Within this block, you have access to methods to build various controls on the form. For example, +f.text_field :name+ tells Rails to create a text input on the form, and to hook it up to the +name+ attribute of the instance being displayed. You can only use these methods with attributes of the model that the form is based on (in this case +name+, +title+, and +content+). Rails uses +form_for+ in preference to having you write raw HTML because the code is more succinct, and because it explicitly ties the form to a particular model instance.
|
588
631
|
|
632
|
+
The +form_for+ block is also smart enough to work out if you are doing a _New Post_ or an _Edit Post_ action, and will set the form +action+ tags and submit button names appropriately in the HTML output.
|
633
|
+
|
589
634
|
TIP: If you need to create an HTML form that displays arbitrary fields, not tied to a model, you should use the +form_tag+ method, which provides shortcuts for building forms that are not necessarily tied to a model instance.
|
590
635
|
|
591
|
-
When the user clicks the +Create+ button on this form, the browser will send information back to the +create+ method of the controller (Rails knows to call the +create+ method because the form is sent with an HTTP POST request; that's one of the conventions that I mentioned earlier):
|
636
|
+
When the user clicks the +Create Post+ button on this form, the browser will send information back to the +create+ method of the controller (Rails knows to call the +create+ method because the form is sent with an HTTP POST request; that's one of the conventions that I mentioned earlier):
|
592
637
|
|
593
638
|
<ruby>
|
594
639
|
def create
|
@@ -596,22 +641,24 @@ def create
|
|
596
641
|
|
597
642
|
respond_to do |format|
|
598
643
|
if @post.save
|
599
|
-
|
600
|
-
|
601
|
-
format.xml { render :xml => @post,
|
602
|
-
|
644
|
+
format.html { redirect_to(@post,
|
645
|
+
:notice => 'Post was successfully created.') }
|
646
|
+
format.xml { render :xml => @post,
|
647
|
+
:status => :created, :location => @post }
|
603
648
|
else
|
604
649
|
format.html { render :action => "new" }
|
605
650
|
format.xml { render :xml => @post.errors,
|
606
|
-
|
651
|
+
:status => :unprocessable_entity }
|
607
652
|
end
|
608
653
|
end
|
609
654
|
end
|
610
655
|
</ruby>
|
611
656
|
|
612
|
-
The +create+ action instantiates a new Post object from the data supplied by the user on the form, which Rails makes available in the +params+ hash. After saving the new post,
|
657
|
+
The +create+ action instantiates a new Post object from the data supplied by the user on the form, which Rails makes available in the +params+ hash. After successfully saving the new post, returns the appropriate format that the user has requested (HTML in our case). It then redirects the user to the resulting post +show+ action and sets a notice to the user that the Post was successfully created.
|
613
658
|
|
614
|
-
|
659
|
+
If the post was not successfully saved, due to a validation error, then the controller returns the user back to the +new+ action with any error messages so that the user has the chance to fix the error and try again.
|
660
|
+
|
661
|
+
The "Post was successfully created" message is stored inside of the Rails +flash+ hash, (usually just called the Flash) so that messages can be carried over to another action, providing the user with useful information on the status of their request. In the case of +create+, the user never actually sees any page rendered during the Post creation process, because it immediately redirects to the new Post as soon Rails saves the record. The Flash carries over a message to the next action, so that when the user is redirected back to the +show+ action, they are presented with a message saying "Post was successfully created."
|
615
662
|
|
616
663
|
h4. Showing an Individual Post
|
617
664
|
|
@@ -633,17 +680,17 @@ The +show+ action uses +Post.find+ to search for a single record in the database
|
|
633
680
|
<erb>
|
634
681
|
<p>
|
635
682
|
<b>Name:</b>
|
636
|
-
<%=
|
683
|
+
<%= @post.name %>
|
637
684
|
</p>
|
638
685
|
|
639
686
|
<p>
|
640
687
|
<b>Title:</b>
|
641
|
-
<%=
|
688
|
+
<%= @post.title %>
|
642
689
|
</p>
|
643
690
|
|
644
691
|
<p>
|
645
692
|
<b>Content:</b>
|
646
|
-
<%=
|
693
|
+
<%= @post.content %>
|
647
694
|
</p>
|
648
695
|
|
649
696
|
|
@@ -666,30 +713,15 @@ After finding the requested post, Rails uses the +edit.html.erb+ view to display
|
|
666
713
|
<erb>
|
667
714
|
<h1>Editing post</h1>
|
668
715
|
|
669
|
-
|
670
|
-
<%= f.error_messages %>
|
671
|
-
|
672
|
-
<p>
|
673
|
-
<%= f.label :name %><br />
|
674
|
-
<%= f.text_field :name %>
|
675
|
-
</p>
|
676
|
-
<p>
|
677
|
-
<%= f.label :title %><br />
|
678
|
-
<%= f.text_field :title %>
|
679
|
-
</p>
|
680
|
-
<p>
|
681
|
-
<%= f.label :content %><br />
|
682
|
-
<%= f.text_area :content %>
|
683
|
-
</p>
|
684
|
-
<p>
|
685
|
-
<%= f.submit "Update" %>
|
686
|
-
</p>
|
687
|
-
<% end %>
|
716
|
+
<%= render 'form' %>
|
688
717
|
|
689
718
|
<%= link_to 'Show', @post %> |
|
690
719
|
<%= link_to 'Back', posts_path %>
|
720
|
+
<% end %>
|
691
721
|
</erb>
|
692
722
|
|
723
|
+
Again, as with the +new+ action, the +edit+ action is using the +form+ partial, this time however, the form will do a PUT action to the PostsController and the submit button will display "Update Post"
|
724
|
+
|
693
725
|
Submitting the form created by this view will invoke the +update+ action within the controller:
|
694
726
|
|
695
727
|
<ruby>
|
@@ -698,13 +730,13 @@ def update
|
|
698
730
|
|
699
731
|
respond_to do |format|
|
700
732
|
if @post.update_attributes(params[:post])
|
701
|
-
|
702
|
-
|
733
|
+
format.html { redirect_to(@post,
|
734
|
+
:notice => 'Post was successfully updated.') }
|
703
735
|
format.xml { head :ok }
|
704
736
|
else
|
705
737
|
format.html { render :action => "edit" }
|
706
738
|
format.xml { render :xml => @post.errors,
|
707
|
-
|
739
|
+
:status => :unprocessable_entity }
|
708
740
|
end
|
709
741
|
end
|
710
742
|
end
|
@@ -712,8 +744,6 @@ end
|
|
712
744
|
|
713
745
|
In the +update+ action, Rails first uses the +:id+ parameter passed back from the edit view to locate the database record that's being edited. The +update_attributes+ call then takes the rest of the parameters from the request and applies them to this record. If all goes well, the user is redirected to the post's +show+ view. If there are any problems, it's back to +edit+ to correct them.
|
714
746
|
|
715
|
-
NOTE. Sharp-eyed readers will have noticed that the +form_for+ declaration is identical for the +new+ and +edit+ views. Rails generates different code for the two forms because it's smart enough to notice that in the one case it's being passed a new record that has never been saved, and in the other case an existing record that has already been saved to the database. In a production Rails application, you would ordinarily eliminate this duplication by moving identical code to a _partial template_, which you could then include in both parent templates. But the scaffold generator tries not to make too many assumptions, and generates code that's easy to modify if you want different forms for +create+ and +edit+.
|
716
|
-
|
717
747
|
h4. Destroying a Post
|
718
748
|
|
719
749
|
Finally, clicking one of the +destroy+ links sends the associated id to the +destroy+ action:
|
@@ -732,124 +762,6 @@ end
|
|
732
762
|
|
733
763
|
The +destroy+ method of an Active Record model instance removes the corresponding record from the database. After that's done, there isn't any record to display, so Rails redirects the user's browser to the index view for the model.
|
734
764
|
|
735
|
-
h3. DRYing up the Code
|
736
|
-
|
737
|
-
At this point, it's worth looking at some of the tools that Rails provides to eliminate duplication in your code. In particular, you can use _partials_ to clean up duplication in views and _filters_ to help with duplication in controllers.
|
738
|
-
|
739
|
-
h4. Using Partials to Eliminate View Duplication
|
740
|
-
|
741
|
-
As you saw earlier, the scaffold-generated views for the +new+ and +edit+ actions are largely identical. You can pull the shared code out into a partial template. This requires editing the new and edit views, and adding a new template. The new +_form.html.erb+ template should be saved in the same +app/views/posts+ folder as the files from which it is being extracted. Note that the name of this file begins with an underscore; that's the Rails naming convention for partial templates.
|
742
|
-
|
743
|
-
<tt>new.html.erb</tt>:
|
744
|
-
|
745
|
-
<erb>
|
746
|
-
<h1>New post</h1>
|
747
|
-
|
748
|
-
<%= render :partial => "form" %>
|
749
|
-
|
750
|
-
<%= link_to 'Back', posts_path %>
|
751
|
-
</erb>
|
752
|
-
|
753
|
-
<tt>edit.html.erb</tt>:
|
754
|
-
|
755
|
-
<erb>
|
756
|
-
<h1>Editing post</h1>
|
757
|
-
|
758
|
-
<%= render :partial => "form" %>
|
759
|
-
|
760
|
-
<%= link_to 'Show', @post %> |
|
761
|
-
<%= link_to 'Back', posts_path %>
|
762
|
-
</erb>
|
763
|
-
|
764
|
-
<tt>_form.html.erb</tt>:
|
765
|
-
|
766
|
-
<erb>
|
767
|
-
<% form_for(@post) do |f| %>
|
768
|
-
<%= f.error_messages %>
|
769
|
-
|
770
|
-
<p>
|
771
|
-
<%= f.label :name %><br />
|
772
|
-
<%= f.text_field :name %>
|
773
|
-
</p>
|
774
|
-
<p>
|
775
|
-
<%= f.label :title, "title" %><br />
|
776
|
-
<%= f.text_field :title %>
|
777
|
-
</p>
|
778
|
-
<p>
|
779
|
-
<%= f.label :content %><br />
|
780
|
-
<%= f.text_area :content %>
|
781
|
-
</p>
|
782
|
-
<p>
|
783
|
-
<%= f.submit "Save" %>
|
784
|
-
</p>
|
785
|
-
<% end %>
|
786
|
-
</erb>
|
787
|
-
|
788
|
-
Now, when Rails renders the +new+ or +edit+ view, it will insert the +_form+ partial at the indicated point. Note the naming convention for partials: if you refer to a partial named +form+ inside of a view, the corresponding file is +_form.html.erb+, with a leading underscore.
|
789
|
-
|
790
|
-
For more information on partials, refer to the "Layouts and Rendering in Rails":layouts_and_rendering.html#using-partials guide.
|
791
|
-
|
792
|
-
h4. Using Filters to Eliminate Controller Duplication
|
793
|
-
|
794
|
-
At this point, if you look at the controller for posts, you'll see some duplication:
|
795
|
-
|
796
|
-
<ruby>
|
797
|
-
class PostsController < ApplicationController
|
798
|
-
# ...
|
799
|
-
def show
|
800
|
-
@post = Post.find(params[:id])
|
801
|
-
# ...
|
802
|
-
end
|
803
|
-
|
804
|
-
def edit
|
805
|
-
@post = Post.find(params[:id])
|
806
|
-
end
|
807
|
-
|
808
|
-
def update
|
809
|
-
@post = Post.find(params[:id])
|
810
|
-
# ...
|
811
|
-
end
|
812
|
-
|
813
|
-
def destroy
|
814
|
-
@post = Post.find(params[:id])
|
815
|
-
# ...
|
816
|
-
end
|
817
|
-
end
|
818
|
-
</ruby>
|
819
|
-
|
820
|
-
Four instances of the exact same line of code doesn't seem very DRY. Rails provides _filters_ as a way to address this sort of repeated code. In this case, you can DRY things up by using a +before_filter+:
|
821
|
-
|
822
|
-
<ruby>
|
823
|
-
class PostsController < ApplicationController
|
824
|
-
before_filter :find_post,
|
825
|
-
:only => [:show, :edit, :update, :destroy]
|
826
|
-
# ...
|
827
|
-
def show
|
828
|
-
# ...
|
829
|
-
end
|
830
|
-
|
831
|
-
def edit
|
832
|
-
end
|
833
|
-
|
834
|
-
def update
|
835
|
-
# ...
|
836
|
-
end
|
837
|
-
|
838
|
-
def destroy
|
839
|
-
# ...
|
840
|
-
end
|
841
|
-
|
842
|
-
private
|
843
|
-
def find_post
|
844
|
-
@post = Post.find(params[:id])
|
845
|
-
end
|
846
|
-
end
|
847
|
-
</ruby>
|
848
|
-
|
849
|
-
Rails runs _before filters_ before any action in the controller. You can use the +:only+ clause to limit a before filter to only certain actions, or an +:except+ clause to specifically skip a before filter for certain actions. Rails also allows you to define _after filters_ that run after processing an action, as well as _around filters_ that surround the processing of actions. Filters can also be defined in external classes to make it easy to share them between controllers.
|
850
|
-
|
851
|
-
For more information on filters, see the "Action Controller Overview":action_controller_overview.html guide.
|
852
|
-
|
853
765
|
h3. Adding a Second Model
|
854
766
|
|
855
767
|
Now that you've seen what's in a model built with scaffolding, it's time to add a second model to the application. The second model will handle comments on blog posts.
|
@@ -859,14 +771,13 @@ h4. Generating a Model
|
|
859
771
|
Models in Rails use a singular name, and their corresponding database tables use a plural name. For the model to hold comments, the convention is to use the name Comment. Even if you don't want to use the entire apparatus set up by scaffolding, most Rails developers still use generators to make things like models and controllers. To create the new model, run this command in your terminal:
|
860
772
|
|
861
773
|
<shell>
|
862
|
-
$
|
863
|
-
post:references
|
774
|
+
$ rails generate model Comment commenter:string body:text post:references
|
864
775
|
</shell>
|
865
776
|
|
866
777
|
This command will generate four files:
|
867
778
|
|
868
779
|
* +app/models/comment.rb+ - The model
|
869
|
-
* +db/migrate/
|
780
|
+
* +db/migrate/20100207235629_create_comments.rb+ - The migration
|
870
781
|
* +test/unit/comment_test.rb+ and +test/fixtures/comments.yml+ - The test harness.
|
871
782
|
|
872
783
|
First, take a look at +comment.rb+:
|
@@ -905,7 +816,14 @@ The +t.references+ line sets up a foreign key column for the association between
|
|
905
816
|
$ rake db:migrate
|
906
817
|
</shell>
|
907
818
|
|
908
|
-
Rails is smart enough to only execute the migrations that have not already been run against the current database
|
819
|
+
Rails is smart enough to only execute the migrations that have not already been run against the current database, so in this case you will just see:
|
820
|
+
|
821
|
+
<shell>
|
822
|
+
== CreateComments: migrating =================================================
|
823
|
+
-- create_table(:comments)
|
824
|
+
-> 0.0017s
|
825
|
+
== CreateComments: migrated (0.0018s) ========================================
|
826
|
+
</shell>
|
909
827
|
|
910
828
|
h4. Associating Models
|
911
829
|
|
@@ -926,8 +844,10 @@ You'll need to edit the +post.rb+ file to add the other side of the association:
|
|
926
844
|
|
927
845
|
<ruby>
|
928
846
|
class Post < ActiveRecord::Base
|
929
|
-
|
930
|
-
|
847
|
+
validates :name, :presence => true
|
848
|
+
validates :title, :presence => true,
|
849
|
+
:length => { :minimum => 5 }
|
850
|
+
|
931
851
|
has_many :comments
|
932
852
|
end
|
933
853
|
</ruby>
|
@@ -936,12 +856,14 @@ These two declarations enable a good bit of automatic behavior. For example, if
|
|
936
856
|
|
937
857
|
TIP: For more information on Active Record associations, see the "Active Record Associations":association_basics.html guide.
|
938
858
|
|
939
|
-
h4. Adding a Route
|
859
|
+
h4. Adding a Route for Comments
|
940
860
|
|
941
|
-
|
861
|
+
As with the +home+ controller, we will need to add a route so that Rails knows where we would like to navigate to see +comments+. Open up the +config/routes.rb+ file again, you will see an entry that was added automatically for +posts+ near the top by the scaffold generator, +resources :posts+, edit it as follows:
|
942
862
|
|
943
863
|
<ruby>
|
944
|
-
|
864
|
+
resources :posts do
|
865
|
+
resources :comments
|
866
|
+
end
|
945
867
|
</ruby>
|
946
868
|
|
947
869
|
This creates +comments+ as a _nested resource_ within +posts+. This is another part of capturing the hierarchical relationship that exists between posts and comments.
|
@@ -953,249 +875,300 @@ h4. Generating a Controller
|
|
953
875
|
With the model in hand, you can turn your attention to creating a matching controller. Again, there's a generator for this:
|
954
876
|
|
955
877
|
<shell>
|
956
|
-
$
|
878
|
+
$ rails generate controller Comments
|
957
879
|
</shell>
|
958
880
|
|
959
|
-
This creates
|
881
|
+
This creates four files:
|
960
882
|
|
961
883
|
* +app/controllers/comments_controller.rb+ - The controller
|
962
884
|
* +app/helpers/comments_helper.rb+ - A view helper file
|
963
|
-
* +app/views/comments/index.html.erb+ - The view for the index action
|
964
|
-
* +app/views/comments/show.html.erb+ - The view for the show action
|
965
|
-
* +app/views/comments/new.html.erb+ - The view for the new action
|
966
|
-
* +app/views/comments/edit.html.erb+ - The view for the edit action
|
967
885
|
* +test/functional/comments_controller_test.rb+ - The functional tests for the controller
|
968
886
|
* +test/unit/helpers/comments_helper_test.rb+ - The unit tests for the helper
|
969
887
|
|
970
|
-
|
971
|
-
|
972
|
-
<ruby>
|
973
|
-
class CommentsController < ApplicationController
|
974
|
-
def index
|
975
|
-
end
|
888
|
+
Like with any blog, our readers will create their comments directly after reading the post, and once they have added their comment, will be sent back to the post show page to see their comment now listed. Due to this, our +CommentsController+ is there to provide a method to create comments and delete SPAM comments when they arrive.
|
976
889
|
|
977
|
-
|
978
|
-
end
|
890
|
+
So first, we'll wire up the Post show template (+/app/views/posts/show.html.erb+) to let us make a new comment:
|
979
891
|
|
980
|
-
|
981
|
-
|
982
|
-
|
983
|
-
|
984
|
-
|
892
|
+
<erb>
|
893
|
+
<p>
|
894
|
+
<b>Name:</b>
|
895
|
+
<%= @post.name %>
|
896
|
+
</p>
|
985
897
|
|
986
|
-
|
987
|
-
|
898
|
+
<p>
|
899
|
+
<b>Title:</b>
|
900
|
+
<%= @post.title %>
|
901
|
+
</p>
|
988
902
|
|
989
|
-
|
903
|
+
<p>
|
904
|
+
<b>Content:</b>
|
905
|
+
<%= @post.content %>
|
906
|
+
</p>
|
990
907
|
|
991
|
-
<
|
992
|
-
|
993
|
-
|
908
|
+
<h2>Add a comment:</h2>
|
909
|
+
<% form_for([@post, @post.comments.build]) do |f| %>
|
910
|
+
<%= f.error_messages %>
|
994
911
|
|
995
|
-
|
996
|
-
|
997
|
-
|
912
|
+
<div class="field">
|
913
|
+
<%= f.label :commenter %><br />
|
914
|
+
<%= f.text_field :commenter %>
|
915
|
+
</div>
|
916
|
+
<div class="field">
|
917
|
+
<%= f.label :body %><br />
|
918
|
+
<%= f.text_area :body %>
|
919
|
+
</div>
|
920
|
+
<div class="actions">
|
921
|
+
<%= f.submit %>
|
922
|
+
</div>
|
923
|
+
<% end %>
|
998
924
|
|
999
|
-
|
1000
|
-
|
1001
|
-
|
925
|
+
<%= link_to 'Edit Post', edit_post_path(@post) %> |
|
926
|
+
<%= link_to 'Back to Posts', posts_path %> |
|
927
|
+
</erb>
|
1002
928
|
|
1003
|
-
|
1004
|
-
@comment = @post.comments.build
|
1005
|
-
end
|
929
|
+
This adds a form on the Post show page that creates a new comment, which will call the +CommentsController+ +create+ action, so let's wire that up:
|
1006
930
|
|
931
|
+
<ruby>
|
932
|
+
class CommentsController < ApplicationController
|
1007
933
|
def create
|
1008
|
-
@comment = @post.comments.build(params[:comment])
|
1009
|
-
if @comment.save
|
1010
|
-
redirect_to post_comment_url(@post, @comment)
|
1011
|
-
else
|
1012
|
-
render :action => "new"
|
1013
|
-
end
|
1014
|
-
end
|
1015
|
-
|
1016
|
-
def edit
|
1017
|
-
@comment = @post.comments.find(params[:id])
|
1018
|
-
end
|
1019
|
-
|
1020
|
-
def update
|
1021
|
-
@comment = Comment.find(params[:id])
|
1022
|
-
if @comment.update_attributes(params[:comment])
|
1023
|
-
redirect_to post_comment_url(@post, @comment)
|
1024
|
-
else
|
1025
|
-
render :action => "edit"
|
1026
|
-
end
|
1027
|
-
end
|
1028
|
-
|
1029
|
-
def destroy
|
1030
|
-
@comment = Comment.find(params[:id])
|
1031
|
-
@comment.destroy
|
1032
|
-
redirect_to post_comments_path(@post)
|
1033
|
-
end
|
1034
|
-
|
1035
|
-
private
|
1036
|
-
def find_post
|
1037
934
|
@post = Post.find(params[:post_id])
|
935
|
+
@comment = @post.comments.create(params[:comment])
|
936
|
+
redirect_to post_path(@post)
|
1038
937
|
end
|
1039
|
-
|
1040
938
|
end
|
1041
939
|
</ruby>
|
1042
940
|
|
1043
|
-
You'll see a bit more complexity here than you did in the controller for posts. That's a side-effect of the nesting that you've set up; each request for a comment has to keep track of the post to which the comment is attached.
|
941
|
+
You'll see a bit more complexity here than you did in the controller for posts. That's a side-effect of the nesting that you've set up; each request for a comment has to keep track of the post to which the comment is attached, thus the initial find action to the Post model to get the post in question.
|
1044
942
|
|
1045
943
|
In addition, the code takes advantage of some of the methods available for an association. For example, in the +new+ method, it calls
|
1046
944
|
|
1047
|
-
|
1048
|
-
@comment = @post.comments.build
|
1049
|
-
</ruby>
|
1050
|
-
|
1051
|
-
This creates a new +Comment+ object _and_ sets up the +post_id+ field to have the +id+ from the specified +Post+ object in a single operation.
|
1052
|
-
|
1053
|
-
h4. Building Views
|
1054
|
-
|
1055
|
-
Because you skipped scaffolding, you'll need to build views for comments "by hand". Invoking +script/generate controller+ will give you skeleton views, but they'll be devoid of actual content. Here's a first pass at fleshing out the comment views.
|
1056
|
-
|
1057
|
-
The +views/comments/index.html.erb+ view:
|
945
|
+
Once we have made the new comment, we send the user back to the +post_path(@post)+ URL. This runs the +show+ action of the +PostsController+ which then renders the +show.html.erb+ template where we want the comment to show, so then, we'll add that to the +app/view/posts/show.html.erb+.
|
1058
946
|
|
1059
947
|
<erb>
|
1060
|
-
<
|
1061
|
-
|
1062
|
-
|
1063
|
-
|
1064
|
-
<th>Commenter</th>
|
1065
|
-
<th>Body</th>
|
1066
|
-
</tr>
|
1067
|
-
|
1068
|
-
<% for comment in @comments %>
|
1069
|
-
<tr>
|
1070
|
-
<td><%=h comment.commenter %></td>
|
1071
|
-
<td><%=h comment.body %></td>
|
1072
|
-
<td><%= link_to 'Show', post_comment_path(@post, comment) %></td>
|
1073
|
-
<td>
|
1074
|
-
<%= link_to 'Edit', edit_post_comment_path(@post, comment) %>
|
1075
|
-
</td>
|
1076
|
-
<td>
|
1077
|
-
<%= link_to 'Destroy', post_comment_path(@post, comment),
|
1078
|
-
:confirm => 'Are you sure?', :method => :delete %>
|
1079
|
-
</td>
|
1080
|
-
</tr>
|
1081
|
-
<% end %>
|
1082
|
-
</table>
|
948
|
+
<p>
|
949
|
+
<b>Name:</b>
|
950
|
+
<%= @post.name %>
|
951
|
+
</p>
|
1083
952
|
|
1084
|
-
<
|
953
|
+
<p>
|
954
|
+
<b>Title:</b>
|
955
|
+
<%= @post.title %>
|
956
|
+
</p>
|
1085
957
|
|
1086
|
-
|
1087
|
-
|
1088
|
-
|
958
|
+
<p>
|
959
|
+
<b>Content:</b>
|
960
|
+
<%= @post.content %>
|
961
|
+
</p>
|
1089
962
|
|
1090
|
-
|
963
|
+
<h2>Comments</h2>
|
964
|
+
<% @post.comments.each do |comment| %>
|
965
|
+
<p>
|
966
|
+
<b>Commenter:</b>
|
967
|
+
<%= comment.commenter %>
|
968
|
+
</p>
|
1091
969
|
|
1092
|
-
<
|
1093
|
-
<
|
970
|
+
<p>
|
971
|
+
<b>Comment:</b>
|
972
|
+
<%= comment.body %>
|
973
|
+
</p>
|
974
|
+
<% end %>
|
1094
975
|
|
1095
|
-
|
976
|
+
<h2>Add a comment:</h2>
|
977
|
+
<% form_for([@post, @post.comments.build]) do |f| %>
|
1096
978
|
<%= f.error_messages %>
|
1097
979
|
|
1098
|
-
<
|
980
|
+
<div class="field">
|
1099
981
|
<%= f.label :commenter %><br />
|
1100
982
|
<%= f.text_field :commenter %>
|
1101
|
-
</
|
1102
|
-
<
|
983
|
+
</div>
|
984
|
+
<div class="field">
|
1103
985
|
<%= f.label :body %><br />
|
1104
986
|
<%= f.text_area :body %>
|
1105
|
-
</
|
1106
|
-
<
|
1107
|
-
<%= f.submit
|
1108
|
-
</
|
987
|
+
</div>
|
988
|
+
<div class="actions">
|
989
|
+
<%= f.submit %>
|
990
|
+
</div>
|
1109
991
|
<% end %>
|
1110
992
|
|
1111
|
-
|
993
|
+
<br />
|
994
|
+
|
995
|
+
<%= link_to 'Edit Post', edit_post_path(@post) %> |
|
996
|
+
<%= link_to 'Back to Posts', posts_path %> |
|
1112
997
|
</erb>
|
1113
998
|
|
1114
|
-
|
999
|
+
Now you can add posts and comments to your blog and have them show up in the right places.
|
1115
1000
|
|
1116
|
-
|
1117
|
-
|
1001
|
+
h3. Refactorization
|
1002
|
+
|
1003
|
+
Now that we have Posts and Comments working, we can take a look at the +app/views/posts/show.html.erb+ template, it is getting long and awkward, we can use partials to clean this up.
|
1118
1004
|
|
1005
|
+
h4. Rendering Partial Collections
|
1006
|
+
|
1007
|
+
First will make a comment partial to extract showing all the comments for the post, so make a file +app/views/comments/_comment.html.erb+ and put into it:
|
1008
|
+
|
1009
|
+
<erb>
|
1119
1010
|
<p>
|
1120
1011
|
<b>Commenter:</b>
|
1121
|
-
<%=
|
1012
|
+
<%= comment.commenter %>
|
1122
1013
|
</p>
|
1123
1014
|
|
1124
1015
|
<p>
|
1125
1016
|
<b>Comment:</b>
|
1126
|
-
<%=
|
1017
|
+
<%= comment.body %>
|
1127
1018
|
</p>
|
1128
|
-
|
1129
|
-
<%= link_to 'Edit', edit_post_comment_path(@post, @comment) %> |
|
1130
|
-
<%= link_to 'Back', post_comments_path(@post) %>
|
1131
1019
|
</erb>
|
1132
1020
|
|
1133
|
-
|
1021
|
+
Then in the +app/views/posts/show.html.erb+ you can change it to look like the following:
|
1134
1022
|
|
1135
1023
|
<erb>
|
1136
|
-
<
|
1024
|
+
<p>
|
1025
|
+
<b>Name:</b>
|
1026
|
+
<%= @post.name %>
|
1027
|
+
</p>
|
1028
|
+
|
1029
|
+
<p>
|
1030
|
+
<b>Title:</b>
|
1031
|
+
<%= @post.title %>
|
1032
|
+
</p>
|
1137
1033
|
|
1138
|
-
|
1034
|
+
<p>
|
1035
|
+
<b>Content:</b>
|
1036
|
+
<%= @post.content %>
|
1037
|
+
</p>
|
1038
|
+
|
1039
|
+
<h2>Comments</h2>
|
1040
|
+
<%= render :partial => "comments/comment",
|
1041
|
+
:collection => @post.comments %>
|
1042
|
+
|
1043
|
+
<h2>Add a comment:</h2>
|
1044
|
+
<% form_for([@post, @post.comments.build]) do |f| %>
|
1139
1045
|
<%= f.error_messages %>
|
1140
1046
|
|
1141
|
-
<
|
1047
|
+
<div class="field">
|
1142
1048
|
<%= f.label :commenter %><br />
|
1143
1049
|
<%= f.text_field :commenter %>
|
1144
|
-
</
|
1145
|
-
<
|
1050
|
+
</div>
|
1051
|
+
<div class="field">
|
1146
1052
|
<%= f.label :body %><br />
|
1147
1053
|
<%= f.text_area :body %>
|
1148
|
-
</
|
1149
|
-
<
|
1150
|
-
<%= f.submit
|
1151
|
-
</
|
1054
|
+
</div>
|
1055
|
+
<div class="actions">
|
1056
|
+
<%= f.submit %>
|
1057
|
+
</div>
|
1152
1058
|
<% end %>
|
1153
1059
|
|
1154
|
-
|
1155
|
-
|
1060
|
+
<br />
|
1061
|
+
|
1062
|
+
<%= link_to 'Edit Post', edit_post_path(@post) %> |
|
1063
|
+
<%= link_to 'Back to Posts', posts_path %> |
|
1156
1064
|
</erb>
|
1157
1065
|
|
1158
|
-
|
1066
|
+
This will now render the partial in +app/views/comments/_comment.html.erb+ once for each comment that is in the +@post.comments+ collection. As the +render+ method iterates over the <tt>@post.comments</tt> collection, it assigns each comment to a local variable named the same as the partial, in this case +comment+ which is then available in the partial for us to show.
|
1159
1067
|
|
1160
|
-
h4.
|
1068
|
+
h4. Rendering a Partial Form
|
1161
1069
|
|
1162
|
-
|
1070
|
+
Lets also move that new comment section out to it's own partial, again, you create a file +app/views/comments/_form.html.erb+ and in it you put:
|
1071
|
+
|
1072
|
+
<erb>
|
1073
|
+
<% form_for([@post, @post.comments.build]) do |f| %>
|
1074
|
+
<%= f.error_messages %>
|
1075
|
+
|
1076
|
+
<div class="field">
|
1077
|
+
<%= f.label :commenter %><br />
|
1078
|
+
<%= f.text_field :commenter %>
|
1079
|
+
</div>
|
1080
|
+
<div class="field">
|
1081
|
+
<%= f.label :body %><br />
|
1082
|
+
<%= f.text_area :body %>
|
1083
|
+
</div>
|
1084
|
+
<div class="actions">
|
1085
|
+
<%= f.submit %>
|
1086
|
+
</div>
|
1087
|
+
<% end %>
|
1088
|
+
</erb>
|
1089
|
+
|
1090
|
+
Then you make the +app/views/posts/show.html.erb+ look like the following:
|
1163
1091
|
|
1164
1092
|
<erb>
|
1165
1093
|
<p>
|
1166
1094
|
<b>Name:</b>
|
1167
|
-
<%=
|
1095
|
+
<%= @post.name %>
|
1168
1096
|
</p>
|
1169
1097
|
|
1170
1098
|
<p>
|
1171
1099
|
<b>Title:</b>
|
1172
|
-
<%=
|
1100
|
+
<%= @post.title %>
|
1173
1101
|
</p>
|
1174
1102
|
|
1175
1103
|
<p>
|
1176
1104
|
<b>Content:</b>
|
1177
|
-
<%=
|
1105
|
+
<%= @post.content %>
|
1178
1106
|
</p>
|
1179
1107
|
|
1180
1108
|
<h2>Comments</h2>
|
1181
|
-
|
1182
|
-
|
1183
|
-
<b>Commenter:</b>
|
1184
|
-
<%=h c.commenter %>
|
1185
|
-
</p>
|
1109
|
+
<%= render :partial => "comments/comment",
|
1110
|
+
:collection => @post.comments %>
|
1186
1111
|
|
1187
|
-
|
1188
|
-
|
1189
|
-
|
1190
|
-
|
1191
|
-
<% end %>
|
1112
|
+
<h2>Add a comment:</h2>
|
1113
|
+
<%= render "comments/form" %>
|
1114
|
+
|
1115
|
+
<br />
|
1192
1116
|
|
1193
1117
|
<%= link_to 'Edit Post', edit_post_path(@post) %> |
|
1194
1118
|
<%= link_to 'Back to Posts', posts_path %> |
|
1195
|
-
<%= link_to 'Manage Comments', post_comments_path(@post) %>
|
1196
1119
|
</erb>
|
1197
1120
|
|
1198
|
-
|
1121
|
+
The second render just defines the partial template we want to render, <tt>comments/form</tt>, Rails is smart enough to spot the forward slash in that string and realise that you want to render the <tt>_form.html.erb</tt> file in the <tt>app/views/comments</tt> directory.
|
1122
|
+
|
1123
|
+
The +@post+ object is available any partials rendered in the view because we defined it as an instance variable.
|
1124
|
+
|
1125
|
+
h3. Deleting Comments
|
1126
|
+
|
1127
|
+
Another important feature on a blog is being able to delete SPAM comments. To do this, we need to implement a link of some sort in the view and a +DELETE+ action in the +CommentsController+.
|
1128
|
+
|
1129
|
+
So first, let's add the delete link in the +app/views/comments/_comment.html.erb+ partial:
|
1130
|
+
|
1131
|
+
<erb>
|
1132
|
+
<p>
|
1133
|
+
<b>Commenter:</b>
|
1134
|
+
<%= comment.commenter %>
|
1135
|
+
</p>
|
1136
|
+
|
1137
|
+
<p>
|
1138
|
+
<b>Comment:</b>
|
1139
|
+
<%= comment.body %>
|
1140
|
+
</p>
|
1141
|
+
|
1142
|
+
<p>
|
1143
|
+
<%= link_to 'Destroy Comment', [comment.post, comment],
|
1144
|
+
:confirm => 'Are you sure?',
|
1145
|
+
:method => :delete %>
|
1146
|
+
</p>
|
1147
|
+
</erb>
|
1148
|
+
|
1149
|
+
Clicking this new "Destroy Comment" link will fire off a <tt>DELETE /posts/:id/comments/:id</tt> to our +CommentsController+, which can then use this to find the comment we want to delete, so let's add a destroy action to our controller:
|
1150
|
+
|
1151
|
+
<ruby>
|
1152
|
+
class CommentsController < ApplicationController
|
1153
|
+
|
1154
|
+
def create
|
1155
|
+
@post = Post.find(params[:post_id])
|
1156
|
+
@comment = @post.comments.create(params[:comment])
|
1157
|
+
redirect_to post_path(@post)
|
1158
|
+
end
|
1159
|
+
|
1160
|
+
def destroy
|
1161
|
+
@post = Post.find(params[:post_id])
|
1162
|
+
@comment = @post.comments.find(params[:id])
|
1163
|
+
@comment.destroy
|
1164
|
+
redirect_to post_path(@post)
|
1165
|
+
end
|
1166
|
+
|
1167
|
+
end
|
1168
|
+
</ruby>
|
1169
|
+
|
1170
|
+
The +destroy+ action will find the post we are looking at, locate the comment within the <tt>@post.comments</tt> collection, and then remove it from the database and send us back to the show action for the post.
|
1171
|
+
|
1199
1172
|
|
1200
1173
|
h4. Deleting Associated Objects
|
1201
1174
|
|
@@ -1203,21 +1176,81 @@ If you decide at some point to delete a post, you likely want to delete the comm
|
|
1203
1176
|
|
1204
1177
|
<ruby>
|
1205
1178
|
class Post < ActiveRecord::Base
|
1206
|
-
|
1207
|
-
|
1179
|
+
validates :name, :presence => true
|
1180
|
+
validates :title, :presence => true,
|
1181
|
+
:length => { :minimum => 5 }
|
1208
1182
|
has_many :comments, :dependent => :destroy
|
1209
1183
|
end
|
1210
|
-
</ruby>
|
1184
|
+
</ruby>
|
1185
|
+
|
1186
|
+
h3. Security
|
1187
|
+
|
1188
|
+
Before you publish your blog online, you will most likely want to prevent just anyone from being able to add, edit and delete posts or delete comments.
|
1189
|
+
|
1190
|
+
Rails provides a very simple http authentication system that will work nicely in this situation. First, we enable simple HTTP based authentication in our <tt>app/controllers/application_controller.rb</tt>:
|
1191
|
+
|
1192
|
+
<ruby>
|
1193
|
+
class ApplicationController < ActionController::Base
|
1194
|
+
protect_from_forgery
|
1195
|
+
|
1196
|
+
private
|
1197
|
+
|
1198
|
+
def authenticate
|
1199
|
+
authenticate_or_request_with_http_basic do |user_name, password|
|
1200
|
+
user_name == 'admin' && password == 'password'
|
1201
|
+
end
|
1202
|
+
end
|
1203
|
+
|
1204
|
+
end
|
1205
|
+
</ruby>
|
1206
|
+
|
1207
|
+
You can obviously change the username and password to whatever you want. We put this method inside of +ApplicationController+ so that it is available to all of our controllers.
|
1208
|
+
|
1209
|
+
Then in the +PostsController+ we need to have a way to block access to the various actions if the person is not authenticated, here we can use the Rails <tt>before_filter</tt> method, which allows us to specify that Rails must run a method and only then allow access to the requested action if that method allows it.
|
1210
|
+
|
1211
|
+
To use the before filter, we specify it at the top of our +PostsController+, in this case, we want the user to be authenticated on every action, except for +index+ and +show+, so we write that:
|
1212
|
+
|
1213
|
+
<ruby>
|
1214
|
+
class PostsController < ApplicationController
|
1215
|
+
|
1216
|
+
before_filter :authenticate, :except => [:index, :show]
|
1217
|
+
|
1218
|
+
# GET /posts
|
1219
|
+
# GET /posts.xml
|
1220
|
+
def index
|
1221
|
+
@posts = Post.all
|
1222
|
+
respond_to do |format|
|
1223
|
+
# snipped for brevity
|
1224
|
+
</ruby>
|
1225
|
+
|
1226
|
+
We also only want to allow authenticated users to delete comments, so in the +CommentsController+ we write:
|
1227
|
+
|
1228
|
+
<ruby>
|
1229
|
+
class CommentsController < ApplicationController
|
1230
|
+
|
1231
|
+
before_filter :authenticate, :only => :destroy
|
1232
|
+
|
1233
|
+
def create
|
1234
|
+
@post = Post.find(params[:post_id])
|
1235
|
+
# snipped for brevity
|
1236
|
+
</ruby>
|
1237
|
+
|
1238
|
+
Now if you try to create a new post, you will be greeted with a basic HTTP Authentication challenge
|
1239
|
+
|
1240
|
+
!images/challenge.png(Basic HTTP Authentication Challenge)!
|
1241
|
+
|
1211
1242
|
|
1212
1243
|
h3. Building a Multi-Model Form
|
1213
1244
|
|
1214
|
-
|
1245
|
+
Another piece of your average blog is the ability to tag posts. This requires that your application edits more than one thing on a single form. Rails offers support for nested forms.
|
1246
|
+
|
1247
|
+
To demonstrate this, we will add support for giving each post multiple tags, right in the form where you create the post. First, create a new model to hold the tags:
|
1215
1248
|
|
1216
1249
|
<shell>
|
1217
|
-
$
|
1250
|
+
$ rails generate model tag name:string post:references
|
1218
1251
|
</shell>
|
1219
1252
|
|
1220
|
-
|
1253
|
+
Again, run the migration to create the database table:
|
1221
1254
|
|
1222
1255
|
<shell>
|
1223
1256
|
$ rake db:migrate
|
@@ -1227,9 +1260,11 @@ Next, edit the +post.rb+ file to create the other side of the association, and t
|
|
1227
1260
|
|
1228
1261
|
<ruby>
|
1229
1262
|
class Post < ActiveRecord::Base
|
1230
|
-
|
1231
|
-
|
1232
|
-
|
1263
|
+
validates :name, :presence => true
|
1264
|
+
validates :title, :presence => true,
|
1265
|
+
:length => { :minimum => 5 }
|
1266
|
+
|
1267
|
+
has_many :comments, :dependent => :destroy
|
1233
1268
|
has_many :tags
|
1234
1269
|
|
1235
1270
|
accepts_nested_attributes_for :tags, :allow_destroy => :true,
|
@@ -1239,54 +1274,150 @@ end
|
|
1239
1274
|
|
1240
1275
|
The +:allow_destroy+ option on the nested attribute declaration tells Rails to display a "remove" checkbox on the view that you'll build shortly. The +:reject_if+ option prevents saving new tags that do not have any attributes filled in.
|
1241
1276
|
|
1242
|
-
|
1277
|
+
We will modify +views/posts/_form.html.erb+ to render a partial to make a tag:
|
1243
1278
|
|
1244
1279
|
<erb>
|
1245
|
-
<% @post.tags.build
|
1280
|
+
<% @post.tags.build %>
|
1246
1281
|
<% form_for(@post) do |post_form| %>
|
1247
1282
|
<%= post_form.error_messages %>
|
1248
1283
|
|
1249
|
-
<
|
1284
|
+
<div class="field">
|
1250
1285
|
<%= post_form.label :name %><br />
|
1251
1286
|
<%= post_form.text_field :name %>
|
1252
|
-
</
|
1253
|
-
<
|
1254
|
-
<%= post_form.label :title
|
1287
|
+
</div>
|
1288
|
+
<div class="field">
|
1289
|
+
<%= post_form.label :title %><br />
|
1255
1290
|
<%= post_form.text_field :title %>
|
1256
|
-
</
|
1257
|
-
<
|
1291
|
+
</div>
|
1292
|
+
<div class="field">
|
1258
1293
|
<%= post_form.label :content %><br />
|
1259
1294
|
<%= post_form.text_area :content %>
|
1260
|
-
</
|
1295
|
+
</div>
|
1261
1296
|
<h2>Tags</h2>
|
1262
|
-
|
1263
|
-
|
1264
|
-
|
1265
|
-
|
1266
|
-
|
1267
|
-
|
1268
|
-
|
1269
|
-
<%= tag_form.label :_delete, 'Remove:' %>
|
1270
|
-
<%= tag_form.check_box :_delete %>
|
1271
|
-
</p>
|
1272
|
-
<% end %>
|
1273
|
-
<% end %>
|
1297
|
+
<%= render :partial => 'tags/form',
|
1298
|
+
:locals => {:form => post_form} %>
|
1299
|
+
<div class="actions">
|
1300
|
+
<%= post_form.submit %>
|
1301
|
+
</div>
|
1302
|
+
<% end %>
|
1303
|
+
</erb>
|
1274
1304
|
|
1275
|
-
|
1276
|
-
|
1277
|
-
|
1305
|
+
This example shows another option of the render helper, being able to pass in local variables, in this case, we want the local variable +form+ in the partial to refer to the +post_form+ object.
|
1306
|
+
|
1307
|
+
You will also note that we also have changed the +f+ in <tt>form_for(@post) do |f|</tt> to <tt>post_form</tt> to clarify what is going on somewhat.
|
1308
|
+
|
1309
|
+
We also add a <tt>@post.tags.build</tt> at the top of this form, this is to make sure there is a new tag ready to have it's name filled in by the user. If you do not build the new tag, then the form will not appear as there is no new Tag object ready to create.
|
1310
|
+
|
1311
|
+
Now create the folder <tt>app/views/tags</tt> and make a file in there called <tt>_form.html.erb</tt> which contains the form for the tag:
|
1312
|
+
|
1313
|
+
<erb>
|
1314
|
+
<% form.fields_for :tags do |tag_form| %>
|
1315
|
+
<div class="field">
|
1316
|
+
<%= tag_form.label :name, 'Tag:' %>
|
1317
|
+
<%= tag_form.text_field :name %>
|
1318
|
+
</div>
|
1319
|
+
<% unless tag_form.object.nil? || tag_form.object.new_record? %>
|
1320
|
+
<div class="field">
|
1321
|
+
<%= tag_form.label :_destroy, 'Remove:' %>
|
1322
|
+
<%= tag_form.check_box :_destroy %>
|
1323
|
+
</div>
|
1324
|
+
<% end %>
|
1278
1325
|
<% end %>
|
1279
1326
|
</erb>
|
1280
1327
|
|
1328
|
+
Finally, we will edit the <tt>app/views/posts/show.html.erb</tt> template to show our tags.
|
1329
|
+
|
1330
|
+
<erb>
|
1331
|
+
<p>
|
1332
|
+
<b>Name:</b>
|
1333
|
+
<%= @post.name %>
|
1334
|
+
</p>
|
1335
|
+
|
1336
|
+
<p>
|
1337
|
+
<b>Title:</b>
|
1338
|
+
<%= @post.title %>
|
1339
|
+
</p>
|
1340
|
+
|
1341
|
+
<p>
|
1342
|
+
<b>Content:</b>
|
1343
|
+
<%= @post.content %>
|
1344
|
+
</p>
|
1345
|
+
|
1346
|
+
<p>
|
1347
|
+
<b>Tags:</b>
|
1348
|
+
<%= @post.tags.map { |t| t.name }.join(", ") %>
|
1349
|
+
</p>
|
1350
|
+
|
1351
|
+
<h2>Comments</h2>
|
1352
|
+
<%= render :partial => "comments/comment",
|
1353
|
+
:collection => @post.comments %>
|
1354
|
+
|
1355
|
+
<h2>Add a comment:</h2>
|
1356
|
+
<%= render "comments/form" %>
|
1357
|
+
|
1358
|
+
|
1359
|
+
<%= link_to 'Edit Post', edit_post_path(@post) %> |
|
1360
|
+
<%= link_to 'Back to Posts', posts_path %> |
|
1361
|
+
</erb>
|
1362
|
+
|
1281
1363
|
With these changes in place, you'll find that you can edit a post and its tags directly on the same view.
|
1282
1364
|
|
1283
|
-
|
1365
|
+
However, that method call <tt>@post.tags.map { |t| t.name }.join(", ")</tt> is awkward, we could handle this by making a helper method.
|
1366
|
+
|
1367
|
+
h3. View Helpers
|
1368
|
+
|
1369
|
+
View Helpers live in <tt>app/helpers</tt> and provide small snippets of reusable code for views. In our case, we want a method that strings a bunch of objects together using their name attribute and joining them with a comma. As this is for the Post show template, we put it in the PostsHelper.
|
1370
|
+
|
1371
|
+
Open up <tt>app/helpers/posts_helper.rb</tt> and add the following:
|
1372
|
+
|
1373
|
+
<erb>
|
1374
|
+
module PostsHelper
|
1375
|
+
def join_tags(post)
|
1376
|
+
post.tags.map { |t| t.name }.join(", ")
|
1377
|
+
end
|
1378
|
+
end
|
1379
|
+
</erb>
|
1380
|
+
|
1381
|
+
Now you can edit the view in <tt>app/views/posts/show.html.erb</tt> to look like this:
|
1382
|
+
|
1383
|
+
<erb>
|
1384
|
+
<p>
|
1385
|
+
<b>Name:</b>
|
1386
|
+
<%= @post.name %>
|
1387
|
+
</p>
|
1388
|
+
|
1389
|
+
<p>
|
1390
|
+
<b>Title:</b>
|
1391
|
+
<%= @post.title %>
|
1392
|
+
</p>
|
1393
|
+
|
1394
|
+
<p>
|
1395
|
+
<b>Content:</b>
|
1396
|
+
<%= @post.content %>
|
1397
|
+
</p>
|
1398
|
+
|
1399
|
+
<p>
|
1400
|
+
<b>Tags:</b>
|
1401
|
+
<%= join_tags(@post) %>
|
1402
|
+
</p>
|
1403
|
+
|
1404
|
+
<h2>Comments</h2>
|
1405
|
+
<%= render :partial => "comments/comment",
|
1406
|
+
:collection => @post.comments %>
|
1407
|
+
|
1408
|
+
<h2>Add a comment:</h2>
|
1409
|
+
<%= render "comments/form" %>
|
1410
|
+
|
1411
|
+
|
1412
|
+
<%= link_to 'Edit Post', edit_post_path(@post) %> |
|
1413
|
+
<%= link_to 'Back to Posts', posts_path %> |
|
1414
|
+
</erb>
|
1284
1415
|
|
1285
1416
|
h3. What's Next?
|
1286
1417
|
|
1287
1418
|
Now that you've seen your first Rails application, you should feel free to update it and experiment on your own. But you don't have to do everything without help. As you need assistance getting up and running with Rails, feel free to consult these support resources:
|
1288
1419
|
|
1289
|
-
* The "Ruby On Rails guides":
|
1420
|
+
* The "Ruby On Rails guides":index.html
|
1290
1421
|
* The "Ruby on Rails mailing list":http://groups.google.com/group/rubyonrails-talk
|
1291
1422
|
* The "#rubyonrails":irc://irc.freenode.net/#rubyonrails channel on irc.freenode.net
|
1292
1423
|
* The "Rails Wiki":http://wiki.rubyonrails.org/
|
@@ -1296,10 +1427,13 @@ Rails also comes with built-in help that you can generate using the rake command
|
|
1296
1427
|
* Running +rake doc:guides+ will put a full copy of the Rails Guides in the +doc/guides+ folder of your application. Open +doc/guides/index.html+ in your web browser to explore the Guides.
|
1297
1428
|
* Running +rake doc:rails+ will put a full copy of the API documentation for Rails in the +doc/api+ folder of your application. Open +doc/api/index.html+ in your web browser to explore the API documentation.
|
1298
1429
|
|
1430
|
+
|
1299
1431
|
h3. Changelog
|
1300
1432
|
|
1301
1433
|
"Lighthouse ticket":http://rails.lighthouseapp.com/projects/16213-rails-guides/tickets/2
|
1302
1434
|
|
1435
|
+
* February 8, 2010: Full re-write for Rails 3.0-beta, added helpers and before_filters, refactored code by "Mikel Lindsaar":credits:html#raasdnil
|
1436
|
+
* January 24, 2010: Re-write for Rails 3.0 by "Mikel Lindsaar":credits:html#raasdnil
|
1303
1437
|
* July 18, 2009: Minor cleanup in anticipation of Rails 2.3.3 by "Mike Gunderloy":credits.html#mgunderloy
|
1304
1438
|
* February 1, 2009: Updated for Rails 2.3 by "Mike Gunderloy":credits.html#mgunderloy
|
1305
1439
|
* November 3, 2008: Formatting patch from Dave Rothlisberger
|
@@ -1307,4 +1441,4 @@ h3. Changelog
|
|
1307
1441
|
* October 16, 2008: Revised based on feedback from Pratik Naik by "Mike Gunderloy":credits.html#mgunderloy (not yet approved for publication)
|
1308
1442
|
* October 13, 2008: First complete draft by "Mike Gunderloy":credits.html#mgunderloy (not yet approved for publication)
|
1309
1443
|
* October 12, 2008: More detail, rearrangement, editing by "Mike Gunderloy":credits.html#mgunderloy (not yet approved for publication)
|
1310
|
-
* September 8, 2008: initial version by James Miller (not yet approved for publication)
|
1444
|
+
* September 8, 2008: initial version by "James Miller":credits.html#bensie (not yet approved for publication)
|