mercredi 14 juin 2017

Passing a default parameter to a Rails 3 route makes it fail to match the route in the controller test

I'm upgrading an old application for a non-profit, and I've having issues with some routes and controller tests on Rails 3.0.20 (Rails 2 -> 3 is the first of many upgrades I need to make :( )

This is my route definition:

match "new/tribes" => "news#tribes",
  as: "en_news_tribes",  
  defaults: {
    page_slug: 'tribes',
    language_id: 3,
    section_id: 4
  }

The idea is that when the controller is called the parameters will include the default values for page_slug, language_id and section_id

In my controller test (TestUnit based) the action is invoked like this:

get :tribes, { section_id: 4, language_id: 4, tribe_id: @loc_tribe.tribe.id }

But this produces this routing exception:

ActionController::RoutingError: No route matches {:section_id=>4, :language_id=>3, :tribe_id=>1, :controller=>"news", :action=>"tribes"}

Only if I specify all the default parameters does it find a route (and the test passes):

get :tribes, { section_id: 4, language_id: 4, tribe_id: @loc_tribe.tribe.id, page_slug: 'tribes' }

If the parameters I pass don't match the default parameter values specified in config/routes.rb it doesn't find the route.

i.e. This doesn't work:

get :tribes, { section_id: 4, language_id: 4, tribe_id: @loc_tribe.tribe.id, page_slug: 'Abc123' }

So it seems like these default values are in fact behaving as routing constraints?

I hope someone can help! I am at the start of a long road in getting this application to Rails 5 ;)

Aucun commentaire:

Enregistrer un commentaire