Search code examples
ruby-on-railsrestrails-routingruby-on-rails-5

Routing for sessions#destroy action


I'm linking to the destroy action for the Sessions controller like this:

<%= link_to "Sign out", session_path, method: :delete  %>

Routes.rb:

resources :sessions, only: [:new, :create, :destroy]

Rails complains about the link above:

No route matches {:action=>"destroy", :controller=>"sessions"} missing required keys: [:id]

How do I link to the destroy action and keeping the REST/resource methodology in Rails when there's no object ID to provide for the link?


Solution

  • It is best to treat the routes to your sessions controller as a singular resource

    routes.rb

    resource :sessions
    

    Doc: http://guides.rubyonrails.org/routing.html#singular-resources

    This will give you a route that you can use without ID's

    DELETE /sessions sessions#destroy