Ruby on Rails - 设计用户/sign_out 不起作用

发布于 2024-11-28 03:01:25 字数 3306 浏览 0 评论 0原文

我正在使用设备 sign_insign_up 网址正在工作

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

,它会生成路由错误

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

如何解决此问题?

耙路线

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

I'm using devise
sign_in and sign_up urls are working

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

it generates routing error

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

How can I fix this?

rake routes

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

走野 2024-12-05 03:01:25

错误原因是使用HTTP GET方法无法访问该路由。请注意 rake paths 输出中相关行的外观:

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 请求,rails 实际上是 POST 数据,但它发送一个模拟 DELETE 方法的特殊参数。

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

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)

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

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.

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

霞映澄塘 2024-12-05 03:01:25

上述任何建议对我来说都不起作用......

这篇文章对于寻找其他替代方案可能更有用。
没有路由匹配“/users/sign_out”设计rails 3

我必须在 application.js 文件中添加以下行

//= require_tree .
// Add following lines.
//= require jquery 
//= require jquery_ujs

Any of the above suggestions did not work for me....

This post can be more useful to find other alternatives.
No route matches "/users/sign_out" devise rails 3

I had to add following lines in the application.js file

//= require_tree .
// Add following lines.
//= require jquery 
//= require jquery_ujs
酷遇一生 2024-12-05 03:01:25

使用button_to代替link_to,并制作data:{turbo: "false"}

<%= button_to "Sign_out", destroy_user_session_path, :method => 'delete', data:{turbo: "false"} %>

use button_to instead of link_to, and make data:{turbo: "false"}

<%= button_to "Sign_out", destroy_user_session_path, :method => 'delete', data:{turbo: "false"} %>
累赘 2024-12-05 03:01:25

使用button_to而不是link_to。问题是,无论您在 link_to 中定义什么 HTTP 方法,它总是会发送 GET 请求。但是,正如您所知,设计注销路由有一个 DELETE 方法来注销用户。

<%= button_to "Sign Out", method: :delete %>

Use button_to instead of link_to. The problem is that no matter what HTTP method you define inside link_to, it will always send a GET request. But, as you know devise signout route has a DELETE method to sign out a user.

<%= button_to "Sign Out", method: :delete %>
毁梦 2024-12-05 03:01:25

我有完全相同的症状,但我还使用 jquery-turbolinks 和 masonry-rails 来“很好地”在站点转换和位置中显示图像。

我发现这个“破坏”了转换:

//= require jquery_ujs

所以我删除了它,转换就像一个魅力......但是当我注销时,我得到了上述错误,即使我的耙子路线显示了路径的存在。

对我来说,“修复”结果是添加 //= requirerails-ujs 并稍微重新安排我的树,所以我最终得到:

//= require rails-ujs
//= require jquery
//= require jquery.turbolinks
//= require twitter/bootstrap
//= require masonry/jquery.masonry
//= require masonry/jquery.imagesloaded.min
//= require masonry/modernizr-transitions
//= require turbolinks
//= require_tree .

顺便说一句,任何在砖石工作时发现此错误的人也应该添加“启用过渡的无限滚动clearfix”到div,并带有他们想要操作的资源的id...在我的例子中是“bookads”

<div class="transitions-enabled" id="bookads">
  <% @books.each do |book| %>
    <div class="box panel panel-default">
      <div class="panel-heading index">
        <h3 class="panel-title text-center">
          <strong><%= book.title %></strong>
        </h3>
      </div>
     <div class="panel-body">... etc

称为“box”

请注意,我使用的各个元素的id在您的coffescript中 :

$ ->
  $('#bookads').imagesLoaded ->
    $('#bookads').masonry
      itemSelector: '.box'
      isFitWidth: true
      isAnimated: true

并得到“平滑”动画我还添加了 masonry/jquery.imagesloaded.min 和 masonry/modernizr-transitions,如上面的树所示。

希望这个答案能为某人节省我寻找解决方案所花费的几个小时。

I had exactly the same symptoms, but I was also using jquery-turbolinks with masonry-rails to have images in the site transition and position "nicely".

I found that having this "broke" the transitions:

//= require jquery_ujs

so I removed it, and transitions worked like a charm... but when I went to log out, I got the above error even though my rake routes showed the existence of the path.

The "fix" for me turned out to be adding //= require rails-ujs and re-arranging my tree somewhat, so I ended up with:

//= require rails-ujs
//= require jquery
//= require jquery.turbolinks
//= require twitter/bootstrap
//= require masonry/jquery.masonry
//= require masonry/jquery.imagesloaded.min
//= require masonry/modernizr-transitions
//= require turbolinks
//= require_tree .

As an aside, anyone that has found this error while working in masonry should also add "transitions-enabled infinite-scroll clearfix" to the div with the id of the resource of what they want to operate on... in my case "bookads"

<div class="transitions-enabled" id="bookads">
  <% @books.each do |book| %>
    <div class="box panel panel-default">
      <div class="panel-heading index">
        <h3 class="panel-title text-center">
          <strong><%= book.title %></strong>
        </h3>
      </div>
     <div class="panel-body">... etc

Note the id of the individual elements I used is called "box"

In your coffeescript:

$ ->
  $('#bookads').imagesLoaded ->
    $('#bookads').masonry
      itemSelector: '.box'
      isFitWidth: true
      isAnimated: true

and to get "smooth" animation I also added masonry/jquery.imagesloaded.min and masonry/modernizr-transitions as shown in the tree above.

Hopefully this answer will save someone the few hours I spent looking for a solution.

不一样的天空 2024-12-05 03:01:25

将其放入 app/views/layouts/application.html.erb

<%= stylesheet_link_tag    'application', media: 'all', 'data-turbolinks-track': 'reload' %>
<%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %>

我希望它对你有帮助

Put this into app/views/layouts/application.html.erb

<%= stylesheet_link_tag    'application', media: 'all', 'data-turbolinks-track': 'reload' %>
<%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %>

I hope it helps you

站稳脚跟 2024-12-05 03:01:25

在routes.rb文件中

devise_scope :user do get '/users/sign_out' =>; 'devise/sessions#destroy' 结束
并在注销链接中添加以下代码。

<%= link_to "注销", destroy_user_session_path, :method => :删除%>

In routes.rb file

devise_scope :user do get '/users/sign_out' => 'devise/sessions#destroy' end
and in the sign out link add below code.

<%= link_to "Sign out", destroy_user_session_path, :method => :delete%>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文