Ruby on Rails: 使用 Devise 进行用户注销功能无法正常工作。

huangapple go评论101阅读模式
英文:

Ruby on Rails: sign out isn't working for devise with User

问题

我有以下的路由配置:
我正在开发一个运动跟踪应用程序,我想为当前登录的用户设置tracked_exercises。这是我的routes.rb文件:

Rails.application.routes.draw do
  resources :exercises
  devise_for :users
  resources :users do
    resources :tracked_exercises
  end
  root to: 'exercises#index'
end

然而,当我尝试使用以下代码注销当前用户时<%= link_to "Logout", destroy_user_session_path, method: :delete, class: "navbar-link" %>,首先遇到一个错误,提示Uninitialized constant UsersController
然后,在创建了这个控制器之后,重定向指向users/show,并且在有了这个控制器之后,注销操作仍然无法正常工作:

class UsersController < ApplicationController
  def show
    redirect_to root_path
  end
end

我在设置Devise时是否做错了什么?

谢谢!

英文:

I have the following routes:
I'm developing an exercise tracking app and I want to have tracked_exercises for the currently logged in user. This is my routes.rb file:

Rails.application.routes.draw do
  resources :exercises
  devise_for :users
  resources :users do
    resources :tracked_exercises
  end
  root to: &#39;exercises#index&#39;
end

However when I'm trying to log the current user out with &lt;%= link_to &quot;Logout&quot;, destroy_user_session_path, method: :delete, class: &quot;navbar-link&quot; %&gt; I first run into an error that says Uninitialized constant UsersController.
Then after creating it, it turns out the redirection points the users to users/show, and after having this controller, the logout never works:

class UsersController &lt; ApplicationController
  def show
    redirect_to root_path
  end
end

Am I doing something wrong with the way I've set devise up?

Thanks!

答案1

得分: 1

method: :delete for links was a Rails UJS thing.

<%= link_to "注销", destroy_user_session_path,
  data: {turbo_method: :delete}
%>

# 或者更好地使用一个表单
<%= button_to "注销", destroy_user_session_path,
  method: :delete
%>

https://github.com/heartcombo/devise#hotwireturbo

英文:

method: :delete for links was a Rails UJS thing.

&lt;%= link_to &quot;Logout&quot;, destroy_user_session_path,
  data: {turbo_method: :delete}
%&gt;

# or better use a form
&lt;%= button_to &quot;Logout&quot;, destroy_user_session_path,
  method: :delete
%&gt;

https://github.com/heartcombo/devise#hotwireturbo

huangapple
  • 本文由 发表于 2023年8月10日 14:54:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/76873259.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定