This file contains 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
# app/controllers/articles_controller.rb | |
respond_with :js | |
def create | |
flash[:notice] = 'Hello World' | |
end |
This file contains 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
<!-- app/views/layouts/application.html.erb. --> | |
<!-- inside <head> --> | |
<script type="text/javascript"> | |
<% update_flash %> | |
</script> |
This file contains 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
// app/views/articles/create.js.erb | |
<% update_flash %> | |
// javascript code... |
This file contains 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
# app/helpers/application_helper.rb | |
def update_flash | |
[:notice, :error].each do |type| | |
message = flash[type] | |
concat "$('#flash_#{type}').html('message')" | |
end | |
end |
This file contains 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
# application_controller.rb | |
before_filter :april_fool | |
def april_fool | |
unless logged_in? && session[:april_fooled] | |
session[:april_fooled] = true | |
flash.now[:april_fool] = 'error 10.4.1: you may have corrupted data. contact administrator immediately.' | |
# get it? 2010.4.1! | |
end |
This file contains 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
# convert results of calling methods into attributes hash. | |
# e.g. Date.today.attributes_for(:day, :month, :year) would return | |
# {:day => 13, :month => 3, :year => 2010} | |
class Object | |
def attributes_for(*method_names) | |
method_names.inject({}) do |hash, method_name| | |
hash[method_name] = send(method_name) | |
hash | |
end |
This file contains 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
# rollback whatever was done in this block. | |
class ActiveRecord::Base | |
def self.sand_boxed | |
transaction do | |
yield | |
raise ActiveRecord::Rollback | |
end | |
end | |
NewerOlder