Rails Active Admin Create action fails to create my Book model but works for other models

Rails Active Admin Create action fails to create my Book model but works for other models

I'm trying to create a new book record through the active admin dashboard, but it doesn't create when pressing the create button. When I hit create there is a POST request to /admin/books and contains the forms data. But instead of returning a 302 and redirecting to the books list it returns a 200 and what looks like the update form. Even though it returns a 200 when I go to the console to check if the book was created it is not created. I manually added a new and create action to the model and that does work. The creation action works fine for other models and only affects my Book model.

Here's the code in my admin/books.rb file

ActiveAdmin.register Book do
  permit_params :title, :page_count, :book_type, :year_released, :price, :genre_id, :language_id, :author_id

  # Displays the drop down menus correctly as well as all other inputs in order of my choosing
  form do |show|
    show.inputs "Details" do
      show.input :language_id, as: :select, collection: Language.all.collect { |language|
      [ language.language_short, language.id ] }
      show.input :author_id, as: :select, collection: Author.all.collect { |author| [ author.full_name, author.age,
      author.gender_short, author.biography, author.id ] }
      show.input :title
      show.input :genre_id, as: :select, collection: Genre.all.collect { |genre| [ genre.genre, genre.id ] }
      show.input :book_type
      show.input :year_released
      show.input :price
    end

    show.actions
  end
end

Here is my Book model

class Book < ApplicationRecord
  # Validates that all columns are not empty
  validates :title, :page_count, :book_type, :year_released, :price, presence: true
  # Validates that the title is unique
  validates :title, uniqueness: true
  # Associations between its parent tables and child table
  # has_and_belongs_to_many :genres
  belongs_to :language
  belongs_to :author
  has_many :purchases

  def self.ransackable_associations(auth_object = nil)
    [ "author", "genres", "language", "purchases" ]
  end

  def self.ransackable_attributes(auth_object = nil)
    [ "author_id", "book_type", "created_at", "genre_id", "id", "language_id", "page_count", "price", "title", "updated_at", "year_released" ]
  end
end

When I use the default Book form rather than my custom form the genre_id attribute does not appear, and still does the same behaviour (Fails to create the book and returns POST 200).

I'm using Ruby version 3.4.1 and Rails version 8.0.2.

And these are the server logs when creating a new Book

Started POST "/admin/books" for 127.0.0.1 at 2025-04-10 14:45:29 -0500
Processing by Admin::BooksController#create as HTML
  Parameters: {"authenticity_token" => "[FILTERED]", "book" => {"language_id" => "3", "author_id" => "6", "title" => "Captain Underpants", "genre_id" => "8", "book_type" => "Physical", "year_released" => "1997", "price" => "9.99"}, "commit" => "Create Book"}
  AdminUser Load (0.2ms)  SELECT "admin_users".* FROM "admin_users" WHERE "admin_users"."id" = 3 ORDER BY "admin_users"."id" ASC LIMIT 1 /*action='create',application='ECommerce',controller='books'*/
  TRANSACTION (0.1ms)  BEGIN immediate TRANSACTION /*action='create',application='ECommerce',controller='books'*/
  Book Exists? (2.9ms)  SELECT 1 AS one FROM "books" WHERE "books"."title" = 'Captain Underpants' LIMIT 1 /*action='create',application='ECommerce',controller='books'*/
  Language Load (0.3ms)  SELECT "languages".* FROM "languages" WHERE "languages"."id" = 3 LIMIT 1 /*action='create',application='ECommerce',controller='books'*/
  Author Load (0.1ms)  SELECT "authors".* FROM "authors" WHERE "authors"."id" = 6 LIMIT 1 /*action='create',application='ECommerce',controller='books'*/
  TRANSACTION (0.1ms)  ROLLBACK TRANSACTION /*action='create',application='ECommerce',controller='books'*/
  Rendering /home/segern/.rbenv/versions/3.4.1/lib/ruby/gems/3.4.0/gems/activeadmin-3.3.0/app/views/active_admin/resource/new.html.arb
  Language Load (0.2ms)  SELECT "languages".* FROM "languages" /*action='create',application='ECommerce',controller='books'*/
  ↳ app/admin/books.rb:7:in 'Enumerable#collect'
  Author Load (0.7ms)  SELECT "authors".* FROM "authors" /*action='create',application='ECommerce',controller='books'*/
  ↳ app/admin/books.rb:9:in 'Enumerable#collect'
  Genre Load (0.3ms)  SELECT "genres".* FROM "genres" /*action='create',application='ECommerce',controller='books'*/
  ↳ app/admin/books.rb:12:in 'Enumerable#collect'
  Rendered /home/segern/.rbenv/versions/3.4.1/lib/ruby/gems/3.4.0/gems/activeadmin-3.3.0/app/views/active_admin/resource/new.html.arb (Duration: 57.9ms | GC: 1.0ms)
Completed 200 OK in 89ms (Views: 58.0ms | ActiveRecord: 4.5ms (7 queries, 0 cached) | GC: 1.7ms)


Started GET "/favicon.ico" for 127.0.0.1 at 2025-04-10 14:45:29 -0500

ActionController::RoutingError (No route matches [GET] "/favicon.ico"):

Answer

You require a page_count but there is no form input for it. With no page_count there will always be a validation error.

  validates :title, :page_count, :book_type, :year_released, :price, presence: true
                    ^^^^^^^^^^^

This error should show up Read more. You can make validation errors show up in your logs, but it's better to display validation errors in your form, for the user's benefit. Add this line.

  show.semantic_errors *show.object.errors.attribute_names

See ActiveAdmin: Displaying Errors and Formtastic: Semantic Errors.

Enjoyed this article?

Check out more content on our blog or follow us on social media.

Browse more articles