这似乎很容易做,但我不知道如何草草。
我有“国家”和“规约”的模式,目前使用的途径如下:
/california/statutes/robbery
/newyork/statutes/burglary使用这条路线:
match '/:state_id/statutes/:id' => 'statutes#show', :as => :state_statute, :id => /[^\/]+/但问题是,在加州,法规被称为代码。在纽约,他们是法律。
我的问题是,我如何能够自动支持这些更有意义的路径:
/california/codes/robbery
/newyork/laws/burglary我将这些信息存储在模型中,可以使用;Statute.meta_name。
发布于 2012-07-16 08:28:29
这应该是可行的:
match '/:state_id/:law/:id' => 'statutes#show', :as => :state_statute, :id => /[^\/]+/, :law => Regexp.new(Statute.meta_name.join("|"))问题是,这两个urls都能工作:
/california/laws/robbery
/newyork/laws/burglary这通常对SEO不利。您可以通过添加一个前置筛选器来解决这个问题,例如:
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中,或者最好是一个库文件中,可以添加:
# 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)
endhttps://stackoverflow.com/questions/11500401
复制相似问题