所以我的应用程序中有几个模型,它们都在ActiveAdmin中注册。他们都很好,除了一个,我无法弄清楚为什么。我一直得到同样的错误:
/ admin / reports上的NameError 未初始化的常量报告::用户
正在发生的模型称为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
来创建它,所以它是单数的。为什么会行为不端?
答案 0 :(得分:0)
根据命名约定,/ admin /上的NameError报告未初始化的常量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