CarrierWave and Nested Form Gem Redisplay :: HTML File Input Icon - ruby-on-rails

I'm using CarrierWave and Nested Form Gem. Using the section titled 'Making uploads work across form redisplays' on the Carrierwave github page(https://github.com/jnicklas/carrierwave) I'm able to successfully store the cache so that even if the user makes a mistake then the file is saved. The first time I hit the page and click "Upload File" it works and shows the name of the file as well as a small thumbnail icon indicating the type of file that has been uploaded.
However the file input button is blank upon a redisplay and the icon is gone. However I know it's there because if I submit again with valid information and leave the file input button alone it creates a new resource. Here's a snippet of my code.
<%= nested_form_for #resource, :html=>{:multipart => true } do |f| %>
<p>
<%= f.label :title %><br />
<%= f.text_field :title %>
</p>
<%= f.fields_for :attachments do |attachment_form| %>
<p>
<%= attachment_form.label :file %>
<% if !attachment_form.object.file.path.blank? %>
<% file_info = get_cache_file_info attachment_form.object.file.path %>
<%= image_tag(File.join('/tmp/cache/', file_info[:folder])) #THIS DISPLAY CORRECTLY %>
<% end %>
<%= attachment_form.file_field :file %>
<%= attachment_form.hidden_field :file_cache %>
</p>
<%= attachment_form.link_to_remove "Remove this attachment" %>
<% end %>
<%= f.link_to_add "Add attachment", :attachments %>
<p><%= f.submit %></p>
<% end %>
I'm wondering if I have to edit the HTML file input to display the icon or is there some other Rails Way to take care of this. Also the image_tag display correctly, but for my application having users upload text files so the images aren't very helpful.
Thanks in advance!
Update
I also tried setting the value of the 'input' tag like so:
<%= attachment_form.file_field :file, :value => "#{File.join(file_info[:folder], '/',attachment_form.object.file.identifier)}" %>
and that didn't work either. The value of the 'input' tag is set correctly, but it still shows up as "No file selected"

The CarrierWave introduction doesn't make this very clear, but it sounds like your code is functioning 'correctly'. The cache field just serves to ensure that your attachment will be saved after a valid submission.
This document explains that the file input element's FileList should be read only, presumably for security reasons.

Related

uploading empty file goes to edit instead of new

I recently noticed that on my form, if I try to upload an empty file the page will get redirected to edit instead of create. If I try to upload the file with some text in it, the form will direct to create. I couldn't find any indication that this would happen as I create my object every time (it's never persisted). Is there an explanation behind this?
The code looks something like this:
//controller
def upload
#new_cool_file = CoolFile.new
end
//form in upload.html.erb
<%= form_for #new_cool_file, html: {role: "form"} do |f| %>
<div class="form-group">
<%= f.label :file %>
<%= f.file_field :file %>
</div>
<%= f.submit "Submit"%>
<% end %>
I think you do some wrong in the rails routes.rb file , can you please see tutorial about file upload.
https://richonrails.com/articles/allowing-file-uploads-with-carrierwave
http://railscasts.com/episodes/253-carrierwave-file-uploads

Rails : From form_for to form_tag

