Activeadmin渲染'new'给出在尝试覆盖create controller方法时找不到没有id的User

 徐韦志弘宇靖宏 发布于 2023-01-19 09:27

我正在尝试在activeadmin中创建一个页面,用户可以进入该页面并创建新的用户帐户.

我正在使用下面的代码覆盖我的用户模型的默认创建方法.

Couldn't find User without an ID尝试渲染new页面时收到错误.

尝试重新渲染new动作时,为什么会出现此错误?

ActiveAdmin.register User do

  permit_params do
    permitted = [:email, :encrypted_password]
    permitted << :admin if current_user.is_admin?
    permitted
  end

  # We're overriding the new and edit controller methods to properly create users with devise. Otherwise the passwords don't get encrypted
  controller do

      def create
        user = User.new
        user.name = params[:user][:name]
        user.email = params[:user][:email]
        user.admin = params[:user][:admin]
        user.password = params[:user][:encrypted_password]
        user.password_confirmation = params[:user][:encrypted_password]

        if user.save
          redirect_to admin_user_path(user)
        else
          flash.now[:error] = user.errors.full_messages
          render 'new'     # THIS CAUSES THE ERROR "Couldn't find User without an ID"
          #redirect_to new_admin_user_path # This redirect works just fine
        end
      end

    end

end

日志:

Started GET "/admin/users/new" for 127.0.0.1 at 2014-03-09 21:34:35 -0500
Processing by Admin::UsersController#new as HTML
  User Load (0.4ms)  SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 ORDER BY `users`.`id` ASC LIMIT 1
  Rendered /Users/tomcaflisch/.rvm/gems/ruby-2.1.1@myapp/bundler/gems/active_admin-739b93bf9d22/app/views/active_admin/resource/new.html.arb (31.1ms)
Completed 200 OK in 37ms (Views: 34.1ms | ActiveRecord: 0.4ms)


Started GET "/admin/users/new" for 127.0.0.1 at 2014-03-09 21:34:42 -0500
Processing by Admin::UsersController#new as HTML
  User Load (0.2ms)  SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 ORDER BY `users`.`id` ASC LIMIT 1
  Rendered /Users/tomcaflisch/.rvm/gems/ruby-2.1.1@myapp/bundler/gems/active_admin-739b93bf9d22/app/views/active_admin/resource/new.html.arb (32.7ms)
Completed 200 OK in 60ms (Views: 35.4ms | ActiveRecord: 4.3ms)


Started POST "/admin/users" for 127.0.0.1 at 2014-03-09 21:34:44 -0500
Processing by Admin::UsersController#create as HTML
  Parameters: {"utf8"=>"?", "authenticity_token"=>"Aa6TBt0LADDcKAHs+gFokQroSVgTnxtlgLwzvCovIcs=", "user"=>{"name"=>"", "email"=>"", "encrypted_password"=>"[FILTERED]", "admin"=>"0"}, "commit"=>"Create User"}
  User Load (0.6ms)  SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 ORDER BY `users`.`id` ASC LIMIT 1
   (0.2ms)  BEGIN
   (0.2ms)  ROLLBACK
  Rendered /Users/tomcaflisch/.rvm/gems/ruby-2.1.1@myapp/bundler/gems/active_admin-739b93bf9d22/app/views/active_admin/resource/new.html.arb (14.6ms)
Completed 500 Internal Server Error in 25ms

ActiveRecord::RecordNotFound - Couldn't find User without an ID:

Ryan Francis.. 5

我在这里比赛有点晚了但是碰到了这个.用@user而不是user.

1 个回答
  • 我在这里比赛有点晚了但是碰到了这个.用@user而不是user.

    2023-01-19 09:29 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有