Add folder parameter to Rails simple form upload
In my rails app, I use Carrierwave to upload files to Cloudinary. I also use Simple form to render forms in views. I am trying to upload my files to specific folders in cloudinary.
In the uploader.rb file, this works well
def public_id
return "my_folder/" + model.full_name
end
But what I want is to specify the folder parameter in the views using simple form. So i tried something like this,
<%= f.input :resume, :folder => "talents/", :public_id => "my_name" %>
The files get uploaded to Cloudinary but not the folder. Is it possible to pass the folder parameter successfully to simple form in the views?
-
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 -
Hi Aleksandar,
Thanks for your response.
I just tried it out but it did not work
0 -
Hi Hakeem,
Can you please share what do you get? (print it out)?
Thanks,
0 -
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 -
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.
Comments
5 comments