tim_xyz
tim_xyz

Reputation: 13551

How to manually specify a :delete request in the URL?

Situation: I want to destroy the current session in Rails, and am currently signed into an admin model setup via devise.

I thought it would be enough to input site.io/admins/sign_out into the URL, but this assumes a GET request and doesn't work.

No route matches [GET] "/admins/sign_out"

A method: :delete request needs to be made to destroy the session.

Can something be done like site.io/admins/sign_out{action:delete}?

UPDATE

Per request, this is the route related to admin.

devise_for :admins

Upvotes: 0

Views: 679

Answers (4)

Pitabas Prathal
Pitabas Prathal

Reputation: 1012

No you can not manually type in the link on the browser and log it out because in the browser you can't specify PUT POST or Delete.If you define the logout path as GET Method you can directly enter the path and log it out as browser by default gives a GET method. you can do it on Rest Client like postman like below

http://localhost:3000/users/sign_out.html

select method as DELETE

Upvotes: 0

HarlemSquirrel
HarlemSquirrel

Reputation: 10174

To log out with devise you need to POST to /admins/sign_out. I use rails link_to to help with this.

<%= link_to "Log Out", destroy_admin_session_path, method: :delete %>

You could also do it without ERB or link_to

<a rel="nofollow" data-method="delete" href="/admins/sign_out">Log Out</a>

For user model, just replace admin with user

<%= link_to "Log Out", destroy_user_session_path, method: :delete %>

or

<a rel="nofollow" data-method="delete" href="/users/sign_out">Log Out</a>

Source: https://github.com/plataformatec/devise/wiki/How-To:-Add-sign_in,-sign_out,-and-sign_up-links-to-your-layout-template

Upvotes: 1

Pravesh Khatri
Pravesh Khatri

Reputation: 2264

If you inject site.io/admins/sign_out forcefully.

It will send you to the show action of the controller with Parameters: {"id"=>"sign_out"}. Because It assumes that, it is a show action rather than calling the Delete function.

So, I think it is not possible, to forcefully use delete method directly from URL.

Upvotes: 0

try this:

<%= link_to "Sign Out", destroy_admin_session_path, :method => :delete %>

Upvotes: 1

Related Questions