如何指定:DRY方式中Rails 3中多条路由的默认值?

时间:2012-08-06 11:12:36

标签: ruby ruby-on-rails-3 routes

我有几条这样的路线:

get '/test1' => 'test#index1', defaults: {common: '123'}
get '/test2' => 'test#index2', defaults: {common: '123'}

他们的规格如下:

specify do
  get('/test1').should route_to controller: 'test', action: 'index1', common: '123'
end
specify do
  get('/test2').should route_to controller: 'test', action: 'index2', common: '123'
end

如何禁止使用默认值?

我尝试过像这样使用with_options

with_options defaults: {common: '123'} do |o|
  o.get '/test1' => 'test#index1'
  o.get '/test2' => 'test#index2'
end

但它打破了第一次测试的消息:

Failure/Error: get('/test1').should route_to controller: 'test', action: 'index1', common: '123'
       The recognized options <{"common"=>"123", "controller"=>"test", "action"=>"index2"}> did not match <{"controller"=>"test", "action"=>"index1", "common"=>"123"}>, difference: <{"action"=>"index1"}>.
       <{"controller"=>"test", "action"=>"index1", "common"=>"123"}> expected but was
       <{"common"=>"123", "controller"=>"test", "action"=>"index2"}>.

我做错了吗?或者还有另一种方式吗?

1 个答案:

答案 0 :(得分:1)

此处您不需要with_optionsdefaults也接受阻止:

defaults common: '123' do
  get '/test1' => 'test#index1'
  get '/test2' => 'test#index2'
end