使用nested_forms时未初始化的常量

时间:2013-12-07 22:57:49

标签: ruby-on-rails ruby-on-rails-3 undefined nested-forms

我正在使用nested_forms gem,并希望拥有如下所述的关系:http://railscasts.com/episodes/196-nested-model-form-part-1?autoplay=true

我有一个类User,它有Projects,有ProjectFiles:

class Project < ActiveRecord::Base
    belongs_to :user
    validates :user_id, presence: true

    has_many :project_files, dependent: :destroy
    accepts_nested_attributes_for :project_files, :reject_if => lambda { |a| a[:filepath].blank? }, :allow_destroy => true
end

class ProjectFile < ActiveRecord::Base
    belongs_to :project
    validates :project_id, presence: true

    mount_uploader :filepath, ProjectFileUploader
end

在我的部分地方,我保留了项目的字段,我只需要调用f.fields_for:project_files

<%= f.fields_for :project_files do |pf_form| %>

我看到很多像我一样的问题,问题总是与复数形式和/或错误的类名混淆。但是,我在关系中找不到任何错误,但我总是收到:

uninitialized constant Project::ProjectFile

1 个答案:

答案 0 :(得分:0)

好吧我放弃了,我删除了我的模型project_file。 我生成了一个名为projectfile的新文件

class Projectfile < ActiveRecord::Base
belongs_to :project
validates :project_id, presence: true

mount_uploader :filepath, ProjectFileUploader
end


class Project < ActiveRecord::Base
belongs_to :user
validates :user_id, presence: true

has_many :projectfiles, dependent: :destroy
accepts_nested_attributes_for :projectfiles, :reject_if => lambda { |a| a[:filepath].blank? }, :allow_destroy => true
end

它现在正在工作......我不知道第一个模型出了什么问题......