在Rails中使用嵌套表单的未初始化常量

时间:2015-02-10 17:11:09

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

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

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

2 个答案:

答案 0 :(得分: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

您还可以将这些模型移动到各自的根文件夹(unnamespaced),但这取决于您的应用程序设计。

答案 1 :(得分:0)

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

一旦我修改了命名以便彼此匹配,uninitialized constant Page::PageAttachment错误消失了,页面加载正常。奇怪的。仍然不确定为什么当它真的是上传者的错时抱怨PageAttachment。在任何情况下,它现在都已修复,在此过程中我决定不使用命名空间我的模型。控制器我仍将保持命名空间,但不是模型。