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

Posted . Visible to the public.

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"}

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.

Martin Straub
Last edit
Martin Straub
Keywords
rspec
License
Source code in this card is licensed under the MIT License.
Posted by Martin Straub to makandra dev (2016-03-11 15:21)