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
@@ -44,7 +44,7 @@ class Person < ActiveRecord::Base
|
|
44
44
|
end
|
45
45
|
</ruby>
|
46
46
|
|
47
|
-
We can see how it works by looking at some
|
47
|
+
We can see how it works by looking at some +rails console+ output:
|
48
48
|
|
49
49
|
<shell>
|
50
50
|
>> p = Person.new(:name => "John Doe")
|
@@ -240,11 +240,11 @@ end
|
|
240
240
|
|
241
241
|
The +validates_exclusion_of+ helper has an option +:in+ that receives the set of values that will not be accepted for the validated attributes. The +:in+ option has an alias called +:within+ that you can use for the same purpose, if you'd like to. This example uses the +:message+ option to show how you can include the attribute's value.
|
242
242
|
|
243
|
-
The default error message for +validates_exclusion_of+ is "_is
|
243
|
+
The default error message for +validates_exclusion_of+ is "_is reserved_".
|
244
244
|
|
245
245
|
h4. +validates_format_of+
|
246
246
|
|
247
|
-
This helper validates the attributes' values by testing whether they match a given regular
|
247
|
+
This helper validates the attributes' values by testing whether they match a given regular expression, which is specified using the +:with+ option.
|
248
248
|
|
249
249
|
<ruby>
|
250
250
|
class Product < ActiveRecord::Base
|
@@ -152,7 +152,7 @@ expire_fragment('all_available_products')
|
|
152
152
|
|
153
153
|
h4. Sweepers
|
154
154
|
|
155
|
-
Cache sweeping is a mechanism which allows you to get around having a ton of +expire_{page,action,fragment}+ calls in your code. It does this by moving all the work required to expire cached content into
|
155
|
+
Cache sweeping is a mechanism which allows you to get around having a ton of +expire_{page,action,fragment}+ calls in your code. It does this by moving all the work required to expire cached content into an +ActionController::Caching::Sweeper+ subclass. This class is an observer and looks for changes to an object via callbacks, and when a change occurs it expires the caches associated with that object in an around or after filter.
|
156
156
|
|
157
157
|
Continuing with our Product controller example, we could rewrite it with a sweeper like this:
|
158
158
|
|
@@ -12,15 +12,18 @@ NOTE: This tutorial assumes you have basic Rails knowledge from reading the "Get
|
|
12
12
|
|
13
13
|
endprologue.
|
14
14
|
|
15
|
+
WARNING. This Guide is based on Rails 3.0. Some of the code shown here will not work in earlier versions of Rails.
|
16
|
+
|
15
17
|
h3. Command Line Basics
|
16
18
|
|
17
19
|
There are a few commands that are absolutely critical to your everyday usage of Rails. In the order of how much you'll probably use them are:
|
18
20
|
|
19
|
-
* console
|
20
|
-
* server
|
21
|
+
* rails console
|
22
|
+
* rails server
|
21
23
|
* rake
|
22
|
-
* generate
|
23
|
-
* rails
|
24
|
+
* rails generate
|
25
|
+
* rails dbconsole
|
26
|
+
* rails app_name
|
24
27
|
|
25
28
|
Let's create a simple Rails application to step through each of these commands in context.
|
26
29
|
|
@@ -28,37 +31,39 @@ h4. +rails+
|
|
28
31
|
|
29
32
|
The first thing we'll want to do is create a new Rails application by running the +rails+ command after installing Rails.
|
30
33
|
|
31
|
-
WARNING: You know you need the rails gem installed by typing +gem install rails+ first,
|
34
|
+
WARNING: You know you need the rails gem installed by typing +gem install rails+ first, if you don't have this installed, follow the instructions in the "Rails 3 Release Notes":/3_0_release_notes.textile
|
32
35
|
|
33
36
|
<shell>
|
34
37
|
$ rails commandsapp
|
35
|
-
|
36
|
-
create
|
37
|
-
create
|
38
|
-
create
|
39
|
-
create
|
40
|
-
|
38
|
+
create
|
39
|
+
create README
|
40
|
+
create .gitignore
|
41
|
+
create Rakefile
|
42
|
+
create config.ru
|
43
|
+
create Gemfile
|
44
|
+
create app
|
41
45
|
...
|
42
|
-
create
|
43
|
-
create
|
44
|
-
create
|
46
|
+
create tmp/cache
|
47
|
+
create tmp/pids
|
48
|
+
create vendor/plugins
|
49
|
+
create vendor/plugins/.gitkeep
|
45
50
|
</shell>
|
46
51
|
|
47
52
|
Rails will set you up with what seems like a huge amount of stuff for such a tiny command! You've got the entire Rails directory structure now with all the code you need to run our simple application right out of the box.
|
48
53
|
|
49
54
|
INFO: This output will seem very familiar when we get to the +generate+ command. Creepy foreshadowing!
|
50
55
|
|
51
|
-
h4. +server+
|
56
|
+
h4. +rails server+
|
52
57
|
|
53
|
-
Let's try it! The +server+ command launches a small web server named WEBrick which comes bundled with Ruby. You'll use this any time you want to view your work through a web browser.
|
58
|
+
Let's try it! The +rails server+ command launches a small web server named WEBrick which comes bundled with Ruby. You'll use this any time you want to view your work through a web browser.
|
54
59
|
|
55
60
|
INFO: WEBrick isn't your only option for serving Rails. We'll get to that in a later section.
|
56
61
|
|
57
|
-
Without any prodding of any kind, +server+ will run our new shiny Rails app:
|
62
|
+
Without any prodding of any kind, +rails server+ will run our new shiny Rails app:
|
58
63
|
|
59
64
|
<shell>
|
60
65
|
$ cd commandsapp
|
61
|
-
$
|
66
|
+
$ rails server
|
62
67
|
=> Booting WEBrick...
|
63
68
|
=> Rails 2.2.0 application started on http://0.0.0.0:3000
|
64
69
|
=> Ctrl-C to shutdown server; call with --help for options
|
@@ -67,45 +72,45 @@ $ ./script/server
|
|
67
72
|
[2008-11-04 10:11:38] INFO WEBrick::HTTPServer#start: pid=18994 port=3000
|
68
73
|
</shell>
|
69
74
|
|
70
|
-
|
75
|
+
With just three commands we whipped up a Rails server listening on port 3000. Go to your browser and open "http://localhost:3000":http://localhost:3000, you will see a basic rails app running.
|
71
76
|
|
72
|
-
|
77
|
+
h4. +rails generate+
|
73
78
|
|
74
|
-
|
75
|
-
|
76
|
-
The +generate+ command uses templates to create a whole lot of things. You can always find out what's available by running +generate+ by itself. Let's do that:
|
79
|
+
The +rails generate+ command uses templates to create a whole lot of things. You can always find out what's available by running +rails generate+ by itself. Let's do that:
|
77
80
|
|
78
81
|
<shell>
|
79
|
-
$
|
80
|
-
Usage:
|
82
|
+
$ rails generate
|
83
|
+
Usage: rails generate generator [options] [args]
|
81
84
|
|
82
85
|
...
|
83
86
|
...
|
84
87
|
|
85
|
-
|
86
|
-
Built-in: controller, integration_test, mailer, migration, model, observer, performance_test, plugin, resource, scaffold, session_migration
|
88
|
+
Please choose a generator below.
|
87
89
|
|
88
|
-
|
89
|
-
|
90
|
+
Rails:
|
91
|
+
controller
|
92
|
+
generator
|
93
|
+
...
|
94
|
+
...
|
90
95
|
</shell>
|
91
96
|
|
92
97
|
NOTE: You can install more generators through generator gems, portions of plugins you'll undoubtedly install, and you can even create your own!
|
93
98
|
|
94
|
-
Using generators will save you a large amount of time by writing *boilerplate code
|
99
|
+
Using generators will save you a large amount of time by writing *boilerplate code*, code that is necessary for the app to work, but not necessary for you to spend time writing. That's what we have computers for.
|
95
100
|
|
96
101
|
Let's make our own controller with the controller generator. But what command should we use? Let's ask the generator:
|
97
102
|
|
98
|
-
INFO: All Rails console utilities have help text. As with most *NIX utilities, you can try adding +--help+ or +-h+ to the end, for example
|
103
|
+
INFO: All Rails console utilities have help text. As with most *NIX utilities, you can try adding +--help+ or +-h+ to the end, for example +rails server --help+.
|
99
104
|
|
100
105
|
<shell>
|
101
|
-
$
|
102
|
-
Usage:
|
106
|
+
$ rails generate controller
|
107
|
+
Usage: rails generate controller ControllerName [options]
|
103
108
|
|
104
109
|
...
|
105
110
|
...
|
106
111
|
|
107
112
|
Example:
|
108
|
-
|
113
|
+
rails generate controller CreditCard open debit credit close
|
109
114
|
|
110
115
|
Credit card controller with URLs like /credit_card/debit.
|
111
116
|
Controller: app/controllers/credit_card_controller.rb
|
@@ -114,7 +119,7 @@ Example:
|
|
114
119
|
Test: test/functional/credit_card_controller_test.rb
|
115
120
|
|
116
121
|
Modules Example:
|
117
|
-
|
122
|
+
rails generate controller 'admin/credit_card' suspend late_fee
|
118
123
|
|
119
124
|
Credit card admin controller with URLs /admin/credit_card/suspend.
|
120
125
|
Controller: app/controllers/admin/credit_card_controller.rb
|
@@ -123,81 +128,80 @@ Modules Example:
|
|
123
128
|
Test: test/functional/admin/credit_card_controller_test.rb
|
124
129
|
</shell>
|
125
130
|
|
126
|
-
|
131
|
+
The controller generator is expecting parameters in the form of +generate controller ControllerName action1 action2+. Let's make a +Greetings+ controller with an action of *hello*, which will say something nice to us.
|
127
132
|
|
128
133
|
<shell>
|
129
|
-
$
|
130
|
-
exists app/controllers/
|
131
|
-
exists app/helpers/
|
132
|
-
create app/views/greetings
|
133
|
-
exists test/functional/
|
134
|
+
$ rails generate controller Greetings hello
|
134
135
|
create app/controllers/greetings_controller.rb
|
135
|
-
|
136
|
-
create
|
137
|
-
create
|
136
|
+
invoke erb
|
137
|
+
create app/views/greetings
|
138
|
+
create app/views/greetings/hello.html.erb
|
139
|
+
error rspec [not found]
|
140
|
+
invoke helper
|
141
|
+
create app/helpers/greetings_helper.rb
|
142
|
+
error rspec [not found]
|
138
143
|
</shell>
|
139
144
|
|
140
|
-
|
145
|
+
What all did this generate? It made sure a bunch of directories were in our application, and created a controller file, a functional test file, a helper for the view, and a view file.
|
141
146
|
|
142
|
-
|
147
|
+
Check out the controller and modify it a little (in +app/controllers/greetings_controller.rb+):ma
|
143
148
|
|
144
149
|
<ruby>
|
145
150
|
class GreetingsController < ApplicationController
|
146
151
|
def hello
|
147
|
-
@message = "Hello, how are you today?
|
152
|
+
@message = "Hello, how are you today?"
|
148
153
|
end
|
149
154
|
|
150
155
|
end
|
151
156
|
</ruby>
|
152
157
|
|
153
|
-
Then the view, to display our
|
158
|
+
Then the view, to display our message (in +app/views/greetings/hello.html.erb+):
|
154
159
|
|
155
160
|
<html>
|
156
161
|
<h1>A Greeting for You!</h1>
|
157
162
|
<p><%= @message %></p>
|
158
163
|
</html>
|
159
164
|
|
160
|
-
Deal. Go check it out in your browser. Fire up your server. Remember?
|
165
|
+
Deal. Go check it out in your browser. Fire up your server. Remember? +rails server+ at the root of your Rails application should do it.
|
161
166
|
|
162
167
|
<shell>
|
163
|
-
$
|
168
|
+
$ rails server
|
164
169
|
=> Booting WEBrick...
|
165
170
|
</shell>
|
166
171
|
|
167
172
|
WARNING: Make sure that you do not have any "tilde backup" files in +app/views/(controller)+, or else WEBrick will _not_ show the expected output. This seems to be a *bug* in Rails 2.3.0.
|
168
173
|
|
169
|
-
The URL will be
|
174
|
+
The URL will be "http://localhost:3000/greetings/hello":http://localhost:3000/greetings/hello.
|
170
175
|
|
171
176
|
INFO: With a normal, plain-old Rails application, your URLs will generally follow the pattern of http://(host)/(controller)/(action), and a URL like http://(host)/(controller) will hit the *index* action of that controller.
|
172
177
|
|
173
|
-
|
178
|
+
Rails comes with a generator for data models too:
|
174
179
|
|
175
180
|
<shell>
|
176
|
-
$
|
177
|
-
Usage:
|
181
|
+
$ rails generate model
|
182
|
+
Usage: rails generate model ModelName [field:type, field:type]
|
178
183
|
|
179
184
|
...
|
180
185
|
|
181
186
|
Examples:
|
182
|
-
|
187
|
+
rails generate model account
|
183
188
|
|
184
|
-
creates an Account model, test, fixture, and migration:
|
185
189
|
Model: app/models/account.rb
|
186
190
|
Test: test/unit/account_test.rb
|
187
191
|
Fixtures: test/fixtures/accounts.yml
|
188
192
|
Migration: db/migrate/XXX_add_accounts.rb
|
189
193
|
|
190
|
-
|
194
|
+
rails generate model post title:string body:text published:boolean
|
191
195
|
|
192
|
-
|
196
|
+
Creates a Post model with a string title, text body, and published flag.
|
193
197
|
</shell>
|
194
198
|
|
195
199
|
But instead of generating a model directly (which we'll be doing later), let's set up a scaffold. A *scaffold* in Rails is a full set of model, database migration for that model, controller to manipulate it, views to view and manipulate the data, and a test suite for each of the above.
|
196
200
|
|
197
|
-
|
201
|
+
We will set up a simple resource called "HighScore" that will keep track of our highest score on video games we play.
|
198
202
|
|
199
203
|
<shell>
|
200
|
-
$
|
204
|
+
$ rails generate scaffold HighScore game:string score:integer
|
201
205
|
exists app/models/
|
202
206
|
exists app/controllers/
|
203
207
|
exists app/helpers/
|
@@ -227,45 +231,43 @@ dependency model
|
|
227
231
|
create db/migrate/20081217071914_create_high_scores.rb
|
228
232
|
</shell>
|
229
233
|
|
230
|
-
|
234
|
+
The generator checks that there exist the directories for models, controllers, helpers, layouts, functional and unit tests, stylesheets, creates the views, controller, model and database migration for HighScore (creating the +high_scores+ table and fields), takes care of the route for the *resource*, and new tests for everything.
|
231
235
|
|
232
|
-
The migration requires that we *migrate*, that is, run some Ruby code (living in that +
|
233
|
-
|
234
|
-
CAUTION: Hey. Install the sqlite3-ruby gem while you're at it. +gem install sqlite3-ruby+
|
236
|
+
The migration requires that we *migrate*, that is, run some Ruby code (living in that +20100209025147_create_high_scores.rb+) to modify the schema of our database. Which database? The sqlite3 database that Rails will create for you when we run the +rake db:migrate+ command. We'll talk more about Rake in-depth in a little while.
|
235
237
|
|
236
238
|
<shell>
|
237
239
|
$ rake db:migrate
|
238
|
-
(in /
|
239
|
-
CreateHighScores: migrating
|
240
|
-
|
241
|
-
|
242
|
-
CreateHighScores: migrated (0.
|
240
|
+
(in /Users/mikel/rails_programs/commandsapp)
|
241
|
+
== CreateHighScores: migrating ===============================================
|
242
|
+
-- create_table(:high_scores)
|
243
|
+
-> 0.0026s
|
244
|
+
== CreateHighScores: migrated (0.0028s) ======================================
|
243
245
|
</shell>
|
244
246
|
|
245
247
|
INFO: Let's talk about unit tests. Unit tests are code that tests and makes assertions about code. In unit testing, we take a little part of code, say a method of a model, and test its inputs and outputs. Unit tests are your friend. The sooner you make peace with the fact that your quality of life will drastically increase when you unit test your code, the better. Seriously. We'll make one in a moment.
|
246
248
|
|
247
|
-
Let's see the interface Rails created for us.
|
249
|
+
Let's see the interface Rails created for us. rails server; http://localhost:3000/high_scores
|
248
250
|
|
249
251
|
We can create new high scores (55,160 on Space Invaders!)
|
250
252
|
|
251
|
-
h4. +console+
|
253
|
+
h4. +rails console+
|
252
254
|
|
253
|
-
The +console+ command lets you interact with your Rails application from the command line. On the underside, +
|
255
|
+
The +console+ command lets you interact with your Rails application from the command line. On the underside, +rails console+ uses IRB, so if you've ever used it, you'll be right at home. This is useful for testing out quick ideas with code and changing data server-side without touching the website.
|
254
256
|
|
255
|
-
h4. +dbconsole+
|
257
|
+
h4. +rails dbconsole+
|
256
258
|
|
257
|
-
+dbconsole+ figures out which database you're using and drops you into whichever command line interface you would use with it (and figures out the command line parameters to give to it, too!). It supports MySQL, PostgreSQL, SQLite and SQLite3.
|
259
|
+
+rails dbconsole+ figures out which database you're using and drops you into whichever command line interface you would use with it (and figures out the command line parameters to give to it, too!). It supports MySQL, PostgreSQL, SQLite and SQLite3.
|
258
260
|
|
259
|
-
h4. +plugin+
|
261
|
+
h4. +rails plugin+
|
260
262
|
|
261
|
-
The +plugin+ command simplifies plugin management; think a miniature version of the Gem utility. Let's walk through installing a plugin. You can call the sub-command *discover*, which sifts through repositories looking for plugins, or call *source* to add a specific repository of plugins, or you can specify the plugin location directly.
|
263
|
+
The +rails plugin+ command simplifies plugin management; think a miniature version of the Gem utility. Let's walk through installing a plugin. You can call the sub-command *discover*, which sifts through repositories looking for plugins, or call *source* to add a specific repository of plugins, or you can specify the plugin location directly.
|
262
264
|
|
263
265
|
Let's say you're creating a website for a client who wants a small accounting system. Every event having to do with money must be logged, and must never be deleted. Wouldn't it be great if we could override the behavior of a model to never actually take its record out of the database, but *instead*, just set a field?
|
264
266
|
|
265
267
|
There is such a thing! The plugin we're installing is called "acts_as_paranoid", and it lets models implement a "deleted_at" column that gets set when you call destroy. Later, when calling find, the plugin will tack on a database check to filter out "deleted" things.
|
266
|
-
|
268
|
+
==================================================================================
|
267
269
|
<shell>
|
268
|
-
$
|
270
|
+
$ rails plugin install http://svn.techno-weenie.net/projects/plugins/acts_as_paranoid
|
269
271
|
+ ./CHANGELOG
|
270
272
|
+ ./MIT-LICENSE
|
271
273
|
...
|
@@ -277,7 +279,7 @@ h4. +runner+
|
|
277
279
|
<tt>runner</tt> runs Ruby code in the context of Rails non-interactively. For instance:
|
278
280
|
|
279
281
|
<shell>
|
280
|
-
$
|
282
|
+
$ rails runner "Model.long_running_method"
|
281
283
|
</shell>
|
282
284
|
|
283
285
|
h4. +destroy+
|
@@ -285,7 +287,7 @@ h4. +destroy+
|
|
285
287
|
Think of +destroy+ as the opposite of +generate+. It'll figure out what generate did, and undo it. Believe you-me, the creation of this tutorial used this command many times!
|
286
288
|
|
287
289
|
<shell>
|
288
|
-
$
|
290
|
+
$ rails generate model Oops
|
289
291
|
exists app/models/
|
290
292
|
exists test/unit/
|
291
293
|
exists test/fixtures/
|
@@ -294,7 +296,7 @@ $ ./script/generate model Oops
|
|
294
296
|
create test/fixtures/oops.yml
|
295
297
|
exists db/migrate
|
296
298
|
create db/migrate/20081221040817_create_oops.rb
|
297
|
-
$
|
299
|
+
$ rails destroy model Oops
|
298
300
|
notempty db/migrate
|
299
301
|
notempty db
|
300
302
|
rm db/migrate/20081221040817_create_oops.rb
|
@@ -314,7 +316,7 @@ h4. +about+
|
|
314
316
|
Check it: Version numbers for Ruby, RubyGems, Rails, the Rails subcomponents, your application's folder, the current Rails environment name, your app's database adapter, and schema version! +about+ is useful when you need to ask for help, check if a security patch might affect you, or when you need some stats for an existing Rails installation.
|
315
317
|
|
316
318
|
<shell>
|
317
|
-
$
|
319
|
+
$ rails about
|
318
320
|
About your application's environment
|
319
321
|
Ruby version 1.8.6 (i486-linux)
|
320
322
|
RubyGems version 1.3.1
|
@@ -399,7 +401,7 @@ Many people have created a large number different web servers in Ruby, and many
|
|
399
401
|
|
400
402
|
NOTE: For more details on the Rack integration, see "Rails on Rack":rails_on_rack.html.
|
401
403
|
|
402
|
-
To use a different server, just install its gem, then use its name for the first parameter to +
|
404
|
+
To use a different server, just install its gem, then use its name for the first parameter to +rails server+:
|
403
405
|
|
404
406
|
<shell>
|
405
407
|
$ sudo gem install mongrel
|
@@ -412,9 +414,9 @@ Successfully installed mongrel-1.1.5
|
|
412
414
|
...
|
413
415
|
...
|
414
416
|
Installing RDoc documentation for mongrel-1.1.5...
|
415
|
-
$
|
416
|
-
=> Booting Mongrel (use '
|
417
|
-
=> Rails
|
417
|
+
$ rails server mongrel
|
418
|
+
=> Booting Mongrel (use 'rails server webrick' to force WEBrick)
|
419
|
+
=> Rails 3.0.0 application starting on http://0.0.0.0:3000
|
418
420
|
...
|
419
421
|
</shell>
|
420
422
|
|
@@ -481,7 +483,7 @@ I got assigned some args:
|
|
481
483
|
Then we'll make sure it got included in the list of available generators:
|
482
484
|
|
483
485
|
<shell>
|
484
|
-
$
|
486
|
+
$ rails generate
|
485
487
|
...
|
486
488
|
...
|
487
489
|
Installed Generators
|
@@ -491,7 +493,7 @@ Installed Generators
|
|
491
493
|
SWEET! Now let's generate some text, yeah!
|
492
494
|
|
493
495
|
<shell>
|
494
|
-
$
|
496
|
+
$ rails generate tutorial_test arg1 arg2 arg3
|
495
497
|
exists public
|
496
498
|
create public/tutorial.txt
|
497
499
|
</shell>
|
@@ -9,24 +9,24 @@ endprologue.
|
|
9
9
|
|
10
10
|
h3. Locations for Initialization Code
|
11
11
|
|
12
|
-
Rails offers (at least)
|
12
|
+
Rails offers (at least) four good spots to place initialization code:
|
13
13
|
|
14
|
-
*
|
15
|
-
* environment.rb
|
14
|
+
* application.rb
|
16
15
|
* Environment-specific Configuration Files
|
17
16
|
* Initializers (load_application_initializers)
|
18
17
|
* After-Initializers
|
19
18
|
|
20
|
-
h3.
|
19
|
+
h3. Running Code Before Rails
|
21
20
|
|
22
|
-
|
21
|
+
To run some code before Rails itself is loaded, simply put it above the call to
|
22
|
+
+require 'rails/all'+ in your +application.rb+.
|
23
23
|
|
24
24
|
h3. Configuring Rails Components
|
25
25
|
|
26
|
-
In general, the work of configuring Rails means configuring the components of Rails, as well as configuring Rails itself. The +
|
26
|
+
In general, the work of configuring Rails means configuring the components of Rails, as well as configuring Rails itself. The +application.rb+ and environment-specific configuration files (such as +config/environments/production.rb+) allow you to specify the various settings that you want to pass down to all of the components. For example, the default Rails 2.3 +application.rb+ file includes one setting:
|
27
27
|
|
28
28
|
<ruby>
|
29
|
-
config.
|
29
|
+
config.filter_parameters << :password
|
30
30
|
</ruby>
|
31
31
|
|
32
32
|
This is a setting for Rails itself. If you want to pass settings to individual Rails components, you can do so via the same +config+ object:
|
@@ -53,8 +53,6 @@ h4. Rails General Configuration
|
|
53
53
|
|
54
54
|
* +config.eager_load_paths+ accepts an array of paths from which Rails will eager load on boot if cache classes is enabled. All elements of this array must also be in +load_paths+.
|
55
55
|
|
56
|
-
* +config.frameworks+ accepts an array of rails framework components that should be loaded. (Defaults to +:active_record+, +:action_controller+, +:action_view+, +:action_mailer+, and +:active_resource+).
|
57
|
-
|
58
56
|
* +config.load_once_paths+ accepts an array of paths from which Rails will automatically load from only once. All elements of this array must also be in +load_paths+.
|
59
57
|
|
60
58
|
* +config.load_paths+ accepts an array of additional paths to prepend to the load path. By default, all app, lib, vendor and mock paths are included in this list.
|
@@ -133,6 +131,8 @@ h4. Configuring Action Controller
|
|
133
131
|
|
134
132
|
* +config.action_controller.asset_host+ provides a string that is prepended to all of the URL-generating helpers in +AssetHelper+. This is designed to allow moving all javascript, CSS, and image files to a separate asset host.
|
135
133
|
|
134
|
+
* +config.action_controller.asset_path+ allows you to override the default asset path generation by providing your own instructions.
|
135
|
+
|
136
136
|
* +config.action_controller.consider_all_requests_local+ is generally set to +true+ during development and +false+ during production; if it is set to +true+, then any error will cause detailed debugging information to be dumped in the HTTP response. For finer-grained control, set this to +false+ and implement +local_request?+ to specify which requests should provide debugging information on errors.
|
137
137
|
|
138
138
|
* +config.action_controller.allow_concurrency+ should be set to +true+ to allow concurrent (threadsafe) action processing. Set to +false+ by default. You probably don't want to call this one directly, though, because a series of other adjustments need to be made for threadsafe mode to work properly. Instead, you should simply call +config.threadsafe!+ inside your +production.rb+ file, which makes all the necessary adjustments.
|
@@ -159,7 +159,7 @@ WARNING: Threadsafe operation in incompatible with the normal workings of develo
|
|
159
159
|
|
160
160
|
* +config.action_controller.relative_url_root+ can be used to tell Rails that you are deploying to a subdirectory. The default is +ENV['RAILS_RELATIVE_URL_ROOT']+.
|
161
161
|
|
162
|
-
* +config.
|
162
|
+
* +config.action_dispatch.session_store+ sets the name of the store for session data. The default is +:cookie_store+; other valid options include +:active_record_store+, +:mem_cache_store+ or the name of your own custom class.
|
163
163
|
|
164
164
|
The caching code adds two additional settings:
|
165
165
|
|