我有一个像http://domain.com/1and2这样的网址,我想在config / routes.rb中设置如下:
匹配“1and2”=> “头版#oneandtwo”
(控制器和视图到位)。
运行'rake routes'输出'无效路径名:'1and2''。当您使用数字字符开始匹配时,显然会触发此错误。
有解决方法,还是我做错了?
答案 0 :(得分:3)
match '/:id' => "frontpage#oneandtwo", :constraints => {:id => /1and2/}
答案 1 :(得分:1)
问题的根源是Ruby中的方法不能以数字开头。由于Rails路由将为每个路由生成一个访问器方法,因此您将收到错误。
您可以通过使用:as
参数以不同方式命名您的路线来传递问题。
我遇到了一个问题,我想从URI / 2012重定向 - 导致错误。我通过在路由中添加:as => current_year
来更正它:
match "/#{Time.now.year}" => redirect("..."), :as => :current_year
更多信息: