Note that there are some explanatory texts on larger screens.

plurals
  1. POForms for file upload in nested model in rails
    primarykey
    data
    text
    <p>I'm aiming to implement a one-to-many -> one-to-many structure like the one described in the <a href="http://guides.rubyonrails.org/association_basics.html" rel="nofollow noreferrer">rails api here</a>:</p> <pre><code>class Document &lt; ActiveRecord::Base has_many :sections has_many :paragraphs, :through =&gt; :sections end class Section &lt; ActiveRecord::Base belongs_to :document has_many :paragraphs end class Paragraph &lt; ActiveRecord::Base belongs_to :section end </code></pre> <p>I'm trying to let the paragraphs class have a file upload through carrierwave, as described in <a href="http://railscasts.com/episodes/253-carrierwave-file-uploads" rel="nofollow noreferrer">the railscast</a> on the topic, <a href="http://railscasts.com/episodes/196-nested-model-form-revised" rel="nofollow noreferrer">within a nested AJAX form that lets me add and remove sections and paragraphs</a>.</p> <p>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:</p> <pre><code> def show @document = Document.find_by_username(params[:documentname]) @section = @document.sections # @paragraph = @section.paragraphs #DONT UNDERSTAND WHY UNCOMMENTING THIS DOESNT WORK. end </code></pre> <p>Both my document and section models have the <code>accepts_nested_attributes_for</code> set up. However, when I load a document view, if I uncomment the <code>@paragraph</code> above, I get an error that suggests section assignment was successful, but paragraph assignment was unsuccessful for lack of a method:</p> <pre><code>NoMethodError in DocumentController#show undefined method `paragraph' for #&lt;ActiveRecord::Relation:0x007f9b6409c340&gt; </code></pre> <p>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.</p> <p><em>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?</em></p> <p>If you have any other suggestions on how to structure this better, I'd appreciate it.</p> <p>Also, what would be a good way to go about debugging this? I seem to be missing a method, which one, where?</p> <p>I've referenced these, but wasn't able to find a solution:</p> <ul> <li><a href="http://railscasts.com/episodes/196-nested-model-form-revised" rel="nofollow noreferrer">http://railscasts.com/episodes/196-nested-model-form-revised</a></li> <li><a href="http://railscasts.com/episodes/253-carrierwave-file-uploads" rel="nofollow noreferrer">http://railscasts.com/episodes/253-carrierwave-file-uploads</a></li> <li><a href="https://stackoverflow.com/questions/5009550/carrierwave-upload-with-nested-forms">Carrierwave upload with nested forms?</a></li> <li><a href="https://stackoverflow.com/questions/9357607/rails-3-jquery-file-upload-nested-model">Rails 3 + JQuery-File-Upload + Nested Model</a></li> <li><a href="https://stackoverflow.com/questions/5533879/rails-3-and-a-nested-jquery-file-upload-model">Rails 3 and a Nested jQuery File Upload Model</a></li> </ul> <h1>Update 1</h1> <p>I suspect the problem is coming from my bad show commands? It can't multi-index the right sections to the right paragraphs? So,</p> <pre><code> def show @document = Document.find_by_username(params[:documentname]) @section = @document.sections # @paragraph = @section.paragraphs #DONT UNDERSTAND WHY UNCOMMENTING THIS DOESNT WORK. end </code></pre> <p>should be more like:</p> <pre><code> def show @document = Document.find_by_username(params[:documentname]) @section = @document.sections @paragraph = @section.find(params[:section_id]).paragraphs end </code></pre> <p>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:</p> <pre><code>ActiveRecord::RecordNotFound in DocumentController#show Couldn't find Section without an ID </code></pre> <h1>Update 2</h1> <p>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:</p> <p>in the document controller:</p> <pre><code>def show @document = Document.find_by_username(params[:documentname]) @section = @document.sections end </code></pre> <p>in the section controller:</p> <pre><code>def show @section = Section.find(params[:id]) @paragraph = @section.pictures end </code></pre> <p><em>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?</em></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload