Tôi đang cố gắng để ghi đè lên phá hủy phương pháp từ lập mưu của SessionsController
, nhưng tôi đã không có thành công nào. Tôi đã thực hiện nó cho phương pháp create
, nhưng tôi không biết tại sao nó không hoạt động cho phương thức destroy
.Overriding đưa ra SessionsController phá hủy
Đây là tôi SessionsController
:
module Api
module V1
class SessionsController < Devise::SessionsController
skip_before_filter :verify_authenticity_token, if: :json_request?
def create
resource = warden.authenticate!(:scope => resource_name, :recall => "#{controller_path}#failure")
resource.update_token
sign_in_and_redirect(resource_name, resource)
end
def sign_in_and_redirect(resource_or_scope, resource=nil)
scope = Devise::Mapping.find_scope!(resource_or_scope)
resource ||= resource_or_scope
sign_in(scope, resource) unless warden.user(scope) == resource
return render :json => {:success => true}
end
# DELETE /resource/sign_out
def destroy
puts "DELETE /resource/sign_out"
return render :json => {:success => true}
end
def failure
return render :json => {:success => false, :errors => ["Login failed."]}
end
protected
def json_request?
request.format.json?
end
end
end
end
Nếu tôi sử dụng curl
yêu cầu sau, tạo ra phương pháp làm việc tốt:
curl -X POST -H "Accept: application/json" -H "Content-Type: application/json" http://localhost:3000/users/sign_in -d '{"user":{"email":"[email protected]", "password":"TopTier2011"}}'
Nhưng khi tôi sử dụng này:
curl -X DELETE -H "Accept: application/json" -H "Content-Type: application/json" http://localhost:3000/users/sign_out
Tôi nhận được <html><body>You are being <a href="http://localhost:3000/">redirected</a>.</body></html>
làm phản hồi vàCuộc gọikhông bao giờ xảy ra.
Đây là những gì tôi nhận được trong Rails STDOUT
đầu ra:
Started DELETE "https://stackoverflow.com/users/sign_out" for 127.0.0.1 at 2014-10-07 14:51:40 -0200
Processing by Api::V1::SessionsController#destroy as JSON
Parameters: {"session"=>{}}
[deprecated] I18n.enforce_available_locales will default to true in the future. If you really want to skip validation of your locale you can set I18n.enforce_available_locales = false to avoid this message.
Redirected to http://localhost:3000/
Filter chain halted as :verify_signed_out_user rendered or redirected
Completed 302 Found in 278ms (ActiveRecord: 0.0ms)
Cảm ơn bạn và xin lỗi vì tiếng Anh của tôi!
Cảm ơn bạn! Điều đó giải quyết nó – DemianArdus
Để làm rõ nhận xét của @pragma: OP thấy một thông báo nói rằng hành động 'destroy' không thực sự chạy, bởi vì một' before_filter' được gọi. Đó là một kiểm tra để xem nếu trong thực tế không có ai được đăng nhập. Để tránh bị chuyển hướng do điều kiện này, bạn phải bỏ qua 'before_filter' cụ thể này – sameers