Ruby on Rails - 设计用户/sign_out 不起作用 [英] Ruby on Rails - devise users/sign_out not working

查看:15
本文介绍了Ruby on Rails - 设计用户/sign_out 不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用设计sign_insign_up 网址有效

I'm using devise sign_in and sign_up urls are working

但是,当我尝试 url 时:http://localhost:3000/users/sign_out

but, when I try the url: http://localhost:3000/users/sign_out

它产生路由错误

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

我该如何解决这个问题?

How can I fix this?

rake routes
        new_user_session GET    /users/sign_in(.:format)           {:action=>"new", :controller=>"devise/sessions"}
            user_session POST   /users/sign_in(.:format)           {:action=>"create", :controller=>"devise/sessions"}
    destroy_user_session DELETE /users/sign_out(.:format)          {:action=>"destroy", :controller=>"devise/sessions"}
           user_password POST   /users/password(.:format)          {:action=>"create", :controller=>"devise/passwords"}
       new_user_password GET    /users/password/new(.:format)      {:action=>"new", :controller=>"devise/passwords"}
      edit_user_password GET    /users/password/edit(.:format)     {:action=>"edit", :controller=>"devise/passwords"}
                         PUT    /users/password(.:format)          {:action=>"update", :controller=>"devise/passwords"}
cancel_user_registration GET    /users/cancel(.:format)            {:action=>"cancel", :controller=>"devise/registrations"}
       user_registration POST   /users(.:format)                   {:action=>"create", :controller=>"devise/registrations"}
   new_user_registration GET    /users/sign_up(.:format)           {:action=>"new", :controller=>"devise/registrations"}
  edit_user_registration GET    /users/edit(.:format)              {:action=>"edit", :controller=>"devise/registrations"}
                         PUT    /users(.:format)                   {:action=>"update", :controller=>"devise/registrations"}
                         DELETE /users(.:format)                   {:action=>"destroy", :controller=>"devise/registrations"}
    status_message_index GET    /status_message(.:format)          {:action=>"index", :controller=>"status_message"}
                         POST   /status_message(.:format)          {:action=>"create", :controller=>"status_message"}
      new_status_message GET    /status_message/new(.:format)      {:action=>"new", :controller=>"status_message"}
     edit_status_message GET    /status_message/:id/edit(.:format) {:action=>"edit", :controller=>"status_message"}
          status_message GET    /status_message/:id(.:format)      {:action=>"show", :controller=>"status_message"}
                         PUT    /status_message/:id(.:format)      {:action=>"update", :controller=>"status_message"}
                         DELETE /status_message/:id(.:format)      {:action=>"destroy", :controller=>"status_message"}
                    home        /home(.:format)                    {:action=>"index", :controller=>"status_message"}
                    root        /                                  {:controller=>"home", :action=>"index"}

routes.rb

Microblog::Application.routes.draw do
  devise_for :users, :controllers => {:migrations => "users/registrations"}
  resources 'status_message'
  match 'home' => 'status_message#index'
  root :to => 'home#index'
end

推荐答案

错误的原因是使用 GET HTTP 方法无法访问该路由.注意您的 rake routes 输出中的相关行:

The reason for the error is that the route is inaccessible using the GET HTTP method. Notice what the relevant line looks like in your rake routes output:

destroy_user_session DELETE /users/sign_out(.:format)

意思是,如果你想注销用户,你需要向那个 url 发送 DELETE 请求.在 rails 中,您可以生成一个链接,如下所示:

Meaning that, if you want to log the user out, you need to send a DELETE request to that url. In rails, you can generate a link that does that like so:

link_to 'Sign out', destroy_user_session_path, :method => :delete

# alternatively (although NOT recommended):

link_to 'Sign out', '/users/sign_out', :method => :delete

重要的部分是:method =>:删除.请注意,浏览器并不真正支持 DELETE 请求,rails 实际上是在 POST 数据,但它会发送一个模拟 DELETE 方法的特殊参数.

The important part is :method => :delete. Note that a DELETE request is not really supported by browsers, rails is actually POSTing the data, but it sends a special parameter that simulates the DELETE method.

这背后的原因是退出"url 会使当前用户退出,这是一种破坏性的操作.如果它可以通过浏览器自由访问,它可能会导致各种问题.GET 请求不应该改变服务器的状态.有关这方面的更多信息,这里有一篇不错的维基百科文章:http://en.wikipedia.org/wiki/REST#RESTful_web_services

The reason behind this is that the "sign out" url is one that would log the current user out, a destructive action. If it was freely accessible through the browser, it could cause various problems. GET requests should never change the state of the server. For more information on this, here's a nice wikipedia article: http://en.wikipedia.org/wiki/REST#RESTful_web_services

这篇关于Ruby on Rails - 设计用户/sign_out 不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