Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New cop suggestion: check if a defined route has a corresponding controller action #22

Closed
AlexVPopov opened this issue Dec 19, 2018 · 4 comments

Comments

@AlexVPopov
Copy link

My suggestion is to have a cop, which checks, that all defined routes in config/routes.rb have corresponding controller actions. For example, if we define:

resources :countries

the following routes are generated:

                countries GET    /countries(.:format)                                                                     countries#index
                          POST   /countries(.:format)                                                                     countries#create
              new_country GET    /countries/new(.:format)                                                                 countries#new
             edit_country GET    /countries/:id/edit(.:format)                                                            countries#edit
                  country GET    /countries/:id(.:format)                                                                 countries#show
                          PATCH  /countries/:id(.:format)                                                                 countries#update
                          PUT    /countries/:id(.:format)                                                                 countries#update
                          DELETE /countries/:id(.:format)                                                                 countries#destroy

However, if the controller has only implemented the #index action:

class CountriesController < ApplicationController
  def index
    render json: Country.all
  end
end

the cop should raise an error/warning.

@andyw8
Copy link
Contributor

andyw8 commented Dec 19, 2018

One slight complication: It's possible to have a view template be rendered even if there is no corresponding controller action, if it's named accordingly. (But I normally prefer to be explicit and have an empty controller method).

@AlexVPopov
Copy link
Author

@andyw8 Andy, I guess this could be integrated into the logic of the cop, it's a matter of catching all edge cases, I assume.

@ghiculescu
Copy link
Contributor

Check out https://github.com/daveallie/rails-route-checker

@koic
Copy link
Member

koic commented Mar 28, 2020

Yeah, I would expect rails-route-checker to solve this.

@koic koic closed this as completed Mar 28, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants