1

I'm aiming to implement a one-to-many -> one-to-many structure like the one described in the rails api here:

class Document < ActiveRecord::Base
  has_many :sections
  has_many :paragraphs, :through => :sections
end

class Section < ActiveRecord::Base
  belongs_to :document
  has_many :paragraphs
end

class Paragraph < ActiveRecord::Base
  belongs_to :section
end

I'm trying to let the paragraphs class have a file upload through carrierwave, as described in the railscast on the topic, within a nested AJAX form that lets me add and remove sections and paragraphs.

Since I will be viewing the pictures only within the full document, I'm trying to keep the code within my Document controller. So, right now, the controller there is:

  def show
    @document = Document.find_by_username(params[:documentname])
    @section = @document.sections
#   @paragraph = @section.paragraphs #DONT UNDERSTAND WHY UNCOMMENTING THIS DOESNT WORK.
  end

Both my document and section models have the accepts_nested_attributes_for set up. However, when I load a document view, if I uncomment the @paragraph above, I get an error that suggests section assignment was successful, but paragraph assignment was unsuccessful for lack of a method:

NoMethodError in DocumentController#show
undefined method `paragraph' for #<ActiveRecord::Relation:0x007f9b6409c340>

If I comment it out, it appears to load the section assignment fine. Currently, my sections and paragraph tables are completely blank because I can't set up the forms, so maybe that is part of the problem. But I think nil would give me a different error than no method, right? So, I suspect something is wrong under the hood already.

So, my main bits of confusion here: How and where do I construct a form controlled by my document controller to accept any data, and then especially to work with the carrierwave file upload?

If you have any other suggestions on how to structure this better, I'd appreciate it.

Also, what would be a good way to go about debugging this? I seem to be missing a method, which one, where?

I've referenced these, but wasn't able to find a solution:

Update 1

I suspect the problem is coming from my bad show commands? It can't multi-index the right sections to the right paragraphs? So,

  def show
    @document = Document.find_by_username(params[:documentname])
    @section = @document.sections
#   @paragraph = @section.paragraphs #DONT UNDERSTAND WHY UNCOMMENTING THIS DOESNT WORK.
  end

should be more like:

  def show
    @document = Document.find_by_username(params[:documentname])
    @section = @document.sections
    @paragraph = @section.find(params[:section_id]).paragraphs
  end

This doesn't work, but something like that? There's a way it doesn't seem to be linking the sections to the individual paragraphs. The error I get with the above is:

ActiveRecord::RecordNotFound in DocumentController#show

Couldn't find Section without an ID

Update 2

Maybe that means I should telescope all of the show commands? ie, that getting the right paragraphs for the right section would be covered more like so:

in the document controller:

def show
    @document = Document.find_by_username(params[:documentname])
    @section = @document.sections
end

in the section controller:

def show
    @section = Section.find(params[:id])
    @paragraph = @section.pictures
end

So, if that is the case, how do I set up my nested form? To jointly create (1) sections, (2) paragraphs, and (3) images in the railcasts-style ajax interface from within document#show page?

Community
  • 1
  • 1
Mittenchops
  • 18,633
  • 33
  • 128
  • 246

3 Answers3

0

Have you tried ?

  @section = @document.sections.build
  @paragraph = @sections.paragraphs.build
RickyonRails
  • 13
  • 1
  • 6
  • Specifically, this does not work because the foreign key document_id that will link sections to the right document---the foreign key does not exist until /after/ the build commands run. Which means the foreign key is filled with nil, rather than the auto-increment id. I have a more detailed post on the problem here: http://stackoverflow.com/questions/11875829/assigning-attributes-with-build-not-working-on-creation-cant-assign-foreign-k – Mittenchops Aug 09 '12 at 18:28
  • Also, I think the second line would have to be `@paragraph = @section.paragraphs.build`, where section is singular. – Mittenchops Aug 10 '12 at 16:02
0

None of the SO answers I searched for worked for me, because the initial key did not exist at the time of upload, so it was impossible to link to it.

So, my solution was just not to try this design. Make a different form with less nesting.

Mittenchops
  • 18,633
  • 33
  • 128
  • 246
0

I wrote a huge answer in another post that outlined how I got multiple-file uploading working in Rails with Carrierwave, jQuery File Uploader, and a nested model. Perhaps it'll help?

Rails 3 + JQuery-File-Upload + Nested Model

Community
  • 1
  • 1
Kyle Carlson
  • 7,967
  • 5
  • 35
  • 43