ruby-on-rails – 尝试在Rails 4中通过ActiveAdmin创建新用户时,
我的设置 – Rails 4,ActiveAdmin,Devise生成的用户模型.用户使用用户名进行身份验证,但没有email属性.我提到了最后一个,因为Devise严重依赖于email属性,所以这可能与问题有关.我的确切设置和代码描述为
in this blog post.
在ActiveAdmin后端,我转到用户 – >新用户 – >填写用户名,密码,密码确认 – >创建用户.新用户表单被删除而不是创建新用户,而在密码字段下我得到的错误不能为空.当我转到Rails控制台并手动创建一个新用户User.create(用户名:’Joe’,密码:’password’,password_confirmation:’password’)一切正常,用户可以登录localhost:3000 /用户/ sign_in. 我看到了这个SO question.如果我添加到我的用户模型: def password_required? new_record? ? false : super end 我可以创建一个新用户,但所有字段(包括用户名,加密密码)都是空白的. 更新正如Leger建议的那样,我发布了我的代码.当我使用Devise和Activeadmin的内置控制器时,我认为在Activeadmin和我的数据库模式中发布我的用户资源的代码是有意义的: Activeadmin中的用户资源: ActiveAdmin.register User do # This determines which attributes of the User model will be displayed in the index page. I have left only username,but feel free to uncomment the rest of the lines or add any other of the User attributes. index do column :username # column :current_sign_in_at # column :last_sign_in_at # column :sign_in_count default_actions end # Default is :email,but we need to replace this with :username filter :username # This is the form for creating a new user using the Admin backend. If you have added additional attributes to the User model,you need to include them here. form do |f| f.inputs "User Details" do f.input :username f.input :password f.input :password_confirmation end f.actions end # This is related to Rails 4 and the changes it introduced in handling strong parameters. Here we replace :email with :username. controller do def permitted_params params.permit admin_user: [:username,:password,:password_confirmation] end end end schema.rb: ctiveRecord::Schema.define(version: 20131031102826) do create_table "active_admin_comments",force: true do |t| t.string "namespace" t.text "body" t.string "resource_id",null: false t.string "resource_type",null: false t.integer "author_id" t.string "author_type" t.datetime "created_at" t.datetime "updated_at" end add_index "active_admin_comments",["author_type","author_id"],name: "index_active_admin_comments_on_author_type_and_author_id" add_index "active_admin_comments",["namespace"],name: "index_active_admin_comments_on_namespace" add_index "active_admin_comments",["resource_type","resource_id"],name: "index_active_admin_comments_on_resource_type_and_resource_id" create_table "admin_users",force: true do |t| t.string "email",default: "",null: false t.string "encrypted_password",null: false t.string "reset_password_token" t.datetime "reset_password_sent_at" t.datetime "remember_created_at" t.integer "sign_in_count",default: 0,null: false t.datetime "current_sign_in_at" t.datetime "last_sign_in_at" t.string "current_sign_in_ip" t.string "last_sign_in_ip" t.datetime "created_at" t.datetime "updated_at" end add_index "admin_users",["email"],name: "index_admin_users_on_email",unique: true add_index "admin_users",["reset_password_token"],name: "index_admin_users_on_reset_password_token",unique: true create_table "users",force: true do |t| t.string "username",null: false t.datetime "current_sign_in_at" t.datetime "last_sign_in_at" t.string "current_sign_in_ip" t.string "last_sign_in_ip" t.datetime "created_at" t.datetime "updated_at" end add_index "users",name: "index_users_on_reset_password_token",unique: true add_index "users",["username"],name: "index_users_on_username",unique: true end 任何其他代码都可用here. 解决方法
我认为问题在于处理User模型的ActiveAdmin页面中的allowed_pa??rams方法:
params.permit admin_user: [:username,:password_confirmation] 如果这实际上是用户模型,则该行应为: params.permit user: [:username,:password_confirmation] 这符合您描述的症状 – 提交后的空表单以及控制台中的所有内容都正常工作. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |