Search code examples
ruby-on-railsrubyruby-on-rails-6rails-adminruby-3

Creating new object in Rails Admin not working after updates


I'm rewriting a project that I had built with Ruby 2.7 and Rails 6.0 with Ruby 3 and Rails 6.1. I'm using the rails admin gem along with devise and cancancan. In my old project I've got no issues creating a simple EventCategory object. When I try now I get:

enter image description here

and:

Started POST "/admin/event_category/new" for ::1 at 2020-12-28 13:44:46 -0500
Processing by RailsAdmin::MainController#new as HTML
  Parameters: {"authenticity_token"=>"[FILTERED]", "event_category"=>{"name"=>"Run", "description"=>"", "unit_of_measurement"=>""}, "return_to"=>"http://localhost:3000/u/sign_in", "_save"=>"", "model_name"=>"event_category"}
  User Load (1.0ms)  SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2  [["id", 1], ["LIMIT", 1]]
  ↳ config/initializers/rails_admin.rb:7:in `block (2 levels) in <main>'
Completed 500 Internal Server Error in 6ms (ActiveRecord: 1.0ms | Allocations: 2754)


  
ArgumentError (wrong number of arguments (given 1, expected 0)):
  
activerecord (6.1.0) lib/active_record/suppressor.rb:43:in `save'
rails_admin (2.0.2) lib/rails_admin/adapters/active_record/abstract_object.rb:23:in `save'
rails_admin (2.0.2) lib/rails_admin/config/actions/new.rb:41:in `block (2 levels) in <class:New>'
rails_admin (2.0.2) app/controllers/rails_admin/main_controller.rb:22:in `instance_eval'
rails_admin (2.0.2) app/controllers/rails_admin/main_controller.rb:22:in `new'

This is my model in event_category.rb

class EventCategory < ApplicationRecord
  has_many :events

  rails_admin do 
    edit do
      field :name
      field :description
      field :unit_of_measurement
    end
  end
end

I would really appreciate some guidance with this one.


Solution

  • My guess is you may have been getting a deprecation warning in ruby 2.7 that now returns the error you're seeing in ruby 3.0. According the the ruby 3.0 changelist, if you want to pass a hash as an argument to a method that expects keyword arguments, you must use the double splat operator (**).

    I'm not familiar with RailsAdmin (I normally use ActiveAdmin), but if you know of a way to "intercept" the params and pass them to the save method using the ** operator, it should resolve your problem.