Search code examples
ruby-on-rails-4nested-formsuninitialized-constant

Uninitialized constant using nested forms in Rails


I keep getting an error saying uninitialized constant Page::PageAttachment when trying to create a nested form using the nested_form gem. Here is some relevant code. Let me know if you need any other code or have any questions.

Stacktrace isn't really telling me anything except that the failing line is this one: <%= 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 %>

Update

My folder structure is as follows:

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

Solution

  • Well, rails just wasn't telling me how deep the error was i guess. In the PageAttachment model file i had this line mount_uploader :file, FileUploader of which i am using Carrierwave to handle file uploads. The actual uploader class name was WikiUploader.

    As soon as i fixed the naming to match up with each other, the uninitialized constant Page::PageAttachment error went away and the page loaded fine. Weird. Still not sure why it was complaining about PageAttachment when it was really the uploader's fault. In any case, it's fixed now and in the process i decided against namespacing my models. The controllers i will still keep namespaced but not the models.