Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 783
Fellow Stranger Avatar asked Jun 15 '16 08:06

Fellow Stranger


People also ask

What is session routing?

For sessions with adjacent nodes, direct session routing. For sessions that traverse one or more intermediate nodes, one of the following: Intermediate session routing (ISR), which provides a route that does not change during the course of the session.

What is session smart routing?

The Session Smart Router integrates multiple middlebox capabilities (security, routing, firewall, VPN, and load balancer) into a single routing platform. This simplifies the overall network architecture and minimizes the costs and deployment time for new network functions.

What is routing IP address?

IP Routing is an umbrella term for the set of protocols that determine the path that data follows in order to travel across multiple networks from its source to its destination. Data is routed from its source to its destination through a series of routers, and across multiple networks.

What is dynamic routing protocol?

Dynamic routing is a mechanism through which routing information is exchanged between routers to determine the optimal path between network devices. A routing protocol is used to identify and announce network paths.


2 Answers

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

like image 157
Andrew Cetinic Avatar answered Oct 03 '22 02:10

Andrew Cetinic


destroy is a member route, you need to pass id in the params to make it work, but you can do this to convert it to a collection route

resources :sessions, only: [:new, :create] do
  delete :destroy, on: :collection
end

Hope that helps!

like image 25
Rajdeep Singh Avatar answered Oct 03 '22 01:10

Rajdeep Singh