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