Search code examples
ruby-on-railsrubyactiveadminnameerroruninitialized-constant

Active Admin Name Error uninitializedconstant Resource::Users


So I have a couple of models in my app and they are all registered with ActiveAdmin. They all work great except for one and I can't figure out why. I keep getting the same error:

NameError at /admin/reports uninitialized constant Report::Users

The model that it is happening on is called Report

    class Report < ActiveRecord::Base
      belongs_to :users
      belongs_to :cars
      enum reason: [:accident,:totaled,:stolen]
      validates :reason, presence:true
    end

The controller looks like this:

Class ReportsController < ApplicationController
  before_action :authenticate_user!


  def create
    @car=Car.find(params[:car_id])
    @[email protected](report_params)
    @report.user_id=current_user.id
    @[email protected]
    if @report.save
      redirect_to car_path(car)
    else
      render 'new'
    end
  end

  def destroy
    @report=Report.find(params[:id])
    @report.destroy
  end

  private
  def report_params
    params.require(:report).permit(:reason)
  end
end

This is the migration used to create the model:

class CreateReports < ActiveRecord::Migration
  def change
    create_table :reports do |t|
      t.references :user, index: true
      t.references :car, index: true
      t.integer :reason, default: 0

      t.timestamps null: false
    end
    add_foreign_key :reports, :users
    add_foreign_key :reports, :cars
  end
end

Lastly here is the active_admin app/admin/report.rb:

ActiveAdmin.register Report do

# See permitted parameters documentation:
# https://github.com/activeadmin/activeadmin/blob/master/docs/2-resource-customization.md#setting-up-strong-parameters
#
# permit_params :list, :of, :attributes, :on, :model
#
# or
#
# permit_params do
#   permitted = [:permitted, :attributes]
#   permitted << :other if resource.something?
#   permitted
# end


end

I have been trying to figure it out for a couple of hours. Solutions that I saw on SO that don't work. I ran rails generate active_admin:resource Report to create it so it is singular. Why is it misbehaving?


Solution

  • NameError at /admin/reports uninitialized constant Report::Users

    Association name for a belongs_to should be singular as per naming conventions.

    class Report < ActiveRecord::Base
    belongs_to :user #here
    belongs_to :car #and here too
    enum reason: [:accident,:totaled,:stolen]
    validates :reason, presence:true
    end