Read more

How to fix routing error when using concerns in Rails up to 3.2.22.1

Martin Straub
March 11, 2016Software engineer at makandra GmbH

tl;dr

  • Don't write resources :people, :concerns => :trashable

  • Write

    resources :people do
      concerns :trashable
    end
    

Why

Writing a controller spec I encountered this error:

Failure/Error: get :index
ActionController::RoutingError:
  No route matches {:controller=>"people"}
Illustration online protection

Rails Long Term Support

Rails LTS provides security patches for old versions of Ruby on Rails (2.3, 3.2, 4.2 and 5.2)

  • Prevents you from data breaches and liability risks
  • Upgrade at your own pace
  • Works with modern Rubies
Read more Show archive.org snapshot

caused by this route definition

resources :people, :concerns => :trashable

which renders strange routes:

      trash_person PUT    /people/:id/trash(.:format)             people#check {:concerns=>:trashable}
            people GET    /people(.:format)                       people#index {:concerns=>:trashable}
                   POST   /people(.:format)                       people#create {:concerns=>:trashable}
        new_person GET    /people/new(.:format)                   people#new {:concerns=>:trashable}
       edit_person GET    /people/:id/edit(.:format)              people#edit {:concerns=>:trashable}
            person GET    /people/:id(.:format)                   people#show {:concerns=>:trashable}
                   PUT    /people/:id(.:format)                   people#update {:concerns=>:trashable}
                   DELETE /people/:id(.:format)                   people#destroy {:concerns=>:trashable}

Note the {:concerns=>:trashable} at the end of each route.

The fix

… was to write the routes with another syntax …

resources :people do
  concerns :trashable
end

… and now my routes are fine.

Posted by Martin Straub to makandra dev (2016-03-11 16:21)