英文:
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: 'exercises#index'
end
However when I'm trying to log the current user out with <%= link_to "Logout", destroy_user_session_path, method: :delete, class: "navbar-link" %>
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 < 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.
<%= link_to "Logout", destroy_user_session_path,
data: {turbo_method: :delete}
%>
# or better use a form
<%= button_to "Logout", destroy_user_session_path,
method: :delete
%>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论