0

所以我的应用程序中有几个模型,它们都在 ActiveAdmin 中注册。除了一个,它们都工作得很好,我不知道为什么。我不断收到同样的错误:

/admin/reports 处的 NameError 未初始化常量 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 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

我一直试图弄清楚几个小时。我在 SO 上看到的不起作用的解决方案。我跑去rails generate active_admin:resource Report创造它,所以它是单一的。为什么行为不端?

4

1 回答 1

0

/admin/reports 处的 NameError 未初始化常量 Report::Users

根据命名约定,a 的关联名称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
于 2015-07-31T04:50:03.553 回答