Difference between revisions of "Ruby on Rails"

From Hawk Wiki
Jump to: navigation, search
 
(7 intermediate revisions by the same user not shown)
Line 1: Line 1:
 +
==Tutorials==
 
Beginner's tutorial<br>
 
Beginner's tutorial<br>
 
http://allaboutruby.wordpress.com/2006/01/09/installing-rails-on-windows-step-by-step-tutorial/<br>
 
http://allaboutruby.wordpress.com/2006/01/09/installing-rails-on-windows-step-by-step-tutorial/<br>
Line 4: Line 5:
 
http://www.ruby-lang.org/en/documentation/quickstart/<br>
 
http://www.ruby-lang.org/en/documentation/quickstart/<br>
  
--nil? empty? and blank?--
+
==nil?, empty? and blank?==
 +
<pre class="brush:ruby">
 
.nil? can be used on any object and is true if the object is nil.
 
.nil? can be used on any object and is true if the object is nil.
  
Line 12: Line 14:
 
Array length == 0
 
Array length == 0
 
Hash length == 0
 
Hash length == 0
 +
 +
Running .empty? on something that is nil will throw a NoMethodError.
 +
 +
.blank? will operate on any object
 +
nil.blank? == true
 +
false.blank? == true
 +
[].blank? == true
 +
{}.blank? == true
 +
"".blank? == true
 +
5.blank? == false
 +
"  ".blank? == true
 +
"  ".empty? == false
 +
</pre>
 +
<h3>How to skip active record in rails 3</h3>
 +
1. Create new rails project with
 +
<pre class="brush:ruby">
 +
rails new myApp --skip-activerecord
 +
</pre>
 +
2.
 +
 +
In application.rb, remove the require 'rails/all' line and instead add these lines:
 +
<pre class="brush:ruby">
 +
require "action_controller/railtie"
 +
require "action_mailer/railtie"
 +
require "active_resource/railtie"
 +
require "rails/test_unit/railtie"
 +
require "sprockets/railtie"
 +
</pre>
 +
Also see Remove ActiveRecord in Rails 3 and look into the Active Model railscast
 +
 +
Rails 3.2.x:
 +
 +
You'll also need to remove/comment out this line in application.rb
 +
<pre class="brush:ruby">
 +
config.active_record.whitelist_attributes = true
 +
</pre>
 +
And remove/comment these two lines from development.rb
 +
<pre class="brush:ruby">
 +
config.active_record.mass_assignment_sanitizer = :strict
 +
config.active_record.auto_explain_threshold_in_seconds = 0.5
 +
</pre>
 +
Rails 2.x:
 +
 +
In config/environment.rb add (or uncomment) the line
 +
<pre class="brush:ruby">
 +
config.frameworks -= [ :active_record, :active_resource, :action_mailer ]
 +
</pre>
 +
This will cause Rails not to use those frameworks. (Note the nearly-invisible -= !)

Latest revision as of 00:20, 20 February 2015

Tutorials

Beginner's tutorial
http://allaboutruby.wordpress.com/2006/01/09/installing-rails-on-windows-step-by-step-tutorial/
Ruby in 20 minutes
http://www.ruby-lang.org/en/documentation/quickstart/

nil?, empty? and blank?

.nil? can be used on any object and is true if the object is nil.

.empty? can be used on strings, arrays and hashes and returns true if:

String length == 0
Array length == 0
Hash length == 0

Running .empty? on something that is nil will throw a NoMethodError.

.blank? will operate on any object
nil.blank? == true
false.blank? == true
[].blank? == true
{}.blank? == true
"".blank? == true
5.blank? == false
"  ".blank? == true
"  ".empty? == false

How to skip active record in rails 3

1. Create new rails project with

rails new myApp --skip-activerecord

2.

In application.rb, remove the require 'rails/all' line and instead add these lines:

require "action_controller/railtie"
require "action_mailer/railtie"
require "active_resource/railtie"
require "rails/test_unit/railtie"
require "sprockets/railtie"

Also see Remove ActiveRecord in Rails 3 and look into the Active Model railscast

Rails 3.2.x:

You'll also need to remove/comment out this line in application.rb

config.active_record.whitelist_attributes = true

And remove/comment these two lines from development.rb

config.active_record.mass_assignment_sanitizer = :strict
config.active_record.auto_explain_threshold_in_seconds = 0.5

Rails 2.x:

In config/environment.rb add (or uncomment) the line

config.frameworks -= [ :active_record, :active_resource, :action_mailer ]

This will cause Rails not to use those frameworks. (Note the nearly-invisible -= !)