Created
June 4, 2012 06:10
-
-
Save matthieua/2866627 to your computer and use it in GitHub Desktop.
Rails: Cheatsheet
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Debug | |
## In a view | |
<%= debug @object %> | |
<%= debug 'string' %> | |
<%= 'string'.inspect %> | |
##In a controller / model | |
logger.debug "The object is #{@object}" | |
RAILS_DEFAULT_LOGGER.debug @object | |
## Controllers only | |
return render :text => "The object is #{@object}" | |
## Exceptions | |
# exception class | |
class CustomException < Exception; end | |
# create an exception | |
CustomException.new("This is the exception message") | |
# catch the exception | |
begin | |
# block of code | |
rescue CustomException.new=> e | |
render :text => e.to_s, :content_type => 'text/plain', :status => 500 | |
rescue Exception => e | |
# deal with other errors | |
else | |
# good, no exception surfaced! | |
ensure | |
# good or bad, this needs to be done | |
end | |
# best practises | |
# never catch all the exceptions, be specific | |
# Do not place return or break in ensure sections and try to avoid throwing exceptions from them. | |
# Place rescue clauses for more specific exceptions (sub classes) before those for less specific ones (super classes). | |
# rescue the most specific exception you can handle. | |
# Do not rescue exceptions that you cannot or do not want to handle. | |
# Models | |
## Touch | |
user.updated_at #=> "Wed Jan 27 23:29:22 +1300 2010" | |
user.touch | |
user.updated_at #=> "Wed Jan 27 23:30:08 +1300 2010" | |
## deep clone | |
deep_cloned = Marshal::load(Marshal.dump(origin)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Debug | |
## In a view | |
<%= debug @object %> | |
<%= debug 'string' %> | |
<%= 'string'.inspect %> | |
##In a controller / model | |
logger.debug "The object is #{@object}" | |
RAILS_DEFAULT_LOGGER.debug @object | |
## Controllers only | |
return render :text => "The object is #{@object}" | |
## Exceptions | |
# exception class | |
class CustomException < Exception; end | |
# create an exception | |
CustomException.new("This is the exception message") | |
# catch the exception | |
begin | |
# block of code | |
rescue CustomException.new=> e | |
render :text => e.to_s, :content_type => 'text/plain', :status => 500 | |
rescue Exception => e | |
# deal with other errors | |
else | |
# good, no exception surfaced! | |
ensure | |
# good or bad, this needs to be done | |
end | |
# best practises | |
# never catch all the exceptions, be specific | |
# Do not place return or break in ensure sections and try to avoid throwing exceptions from them. | |
# Place rescue clauses for more specific exceptions (sub classes) before those for less specific ones (super classes). | |
# rescue the most specific exception you can handle. | |
# Do not rescue exceptions that you cannot or do not want to handle. | |
# Models | |
## Touch | |
user.updated_at #=> "Wed Jan 27 23:29:22 +1300 2010" | |
user.touch | |
user.updated_at #=> "Wed Jan 27 23:30:08 +1300 2010" | |
## deep clone | |
deep_cloned = Marshal::load(Marshal.dump(origin)) | |
- Comments | |
# TODO | |
# FIXME | |
# OPTIMIZE | |
# MATT | |
then run `rake notes` or `rake notes:fixme` or `rake notes:custom ANNOTATION=MATT` | |
- Run helper methods from the console | |
helper.number_to_currency(100) | |
- Show database status | |
rake db:migrate:status | |
- Merge nested hashes | |
{nested: {one: 1}}.deep_merge{nested: {two: 2}} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment