gpt4 book ai didi

ruby-on-rails - Ruby on Rails - 设计用户/sign_out 不工作

转载 作者:数据小太阳 更新时间:2023-10-29 06:30:29 27 4
gpt4 key购买 nike

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

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

它产生路由错误

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

我该如何解决这个问题?

rake 路

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"}

路线.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 输出中的相关行:

destroy_user_session DELETE /users/sign_out(.:format)

意思是,如果你想注销用户,你需要向那个 url 发送一个 DELETE 请求。在 Rails 中,您可以生成这样的链接:

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。请注意,浏览器并不真正支持 DELETE 请求,rails 实际上是在 POST 数据,但它会发送一个模拟 DELETE 方法的特殊参数。

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

关于ruby-on-rails - Ruby on Rails - 设计用户/sign_out 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6965876/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com