2015-02-10 14 views
0

尝试使用nested_form gem创建嵌套窗体时,我不断收到错误消息uninitialized constant Page::PageAttachment。这里有一些相关的代码。让我知道你是否需要任何其他代码或有任何问题。在Rails中使用嵌套窗体的未初始化的常量

堆栈跟踪是不是真的告诉我除了失败的事物都这样一条:<%= 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 ... 
+0

奇怪。你的文件夹结构是什么? – dgilperez 2015-02-10 19:24:08

+0

我添加了一个更新来显示上述文件的文件夹结构。 “维基”文件夹中的控制器或模型有问题吗? – ryanpitts1 2015-02-10 19:50:01

回答

0

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

只要我修复命名以匹配对方,uninitialized constant Page::PageAttachment错误消失,页面加载正常。奇怪的。仍然不确定它为什么在抱怨PageAttachment这是真正的上传者的错。无论如何,现在它已经被修复了,并且在这个过程中,我决定不用命名我的模型。控制器我仍然保留命名空间,但不是模型。

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 

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

+0

好吧,如果我正确地理解了你,我已对这些文件进行了编辑并更新了我的问题以反映它们。我现在得到了同样的错误,但现在说'未初始化的常量Wiki :: Page :: PageAttachment'。看起来它仍然无法找到PageAtachment。我是否需要使用'config/routes.rb'文件或'url:wiki_space_pages_path(@ space.id)'来传递嵌套表单?或者可能更新'belongs_to'和'has_many'声明? – ryanpitts1 2015-02-10 20:50:15

+0

还尝试将所有模型直接移动到'models'目录中,从模型中移除'module'行,并从引用模型中移除所有'Wiki ::'。我仍然得到'未初始化的常量Page :: PageAttachment'错误。 – ryanpitts1 2015-02-10 21:11:26

+0

好吧,你可以在下面看到我的答案,但显然它是导致错误的'PageAttachment'模型中的一行。没有在上面的示例中添加该行,因为错误消息并未在该方向上锁定我。我认为这个模型一般不会被发现 - 不是模型内部存在错误。 – ryanpitts1 2015-02-10 21:55:14

相关问题