繁体   English   中英

设计 destroy_user_session_path 不起作用

[英]Devise destroy_user_session_path doesn't work

在我的 ruby​​ on rails 应用程序中,我遇到了用户使用 devise 注销的问题

我有这个导航栏:

<% if current_user %>
<nav class="top-bar" data-topbar role="navigation">
  <ul class="title-area">
    <li class="name">
      <h1><%= link_to 'Prova CMS', articles_path %></h1>
    </li>
    <li class="toggle-topbar menu-icon"><a href="#"></a></li>
  </ul>

  <section class="top-bar-section">
    <ul class="right">
      <li><%=link_to 'Gestisci Articoli', articles_path %></li>
      <li><%=link_to 'Gestisci Categorie', categories_path %></li>
      <li class="has-dropdown">
        <a href="#"><%= current_user.email %></a>
        <ul class="dropdown">
          <li><%= link_to "Logout", destroy_user_session_path, :method => :delete %></li>
        </ul>
      </li>
    </ul>

    <% if current_page?(articles_path) %>
    <ul class="left">
      <div class="large-12 columns">
        <%= form_tag articles_path, :id => "articles_search" , method: 'get' do %>
          <%= text_field_tag :search, params[:search], :placeholder => "Cerca per titolo"%>
        <% end %>
      </div>
    </ul>
    <% end %>
  </section>
</nav>

<% else %> 

<nav class="top-bar" data-topbar role="navigation">
  <ul class="title-area">
    <li class="name">
      <h1><%= link_to 'Prova CMS', root_path %></h1>
    </li>
    <li class="toggle-topbar menu-icon"><a href="#"></a></li>
  </ul>

  <section class="top-bar-section">
    <ul class="right">
      <li><%= link_to 'Login', new_user_session_path%></li>
    </ul>
  </section>
</nav>

我在站点的应用程序布局 (application.html.erb) 中呈现。 在某些页面中,注销链接不起作用,它们会在视图的 url 上生成一个 Hashtag(例如,如果我在文章路径 localhost:3000/articles 上,则链接在 /articles -> /articles# 附近添加 #)

在设计的配置/初始值设定项中,此字符串未注释:

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

在 application.js 中,我的所有 jquery 链接都可以正常工作:

    // This is a manifest file that'll be compiled into application.js, which will include all the files
// listed below.
//
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
// or any plugin's vendor/assets/javascripts directory can be referenced here using a relative path.
//
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
// compiled file.
//
// Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details
// about supported directives.
//
//= require jquery
//= require jquery_ujs
//= require foundation
//= require turbolinks
//= require_tree .

$(function(){ 
    $(document).foundation();
});

最后这些是我的路线:

Rails.application.routes.draw do
  get 'show/index'
  root to: "show#index"


  devise_for :users
  resources :categories
  resources :articles

end

我认为您的问题可能与 turbolinks 有关。 尝试禁用此页面的 turbolinks 以验证是否是这种情况。 在您的身体标签中添加:

<body data-no-turbolink="true">

我相信 turbolinks 将您的注销链接视为标准页面链接,防止实际功能触发

编辑

如果您证明是这种情况,请将禁用 turbolinks 的重点放在导致问题的特定链接上,并从正文中删除该属性。

<%= link_to "Logout", destroy_user_session_path, :method => :delete, :data => { :no_turbolink => true } %>

@Phil 的回答是正确的。 Rails 6.0.2.2 不需要我禁用 turbolink:

<%= link_to "Logout", destroy_user_session_path, :method => :delete %>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM