gpt4 book ai didi

ruby-on-rails - Activeadmin 渲染 'new' 在尝试覆盖创建 Controller 方法时给出无法找到没有 ID 的用户

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

我正在尝试在 activeadmin 中创建一个页面,用户可以在其中创建新的用户帐户。

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

当我尝试呈现 new 页面时,出现错误 Couldn't find User without an ID

为什么在尝试重新呈现 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:

最佳答案

我来晚了一点,但刚遇到这个。使用 @user 而不是 user

关于ruby-on-rails - Activeadmin 渲染 'new' 在尝试覆盖创建 Controller 方法时给出无法找到没有 ID 的用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22291245/

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