0

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

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 ...
4

2 回答 2

2

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

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

于 2015-02-10T20:26:39.647 回答
0

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

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

于 2015-02-10T21:53:25.653 回答