Wednesday, October 16, 2013

Notes - Rails Tutorial Chapter 3.1 - Static Pages

I set the sample_app on GitHub and Herouku.

underscore method

Rails' ActiveSupport adds underscore to the String methods using the following code (This is NOT a String method in Ruby)

class String
  def underscore
    self.gsub(/::/, '/').
    gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
    gsub(/([a-z\d])([A-Z])/,'\1_\2').
    tr("-", "_").
    downcase
  end
end
underscore(camel_cased_word)
Makes an underscored, lowercase form from the expression in the string.

Changes ‘::’ to ‘/’ to convert namespaces to paths.

  'ActiveModel'. underscore    # => "active_model"
  'ActiveModel::Errors'.underscore    # => "active_model/errors"


As a rule of thumb you can think of it as the inverse of camelize, though there are cases where that does not hold:
'SSLError'
.underscore.camelize # => "SslError"

CREATE & DESTROY

$ rails generate controller FooBars baz quux
$ rails destroy controller FooBars baz quux
$ rails generate model Foo bar:string baz:integer
$ rails destroy model Foo
$ rake db:migrate
  We can undo a single migration step using
$ rake db:rollback
  To go all the way back to the beginning, we can use
$ rake db:migrate VERSION=0

No comments:

Post a Comment