Rails 4路由中的约束范围

时间:2013-03-05 16:29:46

标签: routes ruby-on-rails-4

是否可以在Rails 4中创建约束范围,如here

中所述

的routes.rb

scope format: true, constraints: { format: 'json' } do
  get '/bar' => "bar#index_with_json"
end

我得到的错误是

NoMethodError (undefined method 'source' for "json":String):
  config/routes.rb:17:in `block (2 levels) in <top (required)>'
  config/routes.rb:16:in `block in <top (required)>'
  config/routes.rb:1:in `<top (required)>'

1 个答案:

答案 0 :(得分:2)

找到它,格式类型必须是斜杠:

的routes.rb

scope format: true, constraints: { format: /json/ } do
  get '/bar' => "bar#index_with_json"
end