Silencing Passenger
When using Rails 2.3 and Passenger, you can do yourself a favor by adding this line to config/silencers/backtrace_silencer.rb
[sourcecode language='ruby']
Rails.backtrace_cleaner.add_silencer { |line| line =~ /^\s*passenger/ }
[/sourcecode]

Saves you scrolling through the endless backtraces passenger gives you for free :)
PS. A colleague tweeted this lovely backtrace of a spring with grails error. I say: backtrace cleaner FTW!
Filtering with named scopes (encore)
In my previous post, I talked about making filters using named scopes. To summorize:
I like the method of using a named_scope and delegating to specified filters. This way, you can structure your filters properly and get clean URLs. Also, you can chain other named scopes to the filter.
If you find yourself making an administrative web application, with many tables and filters, here’s an example to make it a little more DRY.
Read the rest of this entry »
Filtering with named scopes
Suppose you have an index page with people and you want to have a series of neat filters to show a selection of people. For example only the people still alive of only the adults. How would one do that?
I like the method of using a named_scope and delegating to specified filters. This way, you can structure your filters properly and get clean URLs. Also, you can chain other named scopes to the filter.
This is an example of how I would do that.
Read the rest of this entry »
Writing YAML files
A short one for today: How do I write YAML files?
Well, to get the prettiest results, I do something like this:
def write(filename, hash)
File.open(filename, "w") do |f|
f.write(yaml(hash))
end
end
def yaml(hash)
method = hash.respond_to?(:ya2yaml) ? :ya2yaml : :to_yaml
string = hash.deep_stringify_keys.send(method)
string.gsub("!ruby/symbol ", ":").sub("---","").split("\n").map(&:rstrip).join("\n").strip
end
Nested Forms
The old subject of nested forms comes back again to hunt me. Rails 2.3 has the new and shiny accepts_nested_attributes_for feature. I like it, but there are some things to take into consideration. Adding a child object through javascript remains a bitch to tackle. So I sat down and wrote some javascript. Here is what I came up with. Not sure if I’m going to release this a plugin though.
First of, build the models. I have a project with many stages:
[sourcecode language='ruby']
class Project < ActiveRecord::Base
validates_presence_of :name
has_many :stages
accepts_nested_attributes_for :stages, :allow_destroy => true
end
class Stage < ActiveRecord::Base
validates_presence_of :title
belongs_to :project
end
[/sourcecode]
Read the rest of this entry »