Difference between revisions of "Ruby on Rails"

From Hawk Wiki
Jump to: navigation, search
 
(4 intermediate revisions by the same user not shown)
Line 1: Line 1:
<h3>Tutorials</h3>
+
==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 5: Line 5:
 
http://www.ruby-lang.org/en/documentation/quickstart/<br>
 
http://www.ruby-lang.org/en/documentation/quickstart/<br>
  
<h3>nil?, empty? and blank?</h3>
+
==nil?, empty? and blank?==
.nil? can be used on any object and is true if the object is nil.<br>
+
<pre class="brush:ruby">
 +
.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:<br>
+
.empty? can be used on strings, arrays and hashes and returns true if:
  
String length == 0<br>
+
String length == 0
Array length == 0<br>
+
Array length == 0
Hash length == 0<br>
+
Hash length == 0
Running .empty? on something that is nil will throw a NoMethodError.<br>
+
  
.blank? will operate on any object<br>
+
Running .empty? on something that is nil will throw a NoMethodError.
nil.blank? == true<br>
+
 
false.blank? == true<br>
+
.blank? will operate on any object
[].blank? == true<br>
+
nil.blank? == true
{}.blank? == true<br>
+
false.blank? == true
"".blank? == true<br>
+
[].blank? == true
5.blank? == false<br>
+
{}.blank? == true
"  ".blank? == true<br>
+
"".blank? == true
"  ".empty? == false<br>
+
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 -= !)