【问题标题】:Active Admin Name Error uninitializedconstant Resource::Users活动管理员名称错误未初始化常量资源::用户
【发布时间】:2015-07-31 03:45:25
【问题描述】:

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

/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 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 来创建它,所以它是单数的。为什么它行为不端?

【问题讨论】:

    标签: ruby-on-rails ruby activeadmin nameerror uninitialized-constant


    【解决方案1】:

    NameError at /admin/reports uninitialized constant 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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-05-13
      • 1970-01-01
      • 1970-01-01
      • 2012-12-11
      • 2015-05-03
      • 2016-07-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多