1

我在使用 rails+mongoid 时遇到了一些问题,以节省操作创建

我写了关系:

class SchoolClass
  include Mongoid::Document
  include Mongoid::MultiParameterAttributes

  has_one :teachers

  field :nome, type: String

  validates_presence_of :nome, :message => 'Nao pode ser vazio'
end

class Teacher
  include Mongoid::Document
  include Mongoid::MultiParameterAttributes
  belongs_to :school_class

  field :nome
  field :nascimento, :type => Date
  field :email
  field :senha
end

我正常创建了学校课程。

显示学校课程:

    <%= f.input :school_class, :collection => SchoolClass.all, label_method: "nome", include_blank: false %><br />

教师控制器是:

  def create
    @teacher = Teacher.new(params[:teacher])

    respond_to do |format|
      if @teacher.save
        format.html {redirect_to(teachers_path, :notice => 'Professor cadastrado com sucesso')}
        format.json { head :no_content }
      else
        format.html {render :action => :new}
        format.json { render json: @teacher.errors, status: :unprocessable_entity }
      end
    end
  end

我不知道如何保存参考。浏览器给我这个:

uninitialized constant SchoolClas

任何人都可以帮助我吗?

4

3 回答 3

0

在 SchoolClass 中,您应该拥有:

has_one :teacher

此外,如果您的错误消息是正确的,看起来您可能在某处将 SchoolClass 错误地输入为 SchoolClas(缺少最后的 's')。

于 2013-05-20T18:46:30.577 回答
0

Mongoid的文档指出“模型类名称不能以“s”结尾,因为它将被视为单词的复数形式”,并建议添加自定义变形器。

我有一个类似的情况,一个类XyzMass被截断为XyzMas. Mongo 正在将其转换为相应的表名xyz_mass- 并调用String#singularize它。

尝试将以下内容放入config/initializers/inflections.rb

ActiveSupport::Inflector.inflections do |inflect|
  inflect.singular /(school_class)$/i, '\1'
end

您可以在 Rails 控制台中使用'school_class'.singularize.

于 2013-07-16T21:16:44.500 回答
0

将您的表单代码修改为:

<%= f.input :school_class_id, :collection => SchoolClass.all, label_method: "nome", include_blank: false %><br />
于 2013-05-21T06:32:35.920 回答