Hello rails-enjoyers!
I have ran multiple times in error-phone issue when defining and then accessing routes. See the following route config
# config/routes.rb
namespace :companies do
# many other routes definitions
# any typo named resource
resources :company_user # should be plural
end
Then for example I’m trying to curl that url provided by rails routes
or I have to write rspec controller test for that resource
# spec/controllers/company_users_controller_spec.rb
# RSpec.describe controller_name and other code
it 'returns list' do
response = get('/api/v1/companies/1/company_users') # here I use plural form and forgot about typo in config/routes
expect(response.status).to eq(200)
end
And then I receive very clear message that route does not exist
Failure/Error: raise ActionController::RoutingError, "No route matches [#{env['REQUEST_METHOD']}] #{env['PATH_INFO'].inspect}"
ActionController::RoutingError:
No route matches [GET] "/api/v1/companies/2/company_users"
and then you can spend some time understanding what’s going on exactly, wondering why route is not defined. I believe that this behaviour should be improved following the ruby The Principle of Least Surprise. I would like to see suggestions like it is done in (for example in DidYouMean) The list with suggestion would return programmer to the code and he will understand that the issue is only in naming (either in url from spec/curl or in config/routes definition) and with no time waste edit it and back to solving other tasks.
Actually did a bit investigation it is already solved when calling the ActionView::Helpers::UrlHelper
api_v1_company_company_users_path(company_id: 1)
ActionController::UrlGenerationError: No route matches {:action=>"index", :company_id=>1, :controller=>"api/v1/companies/1/company_users", :format=>"json"}, missing required keys: [:id]
Did you mean? api_v1_company_company_user_url # and other suggestions
Let’s discuss and see caveats (maybe it is hard to implement due to middleware context or any other complexity could be met) I can handle that feature and ease life for devs, but only if you think that this will help community (not only unfocused/hurry-sicked people like me, I can implement patch for myself )