Rails 4.1.1 ActiveAdmin未定义方法find_or_create_by_id

时间:2014-06-10 20:46:21

标签: ruby-on-rails-4 activeadmin

我发誓我绝对试图避免发布这个问题。我整天都在努力解决这个问题。

每当我尝试删除多个用户或编辑单个用户时,我都会收到以下错误:

A NoMethodError occurred in users#update:

  undefined method `find_or_create_by_id' for #<Class:0x00000006fed200>
  app/admin/users.rb:34:in `block (2 levels) in <top (required)>'

基本上我有一个自定义的users.rb文件,内容如下:

应用程序/管理/ users.rb的

ActiveAdmin.register User do

  config.batch_actions = true
  index do
    selectable_column
    actions defaults: true
    column :id
    column :name
    column :email
    column :current_sign_in_at
    column :last_sign_in_at
    column :sign_in_count
    column :current_sign_in_ip
    column :country
  end


  form do |f|
    f.inputs "User Details" do
      f.input :name
      f.input :email
      f.input :password
      f.input :password_confirmation
      f.input :superadmin, :label => "Super Administrator"
      f.input :ip_address
    end
    f.actions "User Details2" do
      f.action :submit, :as => :button
      f.action :cancel, :as => :link
    end
  end

  create_or_edit = Proc.new {
    @user            = User.find_or_create_by_id(params[:id])
    @user.superadmin = params[:user][:superadmin]
    @user.attributes = params[:user].delete_if do |k, v|
      (k == "superadmin") ||
      (["password", "password_confirmation"].include?(k) && v.empty? && !@user.$
    end
    if @user.save
      redirect_to :action => :show, :id => @user.id
    else
      render active_admin_template((@user.new_record? ? 'new' : 'edit') + '.htm$
    end
  }
  member_action :create, :method => :post, &create_or_edit
  member_action :update, :method => :put, &create_or_edit

end

我已经尝试了所有我能想到的东西......看着所有类似的线索......而我却一无所获。那么有人有任何想法吗?

2 个答案:

答案 0 :(得分:1)

使用以下内容:

User.where(id: params[:id]).first_or_create

自4.1以来,这些查找程序已被弃用,自4.1以来已被删除 http://blog.remarkablelabs.com/2012/12/what-s-new-in-active-record-rails-4-countdown-to-2013

答案 1 :(得分:1)

您正在寻找User.find_or_create_by(user_id: params[:id]) this postsource code

注意:它们尚未作为函数弃用。他们只是将形式改为this