これは機能するはずです:
match '/:state_id/:law/:id' => 'statutes#show', :as => :state_statute, :id => /[^\/]+/, :law => Regexp.new(Statute.meta_name.join("|"))
これに伴う問題は、これらのURLが両方とも機能することです。
/california/laws/robbery
/newyork/laws/burglary
これは一般的にSEOにとって悪いことです。次のようなbeforeフィルターを追加することでこれを修正できます。
before_filter :validate_law_title
def validate_law_title
unless <condition to check if the title used is correct, ie, codes for cali, laws for NY>
redirect_to <correctly generated url>, :status=>:moved_permanently
end
end
- 編集 -
ルートの生成を簡単にするには、次のようなルートを使用します。
match '/:state_id/:law/:id' => 'statutes#show', :as => "_state_statute", :id => /[^\/]+/, :law => Regexp.new(Statute.meta_name.join("|"))
そして、application_controller、またはできればlibファイルに、次のものを追加できます。
# law is the law/rule, etc object
def state_statute_path(law, options={})
options.merge!(:law => <figure out the label to use from the law object>)
_state_statute_path(options)
end