Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do i set a parent_id using s3_direct_upload gem?
    primarykey
    data
    text
    <p>Newbie on Rails 3.2.14, testing on Heroku, uploading image files to S3 with s3_direct_upload gem.</p> <p>I've got the gem working fine. Drag and drop, progress bars and direct upload to S3 work great. The upload is tied to an Attachment model. The new record is created properly on upload. Fine so far.</p> <p>The issue I'm having is that I want to set the <code>parent_id</code> field of the new record but don't know how to pass it in. The gem doesn't use a conventional form to submit the upload, it's handled by via javascript:</p> <pre><code>&lt;%= s3_uploader_form callback_url: attachments_url, callback_param: "attachment[attachment_url]", id: "myS3Uploader" do %&gt; &lt;%= file_field_tag :file, multiple: true %&gt; &lt;% end %&gt; </code></pre> <p>The create action in <code>attachments_controller.rb</code>:</p> <pre><code>def create @attachment = Attachment.create(params[:attachment]) end </code></pre> <p>calls a javascript view <code>create.js.erb</code>:</p> <pre><code>&lt;% if @attachment.new_record? %&gt; alert("Failed to upload attachment: &lt;%= j @attachment.errors.full_messages.join(', ').html_safe %&gt;"); &lt;% else %&gt; $("#attachments").prepend("&lt;%= j render(@attachment) %&gt;"); &lt;% end %&gt; </code></pre> <p>which checks if the record save worked and adds the new upload thumbnail to the current view.</p> <p>Since I'm not dealing with a regular form or <code>create</code> action I'm a bit stumped.</p> <p>I can set default values in the Attachment model with a <code>before_save</code>:</p> <pre><code>before_save :set_defaults def set_defaults self.name = "Photo" self.parent_id = 1 end </code></pre> <p>but the <code>parent_id</code> will be defined on the page where the uploads are done, during the creation of the parent object (i.e. a blog post).</p> <p>I know I should be using <code>has_many</code> and <code>belongs_to</code> and they're set up and working fine but since I'm not using a conventional form to submit the data I don't know how to assign the parent_id.</p> <p>I've tried passing <code>parent_id</code> in the form and accessing it in the model:</p> <pre><code>&lt;%= s3_uploader_form callback_url: attachments_url(:parent_id =&gt; 99), callback_param: "attachment[attachment_url]", id: "myS3Uploader" do %&gt; </code></pre> <p>It didn't work and I know I'm not supposed to be accessing params inside the model anyway.</p> <p>Any help to get me past this point would be greatly appreciated.</p>
    singulars
    1. This table or related slice is empty.
    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.
    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