【问题标题】:Uninitialized constant using nested forms in Rails在 Rails 中使用嵌套形式的未初始化常量
【发布时间】:2015-02-10 17:11:09
【问题描述】:

尝试使用 nested_form gem 创建嵌套表单时,我不断收到错误消息 uninitialized constant Page::PageAttachment。这是一些相关的代码。如果您需要任何其他代码或有任何问题,请告诉我。

Stacktrace 并没有真正告诉我任何事情,只是失败的那一行是:<%= f.fields_for :page_attachments, wrapper: false do |a| %>

# /config/routes.rb
namespace "wiki" do
  resources :spaces do
    resources :pages
  end
end

# /app/models/wiki/space.rb
module Wiki
  class Space < ActiveRecord::Base
    has_many :pages, dependent: :destroy

    validates_presence_of :name
  end
end

# /app/models/wiki/page.rb
module Wiki
  class Page < ActiveRecord::Base
    belongs_to :space
    has_many :page_attachments, dependent: :destroy

    validates_presence_of :name

    accepts_nested_attributes_for :page_attachments, :allow_destroy => true
  end
end


# /app/models/wiki/page_attachment.rb
module Wiki
  class PageAttachment < ActiveRecord::Base
    belongs_to :page
  end
end

# /app/controllers/wiki/pages_controller.rb
class Wiki::PagesController < WikiController
  def new
    @space = Wiki::Space.find(params[:space_id])
    @page = Wiki::Page.new
  end
end

# /app/views/wiki/new.html.erb
<% provide(:title, 'Create a Page') %>

<%= nested_form_for @page, url: wiki_space_pages_path(@space.id), html: { role: "form", multipart: true } do |f| %>
  <%= render "shared/error_messages", obj: @page %>
  <fieldset>
    ... a bunch of form fields ...
  </fieldset>
  <fieldset>
    <legend>Page Attachments</legend>
    <%= f.fields_for :page_attachments, wrapper: false do |a| %>
      <div class="form-group fields">
        <%= a.label :file, "File", class: "sr-only" %>
        <%= a.file_field :file, class: "form-control" %> <%= a.link_to_remove "Remove", class: "button button-danger" %>
      </div>
    <% end %>
    <p><%= f.link_to_add "+ Add Attachment", :page_attachments %></p>
  </fieldset>
  <div class="form-actions">
    <%= f.hidden_field :space_id, value: @space.id %>
    <%= f.submit "Create Page", class: "button button-primary" %>
    <%= link_to "Cancel", :back, class: "text-button" %>
  </div>
<% end %>

更新

我的文件夹结构如下:

app
    controllers
        wiki
            pages_controller.rb
    models
        wiki
            page.rb
            page_attachment.rb
    views
        wiki
            pages
                new.html.erb
                show.html.erb
                ... etc ...

【问题讨论】:

  • 很奇怪。你的文件夹结构是什么?
  • 我添加了更新以显示上述文件的文件夹结构。 wiki 文件夹中的控制器或模型是否存在问题?

标签: ruby-on-rails-4 nested-forms uninitialized-constant


【解决方案1】:

Rails 无法找到PageAttachment 模型,因此它正在寻找第二好的选项Page::PageAttachment,这显然不存在。

Rails 约定说,子文件夹中的模型应该相应地命名空间。使用您的文件夹结构,Rails 期望模型为Wiki::Page 等等。我相信这是过去的错误原因,也许它不适用于您的 Rails 版本。

使用Wiki 模块在wiki 文件夹中为您的模型和控制器命名空间,如下所示:

# /app/models/wiki/page.rb
module Wiki
  class Page < ActiveRecord::Base
  end
end

然后在整个代码中将它们与完整的类名一起使用:

class Wiki::PagesController < WikiController
  def new
    @space = Wiki::Space.find(params[:space_id])
    @page  = Wiki::Page.new
  end
end

您也可以将这些模型移动到它们各自的根文件夹中,没有命名空间,但这取决于您的应用设计。

【讨论】:

  • 好的,如果我理解正确的话,我已经对文件进行了编辑并更新了我的问题以反映它们。我现在遇到了同样的错误,但现在说uninitialized constant Wiki::Page::PageAttachment。看起来它仍然找不到 PageAtachment。我需要对传递嵌套表单的config/routes.rb 文件或url: wiki_space_pages_path(@space.id) 做任何事情吗?或者可能更新belongs_tohas_many 声明?
  • 还尝试将所有模型直接移动到models 目录中,从模型中删除module 行,并从对模型的引用中删除所有Wiki::。我仍然收到uninitialized constant Page::PageAttachment 错误。
  • 好的,您可以在下面阅读我的答案,但显然是 PageAttachment 模型内部的一行导致了错误。没有将该行添加到我上面的示例中,因为错误消息没有将我锁定在那个方向。我认为模型只是一般没有被发现 - 并不是模型内部有错误。
  • 哦。我很高兴你把它整理出来!
【解决方案2】:

好吧,rails 只是没有告诉我错误的深度。在PageAttachment 模型文件中,我有这一行mount_uploader :file, FileUploader,我使用Carrierwave 来处理文件上传。实际的上传器类名是WikiUploader

一旦我将命名修复为彼此匹配,uninitialized constant Page::PageAttachment 错误就消失了,页面加载正常。奇怪的。仍然不知道为什么它抱怨PageAttachment 真的是上传者的错。无论如何,它现在已经修复,在这个过程中我决定不给我的模型命名空间。我仍将保留命名空间的控制器,但不保留模型。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-01
    相关资源
    最近更新 更多