【问题标题】:Can't create new user with ActiveAdmin无法使用 ActiveAdmin 创建新用户
【发布时间】:2011-11-14 19:08:45
【问题描述】:

我刚刚将活动管理员添加到我的 rails 应用程序中,但无法创建新用户。我正在使用活动管理员创建的用户模型,其中包含一些添加的列,例如名字和姓氏。当我为新用户填写表单并单击创建新用户时,该页面会刷新,但不会保存我的用户,也不会转到带有成功消息的摘要页面。

这是我的 AdminUser 模型

class AdminUser < ActiveRecord::Base
  devise :database_authenticatable,
     :recoverable, :rememberable, :trackable, :validatable

  attr_accessible :email, :password, :password_confirmation, :remember_me, :first_name, :last_name

end

这是我活跃的管理类

ActiveAdmin.register AdminUser do
  index do
    column :first_name
    column :last_name
    column :email

    default_actions
  end

 form do |f|
   f.inputs "User Details" do
     f.inputs :email
     f.inputs :first_name
     f.inputs :last_name
   end
  f.buttons
 end
end

【问题讨论】:

    标签: ruby-on-rails-3 devise activeadmin


    【解决方案1】:

    我的解决方案:

    ActiveAdmin.register User do
      permit_params [:email, :password, :password_confirmation] 
    
      form do |f|
          f.inputs "User" do
            f.input :email
            f.input :password
            f.input :password_confirmation
          end
          f.actions
        end
    end
    

    【讨论】:

      【解决方案2】:

      忘记将这个小家伙添加到模型中...fml

      after_create { |admin| admin.send_reset_password_instructions }
      
      def password_required?
        new_record? ? false : super
      end
      

      【讨论】:

        【解决方案3】:

        这与 Rails 中的代码重新加载错误有关,当您的环境指定 config.cache_classes = false 时会出现该错误。

        config/environments/development.rb 中将其更改为 true,重新启动服务器,您应该能够创建用户。

        但这并不理想,suggested here 的一种解决方法是将以下内容放入您的 config/environments/development.rb

          config.to_prepare do
            Thread.current.keys.each{ |k| Thread.current[k] = nil if k.to_s =~ /_scoped_methods$/ }
          end
        

        虽然该错误似乎已解决,但我在 3.1.1 中看到了上述代码修复的问题。

        尽管这是 Rails 中的一个错误,但如果您想看到更多关于此的讨论,它也会记录为 bug in active_admin

        【讨论】:

          【解决方案4】:

          支持@Danpe 的回答。 “密码”是必填字段。因此,您需要将其添加到 permit_params 并在表单中询问密码。只有这样它才能正确保存表单。这是我的许可参数字符串,它还解决了创建此处提到的 ActiveAdmin 用户的其他问题:https://github.com/gregbell/active_admin/issues/2595

          controller do
              def permitted_params
                params.permit :utf8, :_method, :authenticity_token, :commit, :id,
                          model: [:attribute1, :attribute2, etc]
              end
          end
          

          【讨论】:

          • Naren 我遵循了这个解决方案,它解决了未经许可的问题,但我不知道为什么它不执行更新操作。我尝试更新记录,但在补丁操作后,它回滚。你知道为什么吗? stackoverflow.com/questions/24971627/…
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-11-07
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2022-12-14
          相关资源
          最近更新 更多