gpt4 book ai didi

ruby-on-rails - 找不到 id=sign_out 的用户

转载 作者:数据小太阳 更新时间:2023-10-29 06:52:48 24 4
gpt4 key购买 nike

注销链接在我的 Rails 应用程序中不起作用。

我检查了下面列出的 routes.rb,我的 application.html.erb 看起来遵循正确的路径。

出现以下错误。

ActiveRecord::RecordNotFound in UsersController#show

Couldn't find User with id=sign_out
Rails.root: /Users/patrickwalsh/rails_projects/ytutorial

Application Trace | Framework Trace | Full Trace
app/controllers/users_controller.rb:4:in `show'
lib/disable_assets_logger.rb:11:in `call'

我的路线.rb

Refectory::Application.routes.draw do
devise_for :users, :controllers => { :registrations => "users" }
devise_scope :user do
get 'login', to: "devise/sessions#new", as: "login"
get 'logout', to: "devise/sessions#destroy", as: "logout"
get 'logout', to: "users/sessions#destroy", as: "logout"
get 'signup', to: "users#new", as: "signup"
match '/users/:id', :to => 'users#show', :as => :user
end
root :to => 'tutorials#index'

devise_for :users do get '/users/sign_out' => 'devise/sessions#destroy'
get 'users/:id' => 'users#show'
end
resources :tutorials
resources :comments, only: [:show, :create, :update, :destroy]

resources :tutorials do
member { post :vote }
end

if Rails.env == "development"
match 'errors/404' => 'errors#error_404'
match 'errors/500' => 'errors#error_500'
end

unless Rails.application.config.consider_all_requests_local
match '*not_found', to: 'errors#error_404'
end

match 'tagged' => 'tutorials#tagged', :as => 'tagged'
end

还有我的 application.html,从我所见看来,它似乎遵循了正确的路线。

非常感谢任何帮助!

<% if current_user.present? %>
<li><%= link_to "Log out", destroy_user_session_path, (:method => "delete") %></li>
<% else %>
<li><%= link_to "Log in", new_user_session_path %></li>
<li><%= link_to "Sign up", new_user_registration_path %></li>
<% end %>

我的用户 Controller 以及我怀疑这是问题所在但不确定错误是什么。

class UsersController < Devise::RegistrationsController
def show
@user = User.find(params[:id])
@tutorials = @user.tutorials
end
end

最佳答案

我遇到了同样的问题。我的路由顺序正确,link_to 方法被正确使用,Rails 不断触发 users/:id 路由 :id => :sign_out。我发现那是因为我从我的 application.js 文件中删除了 jquery_ujs...

jquery_ujs 处理链接中的 data-method 属性(当您使用 method 时由 link_to 生成)选项),用于确定正确的路线,如下所述:https://thoughtbot.com/blog/a-tour-of-rails-jquery-ujs

因此,只需确保您的 application.js 中包含以下内容:

//= require jquery
//= require jquery_ujs

关于ruby-on-rails - 找不到 id=sign_out 的用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22490555/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com