No route matches [GET] "/logout" - ruby-on-rails

In my session controller I have a method called destroy who is pointed in the route as "Logout", the function of it is only reset the session variable to nil and redirect to the store index:
def destroy
session[:user_id] = nil
redirect_to store_url, notice: "Logged out"
end
In the route file I declared the pointer:
get "sessions/destroy"
controller :sessions do
get 'login' => :new
post 'login' => :create
delete 'logout' => :destroy
end
Now in the browser I should trigger the HTTP DELETE VERB, but instead it takes the GET and the route can't be found it.
For example:
localhost:3000/logout, the error is No route matches [GET] "/logout"

In the link you should put explicit method: :destroy, like this:
= link_to 'Destroy', session, method: :destroy

If you look into your Devise initializer in config/initializers/devise.rb and seek through line 236 and switch to :get if you want your users to sign out via a GET request.
config.sign_out_via = :get

I had the same error..
this worked for me.
Just changed the routes from..
delete 'logout', to: 'sessions#destroy'
#to
get 'logout', to: 'sessions#destroy'

Related

Ruby-on-rails - Destroy - Error: No route matches [GET]

I am using destroy
In my view, I have:
#delete_archive_modal.modal.fade
.modal-header
%h3
%i.icon-exclamation-sign
Attention
.modal-body
%p= "Are you sure you want to delete this portal?"
.modal-footer
%a.btn{"data-dismiss" => "modal", :href => "#"} Cancel
= link_to 'Delete', delete_portal_path(portal)
Routes:
resources :portals do
resources :pill_tabs, only: [:show, :edit]
resources :page_urls do
collection do
get :redirects
end
end
resources :zero_touch_configs do
member do
get :history
end
end
member do
get :navigation
get :history
get :sitemap
get :url_list
post :generate_sitemap
post :add_modules
post :archive
post :delete
end
collection do
get :index, path: '/'
get :new, path: '/new(/:portal_type)'
get :accessible_sites
get :archive_index
get :delete
end
And in my controller:
def destroy
#portal = Portal.find(params[:id])
#portal.destroy
flash[:notice] = 'Portal deleted successfully.'
redirect_to action: :archive_index
end
routes:
portals GET /portals(.:format) portals#index
GET /portals/new(/:portal_type)(.:format) portals#new
accessible_sites_portals GET /portals/accessible_sites(.:format) portals#accessible_sites
archive_index_portals GET /portals/archive_index(.:format) portals#archive_index
delete_portals GET /portals/delete(.:format) portals#delete
history_portal_stack_wrapper GET /portals/:portal_id/stack_wrappers/:id/history(.:format) stack_wrappers#history
drafts_portal_stack_wrapper GET /portals/:portal_id/stack_wrappers/:id/drafts(.:format) stack_wrappers#drafts
purge_portal_stack_wrapper GET /portals/:portal_id/stack_wrappers/:id/purge(.:format) stack_wrappers#purge
all_drafts_portal_stack_wrappers GET /portals/:portal_id/stack_wrappers/drafts(.:format) stack_wrappers#all_drafts
portal_stack_wrappers GET /portals/:portal_id/stack_wrappers(.:format) stack_wrappers#index
POST /portals/:portal_id/stack_wrappers(.:format) stack_wrappers#create
new_portal_stack_wrapper GET /portals/:portal_id/stack_wrappers/new(.:format) stack_wrappers#new
edit_portal_stack_wrapper GET /portals/:portal_id/stack_wrappers/:id/edit(.:format) stack_wrappers#edit
portal_stack_wrapper GET /portals/:portal_id/stack_wrappers/:id(.:format) stack_wrappers#show
PATCH /portals/:portal_id/stack_wrappers/:id(.:format) stack_wrappers#update
PUT /portals/:portal_id/stack_wrappers/:id(.:format) stack_wrappers#update
DELETE /portals/:portal_id/stack_wrappers/:id(.:format) stack_wrappers#destroy
history_portal_config_bundle GET /portals/:portal_id/config_bundles/:id/history(.:format) config_bundles#history
portal_config_bundles GET /portals/:portal_id/config_bundles(.:format) config_bundles#index
POST /portals/:portal_id/config_bundles(.:format) config_bundles#create
new_portal_config_bundle GET /portals/:portal_id/config_bundles/new(.:format) config_bundles#new
edit_portal_config_bundle GET /portals/:portal_id/config_bundles/:id/edit(.:format) config_bundles#edit
portal_config_bundle GET /portals/:portal_id/config_bundles/:id(.:format)
But I am getting a routing error and don't know where to go from here...
No route matches [GET] "/portals/asdg/delete"
Can anyone share a guide or point me to documentation that helps me understand what's wrong here?
Try
= link_to 'Delete', portal_path(portal), method: :delete
Here are the all 7 routes for portal
portals GET /portals(.:format) portals#index
POST /portals(.:format) portals#create
new_portal GET /portals/new(.:format) portals#new
edit_portal GET /portals/:id/edit(.:format) portals#edit
portal GET /portals/:id(.:format) portals#show
PATCH /portals/:id(.:format) portals#update
PUT /portals/:id(.:format) portals#update
DELETE /portals/:id(.:format) portals#destroy
And in your routes you have delete_portal path will exist because of
member do
post :delete
end
If you want to call this then you have do define method for this action in your controller and call this path with method: :post
But in RESTful routes delete action have always DELETE method, see in above routes.
There are two issues in your link_to tag.
You used delete_portal_path(portal) but if you run rake routes on your console you will see no such route exist. it have portal_path(portal).
You need to specify method type in route in case of Show(GET default), update(PUT) and delete(DELETE). Because all share same path portal_path(portal).
So your final route should be:
= link_to 'Delete', portal_path(portal), method: :delete
More detail here
You need to define http method when you use delete
= link_to 'Delete', portal_path(portal), method: :delete
It will work.

How to delete / destroy user in rails app from database?

I recently watched the railscast episode #250 Authentication from Scratch (revised) and I have the signup / login / logout actions working. However I am working on creating an action to delete a user from the database, but I am currently experiencing some errors when I try to delete a user.
The users_controller.rb delete / destroy actions look like the following,
def delete
# the below line calls the destroy method / action
self.destroy
end
def destroy
session[:user_id] = nil
# #user = User.find(params[:id])
#user.destroy
# User.find(parmas[:id]).destroy
# the below line didn't delete the current user :(
# #user = User.destroy
redirect_to :controller=>'users', :action => 'new'
end
The error message I'm getting in the browser when I try to delete a user looks like the following.
The page that contains the delete link looks like the following, index.html.erb
<h1>Welcome
<% if current_user %>
<%= current_user.email %>
<% end %>
</h1>
<p>You have <%= current_user.credit %> credits.</p>
<!-- http://stackoverflow.com/questions/5607155/ -->
<%= link_to('Delete your account', :controller => 'users', :action => 'destroy') %>
routes.rb
Rails.application.routes.draw do
# the below generated route is not necessary
# get 'sessions/new'
# delete user route
#get 'delete' => 'users#delete'
# shortened routes, per railscast comment
get 'signup' => 'users#new'
get 'login' => 'sessions#new'
get 'logout' => 'sessions#destroy'
# get 'signup', to: 'users#new', :as 'signup'
# get 'login', to: 'sessions#new', :as 'login'
# get 'logout', to: 'sessions#destroy', :as 'logout'
resources :users
resources :sessions
root to: 'users#new'
# get 'users/new'
# the below line specifies JSON as the default API format
namespace :api, defaults: {format: 'json'} do
namespace :v1 do
resources :users
end
end
It stands to reason you're getting a NoMethodError, since you've never set the #user variable, that line is commented out:
def destroy
session[:user_id] = nil
# #user = User.find(params[:id]) <-- Commenting out this line was your problem
#user.destroy
Changing to
def destroy
session[:user_id] = nil
#user = User.find(params[:id])
#user.destroy
You should be good to go.
EDIT: one thing you'd probably want to do is change from using the old style of link_to, specifying the controller and action, and change to the new style, using route helpers. In this case, you'd use, i believe, link_to 'Delete your account', current_user, :method => :delete, but you can check by running rake routes, where it will list the helpers available based on your routes.rb file.
Well, I think you should make things a bit simpler and start from the dummiest thing, that works. First of all, if you use your controller as a resource, there would not be a delete action there, only destroy.
def destroy
User.find(params[:id]).destroy
session[:user_id] = nil
redirect_to new_user_path
end
P.S. once again, I assume that you have set resources :users in your routes.rb.
If you have a bunch of get|post|put|delete routes instead, just make sure you point the redirect correctly.

