这应该有效:
match '/:state_id/:law/:id' => 'statutes#show', :as => :state_statute, :id => /[^\/]+/, :law => Regexp.new(Statute.meta_name.join("|"))
这样做的问题是这两个网址都可以工作:
/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