Skip to main content

Add folder parameter to Rails simple form upload

Comments

5 comments

  • Aleksandar Kostadinov

    Hi Hakeem,

    Since the folder is innately part of the public_id, could you try to add the folder as part of the public_id when rendering the form? For example -

    <%= f.input :resume, :public_id => "talents/my_name" %>

    Let us know if that works?

    0
  • Hakeem Baba

    Hi Aleksandar, 

    Thanks for your response.

    I just tried it out but it did not work 

    0
  • Shirly Manor

    Hi Hakeem,

    Can you please share what do you get? (print it out)?

    Thanks,

     

    0
  • Hakeem Baba

    I do not get an error per se. But the stack trace looks like this 

     

      Parameters: {"talent"=>{"full_name"=>"", "email"=>"", "mobile_number"=>"", "resume"=>#<ActionDispatch::Http::UploadedFile:0x00007ffc21aba8b8 @tempfile=#<Tempfile:/var/folders/7h/tdhsj9mn4p53fsnch2hn2500000gn/T/RackMultipart20200526-10917-ccz4vu.pdf>, @original_filename="Resume.pdf", @content_type="application/pdf", @headers="Content-Disposition: form-data; name=\"talent[resume]\"; filename=\"Resume.pdf\"\r\nContent-Type: application/pdf\r\n">}, "commit"=>"Submit Application", "job_id"=>"79"}

    From the stack trace I can see that the public_id is not been passed as part of the params

    0
  • Aleksandar Kostadinov

    Hi Hakeem,

    Thanks for sharing that.

    Could you try with the direct client-side uploads detailed in this section of the documentation - https://cloudinary.com/documentation/rails_image_and_video_upload#direct_upload_file_tag? That will allow you to render a form input field for uploading directly and as part of the method call to render it, you should be able to include a "folder" parameter or instead, a "public_id" parameter which will include both the folder and name of the file. For example "talents/my_image_name".

    Once it uploads successfully, the identifier (including resource_type, type version and public_id) would be set in a hidden field which you can define. Then in your controller, you can take this value and use the StoredFile class to generate a preloaded file and attach it to the model. There is an example of that further down in the above-linked documentation. Specifically for CarrierWave, we also have a class for that which you can use to preload your files. Please see the following support article for details - https://support.cloudinary.com/hc/en-us/articles/213916769-How-to-inject-the-information-of-a-preloaded-image-into-my-Carrierwave-mounted-entity-

    Similar to the general example, you would take the value (identifier) from the form's hidden input and supply that to "Cloudinary::CarrierWave::StoredFile" to generate and you can attach this preloaded object to your model.

    0

Post is closed for comments.