没有路线匹配[GET]“/ user / sign_out”

时间:2014-06-07 19:54:15

标签: ruby-on-rails ruby-on-rails-4 devise

因此,点击导航栏上的下拉菜单中的“退出”时,我收到以下错误:

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

但是,这仅在使用下拉导航栏中的注销(移动设备的汉堡菜单)时发生,而不是在常规导航栏上单击注销时发生。

请参阅以下代码:

  <div class="container demo-5">
    <div class="main clearfix">
      <div class="column">
        <div id="dl-menu" class="dl-menuwrapper">
          <button class="dl-trigger">Open Menu</button>
          <ul class="dl-menu dl-menu-toggle">
            <div id="closebtn"  onclick="closebtn()"></div>
            <% if user_signed_in? %>
            <li><%= link_to 'FAQ', faq_path %></li>
            <li><a href="#">Contact Us</a></li>
            <li><%= link_to 'My Account', account_path %></li>                    
            <li><%= link_to 'Sign Out',  destroy_user_session_path, method: 'delete' %></li> <--- this is the line
            <% else %>
            <li><%= link_to 'FAQ', faq_path %></li>
            <li><a href="#">Contact Us</a></li>
            <li><%= link_to 'Sign In', new_user_session_path %></li>
            <li><%= link_to 'Free Trial', plans_path %></li>
            <% end %>
          </ul>
        </div><!-- /dl-menuwrapper -->
      </div>
    </div>
  </div><!-- /container -->
</div>

这是非下拉代码:

  <div class="signincontainer pull-right">
        <div class="navbar-form navbar-right">
          <% if user_signed_in? %>
            <%= link_to 'Sign out', destroy_user_session_path, class: 'btn signin-button', method: :delete %>
              <div class="btn signin-button usernamefont"><%= link_to current_user.full_name, account_path %></div>
          <% else %>
....rest of code here

更新错误:

ActionController::RoutingError (No route matches [GET] "/user/sign_out"):
  actionpack (4.0.4) lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call'
  actionpack (4.0.4) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
  railties (4.0.4) lib/rails/rack/logger.rb:38:in `call_app'
  railties (4.0.4) lib/rails/rack/logger.rb:20:in `block in call'
  activesupport (4.0.4) lib/active_support/tagged_logging.rb:68:in `block in tagged'
  activesupport (4.0.4) lib/active_support/tagged_logging.rb:26:in `tagged'
  activesupport (4.0.4) lib/active_support/tagged_logging.rb:68:in `tagged'
  railties (4.0.4) lib/rails/rack/logger.rb:20:in `call'
  quiet_assets (1.0.2) lib/quiet_assets.rb:18:in `call_with_quiet_assets'
  actionpack (4.0.4) lib/action_dispatch/middleware/request_id.rb:21:in `call'
  rack (1.5.2) lib/rack/methodoverride.rb:21:in `call'
  rack (1.5.2) lib/rack/ru

3 个答案:

答案 0 :(得分:2)

指定method must be a symbol

在不起作用的代码中,它目前是一个字符串:

link_to 'Sign Out', destroy_user_session_path, method: 'delete' 

在正在运行的代码中,它已正确指定为符号:

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

答案 1 :(得分:1)

@colinm的回答是正确的;但让我解释一下原因:

-

HTTP动词

Devise sets a series of RESTful routes when you install it。在它生成的路由中,有destroy_user_session_path,它使用delete HTTP动词:

 destroy_user_session DELETE /users/sign_out   {controller:"devise/sessions", action:"destroy"}

这意味着当您在delete方法中使用link_to方法时,此路由可用,如下所示:

<%= link_to "Log Out", destroy_user_session_path, method: :delete %>

HTML默认为GET HTTP Verb,这意味着如果您未在代码中明确定义method: :delete,则Rails不会为verb寻找正确的{{1}}路线

答案 2 :(得分:-1)

让我们尝试一下; 删除

devise_for :users, :path => 'user'

添加此内容;

devise_for :users, :path => 'user' do
  get '/user/sign_out' => 'devise/sessions#destroy' 
end

更多; here

最后的建议

我发现它需要javascript来将请求变为DELETE;

所以

use devise_for :users, :path => 'user' in your config/routes

确保您在application.html.erb

中加载了javascript
<%= javascript_include_tag "application" %>

其次要确保jquery添加了jquery-ujs gem以及类似的内容;

//= require jquery
    //= require jquery_ujs