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:
- http://railscasts.com/episodes/196-nested-model-form-revised
- http://railscasts.com/episodes/253-carrierwave-file-uploads
- Carrierwave upload with nested forms?
- Rails 3 + JQuery-File-Upload + Nested Model
- Rails 3 and a Nested jQuery File Upload Model
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?