Rails奇怪:使用twitter-bootstrap-rails删除行为

时间:2012-08-23 17:05:12

标签: ruby-on-rails-3 twitter-bootstrap

我遇到奇怪的问题:链接的删除方法行为。我配置了一个设计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>

2 个答案:

答案 0 :(得分: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>

答案 1 :(得分:2)