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

Rails后台用户编辑表单报错:Couldn't find User without an ID

问题描述

开发后台管理面板的用户编辑功能时遇到错误,管理员需要能编辑用户角色及删除用户,创建表单后出现如下错误:

ActiveRecord::RecordNotFound (Couldn't find User without an ID):

相关代码

routes.rb

Rails.application.routes.draw do
  namespace :admin do
    root to: 'users#index'
    resources :users
  end
  get 'home', to: 'home#index'
  
  devise_for :users, controllers: {
    sessions: 'users/sessions'
  }

  devise_scope :user do
    root to: 'users/sessions#new'
    get 'sign_in', to: 'users/sessions#new'
    get '/users/sign_out', to: 'users/sessions#destroy'
    get 'search_users', to: 'users/sessions#search_users'
  end
end

app/controllers/admin/users_controller.rb

class Admin::UsersController < Admin::BaseController
  def index
    #@users = User.all
    @users = User.find(params[:id])
  end
end

app/views/admin/users/index.html.erb

<div class="container-fluid admin-container">
  <div class="admin-h2-container">
    <h2 class="admin-h2">Zarządzanie użytkownikami</h2>
  </div>
  <div class="admin-table">
    <table class="table table-striped" style="width: 80%;">
      <thead>
        <tr>
          <th class="admin-table">No</th>
          <th class="admin-table">Username</th>
          <th  class="admin-table">Imię</th>
          <th class="admin-table">Nazwisko</th>
          <th class="admin-table">Uprawnienia</th>
        </tr>
      </thead>
      <body>
        <% @users.each do |user| %>
          <tr>
            <td>
              <%= simple_form_for [:admin, @users] do |f| %>
                <div class="form-inputs">
                  <%= f.input :id, input_html: {readonly: true } %>
                  <%= f.input :username, input_html: {readonly: true } %>
                  <%= f.input :first_name, input_html: {readonly: true } %>
                  <%= f.input :last_name, input_html: {readonly: true } %>
                  <%= f.input :role, input_html: {readonly: true } %>
                </div>
              <% end %>
            </td>
          </tr>
        <% end %>
      </body>
    </table>
  </div>
</div>

问题根源与修复方案

1. 控制器核心错误修复

index动作是展示用户列表的,你错误用了User.find(params[:id])——这个方法需要请求参数里的id来查询单个用户,但访问/admin/users(index路由)不会传入id,直接触发RecordNotFound错误。同时需要补充编辑、更新、删除的动作逻辑:

class Admin::UsersController < Admin::BaseController
  def index
    @users = User.all # 恢复为获取所有用户
  end

  # 处理单个用户的编辑请求
  def edit
    @user = User.find(params[:id])
  end

  # 保存用户角色修改
  def update
    @user = User.find(params[:id])
    if @user.update(user_params)
      redirect_to admin_users_path, notice: '用户角色已更新'
    else
      render :edit
    end
  end

  # 删除用户
  def destroy
    @user = User.find(params[:id])
    @user.destroy
    redirect_to admin_users_path, notice: '用户已删除'
  end

  private

  def user_params
    params.require(:user).permit(:role) # 仅允许修改role字段,可按需调整
  end
end

2. 视图错误修正

  • 循环中表单错误传入集合@users,应改为单个user对象
  • 表格主体标签应为<tbody>而非<body>,修正HTML语法错误
  • 新增编辑、删除操作按钮:
<div class="container-fluid admin-container">
  <div class="admin-h2-container">
    <h2 class="admin-h2">Zarządzanie użytkownikami</h2>
  </div>
  <div class="admin-table">
    <table class="table table-striped" style="width: 80%;">
      <thead>
        <tr>
          <th class="admin-table">No</th>
          <th class="admin-table">Username</th>
          <th class="admin-table">Imię</th>
          <th class="admin-table">Nazwisko</th>
          <th class="admin-table">Uprawnienia</th>
          <th class="admin-table">操作</th>
        </tr>
      </thead>
      <tbody> <!-- 修正为tbody标签 -->
        <% @users.each_with_index do |user, index| %>
          <tr>
            <td><%= index + 1 %></td>
            <td><%= user.username %></td>
            <td><%= user.first_name %></td>
            <td><%= user.last_name %></td>
            <td><%= user.role %></td>
            <td>
              <%= link_to '编辑', edit_admin_user_path(user), class: 'btn btn-sm btn-primary' %>
              <%= link_to '删除', admin_user_path(user), method: :delete, data: { confirm: '确定要删除该用户吗?' }, class: 'btn btn-sm btn-danger' %>
            </td>
          </tr>
        <% end %>
      </tbody>
    </table>
  </div>
</div>

3. 新增编辑页面

创建app/views/admin/users/edit.html.erb,提供角色编辑表单:

<div class="container-fluid admin-container">
  <h2 class="admin-h2">编辑用户角色</h2>
  <%= simple_form_for [:admin, @user] do |f| %>
    <% if @user.errors.any? %>
      <div class="alert alert-danger">
        <% @user.errors.full_messages.each do |msg| %>
          <p><%= msg %></p>
        <% end %>
      </div>
    <% end %>
    <div class="form-inputs">
      <%= f.input :username, input_html: {readonly: true } %>
      <%= f.input :first_name, input_html: {readonly: true } %>
      <%= f.input :last_name, input_html: {readonly: true } %>
      <%= f.input :role, collection: User.roles.keys, prompt: '选择角色' %> <!-- 若role是枚举类型,可直接用此方式,否则按需调整选项 -->
    </div>
    <div class="form-actions">
      <%= f.button :submit, '保存修改', class: 'btn btn-primary' %>
      <%= link_to '返回列表', admin_users_path, class: 'btn btn-secondary' %>
    </div>
  <% end %>
</div>

4. 路由说明

你的路由配置是正确的,namespace :admin do resources :users已自动生成index、edit、update、destroy等RESTful路由,可运行rails routes命令查看所有路由,确认edit_admin_user_path等辅助方法是否存在。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.25 07:20:25