You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Ruby on Rails Devise路由异常:访问/users/:id时提示sign_out用户不存在

Hey there, let's work through this Devise routing issue you're facing—super frustrating when similar solutions don't fit your code, right?

The Root Cause

That error no user with id='sign_out' tells us exactly what's going wrong: when you try to sign out, your request is getting routed to your users#show action instead of Devise's sign-out handler. Rails is treating the string sign_out like a user ID parameter, hence the missing user error.

Common Fixes to Try

1. Reorder Your Routes (Most Likely Fix)

Rails matches routes from top to bottom. If your custom /users/:id route comes before your Devise configuration, Rails will grab any /users/anything request first—including /users/sign_out.

Adjust your routes file to put Devise before any user-related routes:

Rails.application.routes.draw do
  # Devise first!
  devise_for :users, path: 'devise'

  # Now your custom user routes
  resources :users, only: [:show] # Or your custom get '/users/:id', to: 'users#show'

  get 'sessions/new'
  get 'intro', to: 'index#intro'
  root 'index#intro'

  authenticated :user do
    root 'index#homepage', as: :authenticated_root
  end
end

2. Double-Check Your Sign-Out Link

Since you set path: 'devise' for Devise, your sign-out path should be /devise/sign_out, not /users/sign_out. Make sure your view code uses the correct Devise helper:

<%= link_to "Sign Out", destroy_user_session_path, method: :delete, data: { turbo_method: :delete } %>
  • The method: :delete ensures it uses the correct HTTP verb (Devise defaults to DELETE for sign-out).
  • If you're using Turbo, add data: { turbo_method: :delete } to prevent it from falling back to a GET request.

3. Verify Devise's Sign-Out Configuration

If you've changed Devise to allow GET requests for sign-out (not recommended for security), this can amplify routing conflicts. Check your config/initializers/devise.rb for:

config.sign_out_via = :get

If this exists, make sure your Devise routes still come before custom user routes. Better yet, switch back to DELETE to avoid CSRF risks and routing headaches.

4. Confirm Route Order with rails routes

Run rails routes in your terminal to see the full list of routes and their order. Look for:

  • destroy_user_session (your sign-out route)
  • Your user route (the /users/:id one)

If the user route appears above destroy_user_session, that's the problem—reorder them as suggested in step 1.

内容的提问来源于stack exchange,提问作者Riley Shaw

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 06:20:33