Ошибка активного имени администратора: ресурс uninitializedconstant:Users

Итак, у меня есть несколько моделей в моем приложении, и все они зарегистрированы в ActiveAdmin. Все они отлично работают, кроме одного, и я не могу понять, почему. Я продолжаю получать ту же ошибку:

NameError в /admin/reports неинициализированная константа Report::Users

Модель, по которой это происходит, называется Report

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

Контроллер выглядит так:

Class ReportsController < ApplicationController
  before_action :authenticate_user!


  def create
    @car=Car.find(params[:car_id])
    @report=@car.reports.build(report_params)
    @report.user_id=current_user.id
    @report.car_id=@car.id
    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

Это миграция, используемая для создания модели:

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

Наконец, вот приложение active_admin /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

Я пытался понять это в течение нескольких часов. Решения, которые я видел на SO, которые не работают. Я побежал rails generate active_admin:resource Report создать его так, чтобы он был единичным. Почему это плохо?

1 ответ

Решение

NameError в /admin/reports неинициализированная константа Report::Users

Название ассоциации для belongs_to должен быть единственным в соответствии с соглашениями об именах.

class Report < ActiveRecord::Base
belongs_to :user #here
belongs_to :car #and here too
enum reason: [:accident,:totaled,:stolen]
validates :reason, presence:true
end
Другие вопросы по тегам