diff options
Diffstat (limited to 'railties')
-rw-r--r-- | railties/guides/source/action_controller_overview.textile | 19 | ||||
-rw-r--r-- | railties/guides/source/active_support_core_extensions.textile | 93 | ||||
-rw-r--r-- | railties/guides/source/generators.textile | 2 | ||||
-rw-r--r-- | railties/guides/source/getting_started.textile | 2 | ||||
-rw-r--r-- | railties/guides/source/rails_application_templates.textile | 4 | ||||
-rw-r--r-- | railties/guides/source/routing.textile | 15 | ||||
-rw-r--r-- | railties/guides/w3c_validator.rb | 1 |
7 files changed, 127 insertions, 9 deletions
diff --git a/railties/guides/source/action_controller_overview.textile b/railties/guides/source/action_controller_overview.textile index 9d8426b5de..038ca903c1 100644 --- a/railties/guides/source/action_controller_overview.textile +++ b/railties/guides/source/action_controller_overview.textile @@ -338,6 +338,25 @@ end Note that while for session values you set the key to +nil+, to delete a cookie value you should use +cookies.delete(:key)+. +h3. Rendering xml and json data + +ActionController makes it extremely easy to render +xml+ or +json+ data. If you generate a controller using scaffold then your controller would look something like this. + +<ruby> +class UsersController < ApplicationController + def index + @users = User.all + respond_to do |format| + format.html # index.html.erb + format.xml { render :xml => @users} + end + end +end +</ruby> + +Notice that in the above case code is <tt>render :xml => @users</tt> and not <tt>render :xml => @users.to_xml</tt>. That is because if the input is not string then rails automatically invokes +to_xml+ . + + h3. Filters Filters are methods that are run before, after or "around" a controller action. diff --git a/railties/guides/source/active_support_core_extensions.textile b/railties/guides/source/active_support_core_extensions.textile index cd7a183def..844b9428bd 100644 --- a/railties/guides/source/active_support_core_extensions.textile +++ b/railties/guides/source/active_support_core_extensions.textile @@ -24,7 +24,7 @@ h5. Cherry-picking a Definition The most lightweight way to get +blank?+ is to cherry-pick the file that defines it. -For every single method defined as a core extension this guide has a note that says where is such a method defined. In the case of +blank?+ the note reads: +For every single method defined as a core extension this guide has a note that says where such a method is defined. In the case of +blank?+ the note reads: NOTE: Defined in +active_support/core_ext/object/blank.rb+. @@ -124,7 +124,7 @@ NOTE: Defined in +active_support/core_ext/object/blank.rb+. h4. +duplicable?+ -A few fundamental objects in Ruby are singletons. For example, in the whole live of a program the integer 1 refers always to the same instance: +A few fundamental objects in Ruby are singletons. For example, in the whole life of a program the integer 1 refers always to the same instance: <ruby> 1.object_id # => 3 @@ -209,8 +209,7 @@ String.singleton_class # => #<Class:String> String.new.singleton_class # => #<Class:#<String:0x17a1d1c>> </ruby> -WARNING: Fixnums and symbols have no singleton classes, +singleton_class+ -raises +TypeError+ on them. Moreover, the singleton classes of +nil+, +true+, and +false+, are +NilClass+, +TrueClass+, and +FalseClass+, respectively. +WARNING: Fixnums and symbols have no singleton classes, +singleton_class+ raises +TypeError+ on them. Moreover, the singleton classes of +nil+, +true+, and +false+, are +NilClass+, +TrueClass+, and +FalseClass+, respectively. NOTE: Defined in +active_support/core_ext/kernel/singleton_class.rb+. @@ -256,7 +255,7 @@ NOTE: Defined in +active_support/core_ext/object/acts_like.rb+. h4. +to_param+ -All objects in Rails respond to the method +to_param+, which is meant to return something that represents them as values in a query string, or as a URL fragments. +All objects in Rails respond to the method +to_param+, which is meant to return something that represents them as values in a query string, or as URL fragments. By default +to_param+ just calls +to_s+: @@ -1697,6 +1696,90 @@ foreign_key = options[:foreign_key] || reflection.active_record.name.foreign_key NOTE: Defined in +active_support/core_ext/string/inflections.rb+. +h4. Conversions + +h5. +constantize+ + +The method +constantize+ expects the receiver to contain the name of a constant, and tries to get you the object stored in there, assuming it is defined: + +<ruby> +"ActiveRecord::Base".constantize # => ActiveRecord::Base +</ruby> + +The name is assumed to be top-level, no matter whether it starts with "::" or not. No lexical context is taken into account: + +<ruby> +C = 1 +module M + C = 2 + "C".constantize # => 1, same as "::C".constantize +end +</ruby> + +NOTE: Defined in +active_support/core_ext/string/conversions.rb+. + +h5. +ord+ + +Ruby 1.9 defines +ord+ to be the codepoint of the first character of the receiver. Active Support backports +ord+ for single-byte encondings like ASCII or ISO-8859-1 in Ruby 1.8: + +<ruby> +"a".ord # => 97 +"à".ord # => 224, in ISO-8859-1 +</ruby> + +In Ruby 1.8 +ord+ doesn't work in general in UTF8 strings, use the multibyte support in Active Support for that: + +<ruby> +"a".mb_chars.ord # => 97 +"à".mb_chars.ord # => 224, in UTF8 +</ruby> + +Note that the 224 is different in both examples. In ISO-8859-1 "à" is represented as a single byte, 224. Its single-character representattion in UTF8 has two bytes, namely 195 and 160, but its Unicode codepoint is 224. If we call +ord+ on the UTF8 string "à" the return value will be 195 in Ruby 1.8. That is not an error, because UTF8 is unsupported, the call itself would be bogus. + +INFO: +ord+ is equivalent to +getbyte(0)+. + +NOTE: Defined in +active_support/core_ext/string/conversions.rb+. + +h5. +getbyte+ + +Active Support backports +getbyte+ from Ruby 1.9: + +<ruby> +"foo".getbyte(0) # => 102, same as "foo".ord +"foo".getbyte(1) # => 111 +"foo".getbyte(9) # => nil +"foo".getbyte(-1) # => 111 +</ruby> + +INFO: +getbyte+ is equivalent to +[]+. + +NOTE: Defined in +active_support/core_ext/string/conversions.rb+. + +h5. +to_date+, +to_time+, +to_datetime+ + +The methods +to_date+, +to_time+, and +to_datetime+ are basically convenience wrappers around +Date._parse+: + +<ruby> +"2010-07-27".to_date # => Tue, 27 Jul 2010 +"2010-07-27 23:37:00".to_time # => Tue Jul 27 23:37:00 UTC 2010 +"2010-07-27 23:37:00".to_datetime # => Tue, 27 Jul 2010 23:37:00 +0000 +</ruby> + ++to_time+ receives an optional argument +:utc+ or +:local+, to indicate which time zone you want the time in: + +<ruby> +"2010-07-27 23:42:00".to_time(:utc) # => Tue Jul 27 23:42:00 UTC 2010 +"2010-07-27 23:42:00".to_time(:local) # => Tue Jul 27 23:42:00 +0200 2010 +</ruby> + +Default is +:utc+. + +Please refer to the documentation of +Date._parse+ for further details. + +INFO: The three of them return +nil+ for blank receivers. + +NOTE: Defined in +active_support/core_ext/string/conversions.rb+. + h3. Extensions to +Numeric+ h4. Bytes diff --git a/railties/guides/source/generators.textile b/railties/guides/source/generators.textile index f39451f243..c5b41673e1 100644 --- a/railties/guides/source/generators.textile +++ b/railties/guides/source/generators.textile @@ -33,7 +33,7 @@ $ rails generate helper --help h3. Creating Your First Generator -Since Rails 3.0, generators are built on top of "Thor":http://github.com/wycats/thor. Thor provides power options parsing and a great API for manipulating files. For instance, let's build a generator that creates an initializer file named +initializer.rb+ inside +config/initializers+. +Since Rails 3.0, generators are built on top of "Thor":http://github.com/wycats/thor. Thor provides powerful options parsing and a great API for manipulating files. For instance, let's build a generator that creates an initializer file named +initializer.rb+ inside +config/initializers+. The first step is to create a file at +RAILS_APP/lib/generators/initializer_generator.rb+ with the following content: diff --git a/railties/guides/source/getting_started.textile b/railties/guides/source/getting_started.textile index 7b4f1ca471..6abb3ed9f4 100644 --- a/railties/guides/source/getting_started.textile +++ b/railties/guides/source/getting_started.textile @@ -227,7 +227,7 @@ development: timeout: 5000 </yaml> -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. +NOTE: In this guide we are using an SQLite3 database for data storage, 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. h5. Configuring a MySQL Database diff --git a/railties/guides/source/rails_application_templates.textile b/railties/guides/source/rails_application_templates.textile index 1af6f56957..1bf9cfec33 100644 --- a/railties/guides/source/rails_application_templates.textile +++ b/railties/guides/source/rails_application_templates.textile @@ -51,11 +51,11 @@ h4. gem(name, options = {}) Adds a +config.gem+ entry for the supplied gem to the generated application’s +config/environment.rb+. -For example, if your application depends on the gems +bj+ and +hpricot+ : +For example, if your application depends on the gems +bj+ and +nokogiri+ : <ruby> gem "bj" -gem "hpricot", :version => '0.6', :source => "http://code.whytheluckystiff.net" +gem "nokogiri" </ruby> Please note that this will NOT install the gems for you. So you may want to run the +rake gems:install+ task too : diff --git a/railties/guides/source/routing.textile b/railties/guides/source/routing.textile index 3f6bb66ee5..9ff06856c3 100644 --- a/railties/guides/source/routing.textile +++ b/railties/guides/source/routing.textile @@ -444,6 +444,21 @@ This route would match URLs such as +/photo/A12345+. You can more succinctly exp match 'photo/:id' => 'photos#show', :id => /[A-Z]\d{5}/ </ruby> ++:constraints+ takes regular expression. However note that regexp anchors can't be used within constraints. For example following route will not work: + +<ruby> +match '/:id' => 'posts#show', :constraints => {:id => /^\d/} +</ruby> + +However, note that you don't need to use anchors because all routes are anchored at the start. + +For example, the following routes would allow for +posts+ with +to_param+ values like +1-hello-world+ that always begin with a number and +users+ with +to_param+ values like +david+ that never begin with a number to share the root namespace: + +<ruby> +match '/:id' => 'posts#show', :constraints => { :id => /\d.+/ } +match '/:username' => 'users#show' +</ruby> + h4. Request-Based Constraints You can also constrain a route based on any method on the <a href="action_controller_overview.html#the-request-object">Request</a> object that returns a +String+. diff --git a/railties/guides/w3c_validator.rb b/railties/guides/w3c_validator.rb index 49cfb984cf..4da48bf3fb 100644 --- a/railties/guides/w3c_validator.rb +++ b/railties/guides/w3c_validator.rb @@ -55,6 +55,7 @@ module RailsGuides private def guides_to_validate guides = Dir["./guides/output/*.html"] + guides.delete("./guides/output/layout.html") ENV.key?('ONLY') ? select_only(guides) : guides end |