带有约束的Rails路由

时间:2012-06-14 18:32:40

标签: ruby-on-rails routing

我正在尝试捕获UTF-8网址。通常,使用约束这很有效。对于URL:/international-delight-iced-coffee/路由有效:

match ":post_name", :constraints => { :post_name => /.+/}}

一切都很好。失败的地方是我需要使用高级约束时:

match ":post_name", :constraints => Post.new

# inside Post.rb
self.matches?(request)
  puts ">>>> Arrived at matches!"
  Post.find_by_name(request.path_parameters[:post_name])
end

self.matches永远不会被调用。任何想法如何使约束捕获?

先谢谢了! 贾斯汀

2 个答案:

答案 0 :(得分:0)

你是否需要你想要匹配的控制器/动作(下面是to参数)。

我有这样的事情:

match "/:vanity", to: 'vanity#routing', constraints: RoutingConstraint.new

我的RoutingConstraint#matches?方法 被点击。

我用你的UTF-8网址尝试了这个,看起来效果很好。

答案 1 :(得分:0)

您的.matches?方法是一种类方法,但您将Post.new作为约束传递。将Post.new替换为Post,或从方法定义中删除self.