ruby on rails button_to not activating the delete method

Hi im following the agile web development ebook and i cant seem to activate the logout action
here are the revelant parts (TAB key not working could not format to code)
rake routes
logout DELETE /logout(.:format) sessions#destroy
from the route file
controller :sessions do
get 'login' => :new
post 'login' => :create
delete 'logout' => :destroy
end
my controller
def destroy
session[:user_id] = user.id
redirect_to store_url , notice: "Logged out"
end
and my view (relevant part)
<%= button_to 'Logout', logout_path, method: :delete %>
the error message is
No route matches [GET] "/logout"
i know it should use delete method but nothing i do seems to help
You may need to add a match in your routes. Sorry that I don't have the book with me to refer to.
Put this above your controller :sessions ...
match 'logout' => 'sessions#destroy', :as => :logout
If you didn't put the above line, your logout path should be sessions_logout_path, not logout_path.
Reference:
http://guides.rubyonrails.org/routing.html#naming-routes
match '/logout' => 'sessions#destroy', :via => :delete
or
controller :sessions do
member do
delete :destroy, :as => :logout
end
end

Rails routing error: receiving Unknown Action exception

In my routes.rb file, I have the following:
controller :sessions do
get 'login' => :new
post 'login' => :create
delete 'logout' => :destory
end
And in a view file, I have this link:
<%= link_to 'Logout', logout_url, :method => :delete%>
But when click the Logout link, I get this error
Unknown action
The action 'destory' could not be found for SessionsController
Your route is fine, the problem is that your SessionsController has no destroy action defined
Actually, I just noticed, your route has destory instead of destroy

redirect_to and :controller, :actions getting mixed up in Rails

In a Rails controller I'm calling this:
redirect_to :controller => "user", :action => "login"
My user_controller.rb defines the following method:
class UserController < ApplicationController
def login
###
end
When I call my redirect_to method I get this error in my browser: No action responded to show. Checking the server logs I see this line:
Processing UserController#show (for 127.0.0.1 at 2009-12-19 12:11:53) [GET]
Parameters: {"action"=>"show", "id"=>"login", "controller"=>"user"}
Finally, I've got this line in my routes.rb
map.resources :user
Any idea what I'm doing wrong?
The hack-y answer is to change the following line in your routes.rb
map.resources :user
to
map.resources :user, :collection => { :login => :get }
But that's bad for a variety of reasons -- mainly because it breaks the RESTful paradigm. Instead, you should use a SessionController and use sessions/new to login. You can alias it to /login by the following line in routes.rb
map.login 'login', :controller=>"sessions", :action=>"new"
Tried
redirect_to :url => {:action => 'login', :controller => 'user'}
Or you might have to remove the routing or write your own. The routing maps POST to create, PUT to update etc.
You need to add the login action in your routes.rb. Refer to this excellent guide.
What I ended up doing was creating a new login_controller to handle logins.
Here's the relavent bit from my routes.rb file:
map.login '/login', :controller => 'login', :action => 'login'
Thanks for your help...

Resources