2

我遇到了奇怪的 :delete 链接方法行为问题。我配置了一个设计 sign_out 路由,该路由链接在一个下拉框中。单击该链接时,会导致路由错误(没有路由匹配 [GET] "/users/sign_out")。

奇怪的是,当我将该链接复制到导航栏中的另一个位置时,它可以完美运行。

RoR 导航栏代码为:

<div class="container nav-collapse">
<ul class="nav">
  <li class="active">
    <%= link_to t('activeview.navigation.home'), home_index_path %>
  </li>
  <li>
    <%= link_to t('activeview.navigation.sign_in'), new_user_session_path %>
  </li>
  <li>
    <%= link_to t('activeview.navigation.sign_up'), new_user_registration_path %>
  </li>
  <li><%= link_to t('activeview.navigation.sign_out'), destroy_user_session_path, :method => :delete %></li>
  <li class="dropdown">
    <% if user_signed_in? %>
      <%= link_to (current_user.email + ' <span class="caret"></span>').html_safe, '#', {
        :class => 'dropdown-toggle',
        'data-toggle' => 'dropdown' } %>
      <ul class="dropdown-menu">
        <li><%= link_to t('activeview.navigation.settings'), edit_user_registration_path(current_user) %></li>
        <li class="divider"></li>
        <li><%= link_to t('activeview.navigation.sign_out'), destroy_user_session_path, :method => :delete %></li>
    <% else %>
      <%= link_to (t('activeview.navigation.not_connected') + ' <span class="caret"></span>').html_safe, '#', {
        :class => 'dropdown-toggle',
        'data-toggle' => 'dropdown' } %>
    <% end %>
    </ul>
  </li>
</ul>
</div><!--/.nav-collapse -->

我的 application.js 如下所示:

//= require jquery
//= require jquery_ujs
//= require twitter/bootstrap
//= require_tree .

下面是我的导航栏。案例是 sign_out_1 完美运行,但 sign_out_2 使用 GET 方法而不是 DELETE。

<div class="navbar navbar-fixed-top">
  <div class="navbar-inner">
    <div class="container">
    <a class="btn btn-navbar" data-target=".nav-collapse" data-toggle="collapse">
    <span class="icon-bar"></span>
    <span class="icon-bar"></span>
    <span class="icon-bar"></span>
    </a>
    <a class="brand" href="#">Brand</a>
    <div class="container nav-collapse">
    <ul class="nav">
      <li><a href="/users/sign_out" data-method="delete" rel="nofollow">sign_out_1</a></li>
      <li class="dropdown">
          <a href="#" class="dropdown-toggle" data-toggle="dropdown">mail@hotmail.com <span class="caret"></span></a>
          <ul class="dropdown-menu">
            <li><a href="/users/edit.2">Configuración</a></li>
            <li class="divider"></li>
            <li><a href="/users/sign_out" data-method="delete" rel="nofollow">sign_out_2</a></li>
        </ul>
      </li>
    </ul>
    </div><!--/.nav-collapse -->
    </div>
  </div>
</div>

我的销毁路线是:

destroy_user_session     DELETE /users/sign_out(.:format)    devise/sessions#destroy

最后,.js 加载部分是:

<script src="/assets/jquery.js?body=1" type="text/javascript"></script>
<script src="/assets/jquery_ujs.js?body=1" type="text/javascript"></script>
<script src="/assets/twitter/bootstrap/bootstrap-transition.js?body=1" type="text/javascript"></script>
<script src="/assets/twitter/bootstrap/bootstrap-alert.js?body=1" type="text/javascript"></script>
<script src="/assets/twitter/bootstrap/bootstrap-modal.js?body=1" type="text/javascript"></script>
<script src="/assets/twitter/bootstrap/bootstrap-dropdown.js?body=1" type="text/javascript"></script>
<script src="/assets/twitter/bootstrap/bootstrap-scrollspy.js?body=1" type="text/javascript"></script>
<script src="/assets/twitter/bootstrap/bootstrap-tab.js?body=1" type="text/javascript"></script>
<script src="/assets/twitter/bootstrap/bootstrap-tooltip.js?body=1" type="text/javascript"></script>
<script src="/assets/twitter/bootstrap/bootstrap-popover.js?body=1" type="text/javascript"></script>
<script src="/assets/twitter/bootstrap/bootstrap-button.js?body=1" type="text/javascript"></script>
<script src="/assets/twitter/bootstrap/bootstrap-collapse.js?body=1" type="text/javascript"></script>
<script src="/assets/twitter/bootstrap/bootstrap-carousel.js?body=1" type="text/javascript"></script>
<script src="/assets/twitter/bootstrap/bootstrap-typeahead.js?body=1" type="text/javascript"></script>
<script src="/assets/twitter/bootstrap/bootstrap-affix.js?body=1" type="text/javascript"></script>
4

2 回答 2

2

一个快速的解决方法是在 ./config/initializers/devise.rb 中将 sign_out 更改为使用 HTTP GET 而不是 HTTP DELETE

# The default HTTP method used to sign out a resource. Default is :delete.
config.sign_out_via = :get

然后更改对 sign_out 的调用以发送 GET:

<%= link_to t('activeview.navigation.sign_out'), destroy_user_session_path, :method => :get %></li>
于 2012-08-24T11:27:41.530 回答