rails authlogic gem - destroy action error in UserSessionsController
-
20-06-2021 - |
質問
I'm using the Authlogic gem as the authentication system for a rails app. This is my code in the UserSessionsController
class UserSessionsController < ApplicationController
layout 'auth_layout'
def new
@user_session = UserSession.new
end
def create
@user_session = UserSession.new(params[:user_session])
if @user_session.save
flash[:notice] = "Login successful!"
else
render :action => :new
end
end
def destroy
current_user_session.destroy
redirect_to new_user_session_url
end
end
Although every time I type in the address bar /user_sessions/destroy, I get the following error:
Unknown action
The action 'show' could not be found for UserSessionsController
Does anyone know how to resolve this? Thanks :)
解決
Add the following to your config/routes.rb
match "user_sessions/destroy" => "user_sessions#destroy", via: [:get]
Assuming you have your user_session
routes defined as a resource
in your config/routes.rb
, the destroy
action's route requires the HTTP method be DELETE
to /user_sessions
, making a GET
request to the (non-existent) /user_sessions/destroy
route through the address bar of your browser fail.
The addition of the above route maps the /user_sessions/destroy
path you're trying to access to load the UserSessions
controller's destroy
method instead of the (non-existent) show
method, passing "destroy"
as the :id
param.