For one of my projects i'm using https://github.com/rsantamaria/papercrop in addition to paperclip to crop pictures before upload.
It is said in the readme to do :
<%= form_for #user do |f| %>
<%= f.cropbox :avatar %>
<%= f.crop_preview :avatar %>
<%= f.submit 'Save' %>
<% end %>
The issue is that my form i need to adapt those function to is a form_tag.
I usually always manage to navigate between the two as needed but since my form is in my application layout ( needs to be there to be on all pages) i don't really use a controller for it but instead form_tag my_post_route_path :
<%= form_tag products_path, multipart: true do %>
name
<%= text_field_tag(:name) %>
</br>description
<%= text_field_tag(:description) %>
</br>image
</br><%= file_field_tag "image" %>
<%= submit_tag("create product") %>
<% end %>
I would like to know two things :
-How do you translate the methods from f.cropbox etc. to Form tag syntax ? i really don't see a way.
I've tried cropbox_tag but it didn't work ( and i didn't make any sens anyway)
-My second question is kind of different but where , when i have a form that need to pop up on all pages ( i'm using a jquery dialog to make it pop up and a button in the navbar ), should it be placed ? is the fact that i've put it in my application_view completely wrong , i've placed it cause the form is only for logged in users ?
Thanks to anyone who tries to help!

set custom message on file_field

i have a file_field on my form to upload user's avatars:
<%= f.file_field :avatar %>
The problem is, whether the model has already an avatar or not, the helper shows a "Not file selected" message. Only if you browse and add an image the message is replaced by the name of the selected image.
Basically, i'd like to show a message with the image name straight ahead if the user has already an image. Any ideas?
I'm sure you're saving the uploaded file in any way (carrierwave, paperclip etc.).
So you know whether you already have an avatar or not.
If you have one, show the file name or even a thumb, if not, show a note.
But you cannot avoid the "No file selected" message because it (aka the browser) sais:
There is no file selected to upload with this POST.
You will end up with somthing like:
<%- if f.object.avatar.present %>
Known Avatar: <%= image_tag f.object.avatar.url(:thumb) %>
<%- else %>
no Avatar uploaded yet
<%- end %>
<%= f.file_field :avatar %>
Remark:
I used a thumb url generating syntax of carrierwave. It may vary with other attachment gems.
I don't know your form object, that's why I used the f.object. If it's i.e. #user, you can simplify it:
<%- if #user.avatar.present %>
Known Avatar: <%= image_tag #user.avatar.url(:thumb) %>
<%- else %>
no Avatar uploaded yet
<%- end %>
<%= f.file_field :avatar %>

Displaying file field value on edit

When upload pictures, everything goes okay.
But on edit, it doesn't display the file fields value. Just an empty file_field, like nothing is there. Pic title displays correctly.
Other text is in hungarian.
_form.html.haml
= simple_nested_form_for(#post) do |f|
= f.input :title, label: 'Cím'
= f.input :body, label: "Test"
= f.fields_for :pics do |pic_form|
= pic_form.text_field :title
%br/
= pic_form.file_field :image
= pic_form.link_to_remove "Kép Törlése", class: "btn btn-warning"
%br/
%br/
%p= f.link_to_add "Kép hozzáadása", :pics, class: "btn btn-success"
%br/
%br/
= f.submit "Mentés", class: "btn btn-primary"
= javascript_include_tag :defaults, "nested_form"
How to pass, the existing file to the file_field?
You have to use a conditional to find out if the file(s) is/are attached. Using ActiveStorage, the conditional could look like this (for a field that accepts only one file):
<% if #my_object.my_file.attached? %>
<%= #my_object.my_file.blob.filename %>
<% else %>
<%= f.file_field :my_file %>
<% end %>
If the field accepts many files, you have to iterate to show the filenames:
<% #my_object.my_files.each do |i| %>
<%= i.filename %><br>
<% end %>
I am sure there is a corresponding way to handle this in Carrierwave.
I guess this is the default behavior of the file field. On editing a particular action, the id of the file is stored in the file field rather than its entire value.
In the code, I can see that you have used the field for and therefore, a relationship would have been setup between post and pic. So, on close inspection, you would find that Rails would send in the id of the file to the server when you click on the Submit button which is an indication that there is no need to upload or process the pic. However, if you select some file in the file field, then you can see that it does the actual uploading the file. This processing is well handled by Paperclip gem also.

CarrierWave and Nested Form Gem Redisplay

I have CarrierWave (0.6.1) and Nested Form gem installed. I have a Resource model with many Attachments which have a FileUploader.
I have a nested form where users can upload multiple files with one resource. I am following the section on github (https://github.com/jnicklas/carrierwave) that says how to make uploads work across redisplays unfortunately it's only for 1:1 ratio.
Here's the code that I have:
<%= nested_form_for #resource, :html=>{:multipart => true } do |f| %>
<p>
<%= f.label :title %>
<%= f.text_field :title %>
</p>
<%= f.fields_for :attachments, #attachment do |attachment_form| %>
<p>
<%= attachment_form.label :file %>
<%= attachment_form.file_field :file %>
<%= attachment_form.hidden_field :file_cache %>
<%= image_tag(attachment_form.file_url) if attachment_form.file? # DOESN'T WORK!!! %>
</p>
<%= attachment_form.link_to_remove "Remove this attachment" %>
<% end %>
<%= f.link_to_add "Add attachment", :attachments %>
<p><%= f.submit %></p>
<% end %>
Everything works and it populates the file_cache variable just fine for the attachment_form however I somehow need to add the following line in there to show the user the image of the document:
<%= image_tag(attachment_form.file_url) if attachment_form.file? %>
However there's a number of problems with this. First of all attachment_form is referencing the form_builder whereas I want the actual attachment. Second, attachment knows nothing about file.
Probably need to use another type of looping mechanism, but I'm new to Ruby so any help is appreciated.
Thanks all!
If you try this:
<%= image_tag(attachment_form.object.file_url) if attachment_form.object.file? %>
You will be able to show previous uploaded images. But if you want to display uploaded right now, you need to use something else. For example: https://github.com/blueimp/jQuery-File-Upload/wiki/jQuery-File-Upload-v4-for-Rails-3
Sorry, if I misunderstood your question.

Resources