使用するルートを選択するときに、url_forをレンダリングしてto_paramを考慮に入れるのに問題があります。
同じモデル(Foo)を使用する2セットのルートがあります。Foo.is_specialの場合、URLは/ special /:actionにマップする必要があります。そうでない場合は、/:id /:actionにマップする必要があります。同じモデルなので、is_specialに応じて、どのパスにマップするかをurl_forに自動的に知らせたいと思います。
ルート.rb:
map.special 'special/:action', :controller => 'bar', :id => 'special'
map.regular ':id/:action', :controller => 'bar', :id => /\d+/
foo.rb:
def to_param
is_special ? 'special' : id.to_s
end
これは、:idを明示的に設定すると機能します。例えば:
url_for(:controller => 'bar', :id => 'special')
url_for(:controller => 'bar', :id => @foo)
:idが明示的に'special'に設定されている場合、および@foo is_special == falseの場合、specialの正しいURLを生成します。ただし、@ foo.is_special == trueの場合、特別なルートは認識されません。