Skip to content

Instantly share code, notes, and snippets.

@derekprior
Created August 19, 2015 17:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save derekprior/3015a311182a34e0aa3a to your computer and use it in GitHub Desktop.
Save derekprior/3015a311182a34e0aa3a to your computer and use it in GitHub Desktop.
Limit resourceful routes to used actions

By default, a `resource` or `resources` call in the routes will create
routes for all 7 of the standard rails actions. Most of the resources
here do not use all of those actions, and allowing all of the routes to
exist can lead to much confusion.

For instance, consider the following:

```ruby
resources :user

class UsersController
  def index
    @users = User.all
  end
end
```

That looks like the controller only supports `index`, but we can't
actually be certain by only looking at the controller. If an action is
dispatched to a controller, but no action method exists for that action
rails will still try to render the template that corresponds to the
action name. So, in this case, if we send the `show` action to that
controller the action will still work if there's a `users/show` template
in the views directory. This is a bad practice because it's confusing

Because of this I had to look through the controllers *and* the views
directory to see which actions were actually renderable. Some of the
views that existed without corresponding controller action methods
seemed like they might be boilerplate or not actually in use, but to
limit the possibility of something going wrong, I included those actions
as routable.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment