file_field does not choose files in production - ruby-on-rails

I am using Paperclip to manage files.
File upload works perfectly in development, but in production environment file_field tag is not choosing anything.
Form:
<div class="change-avatar">
<%= form_for #user, :html => { multipart: true } do |f| %>
<%= f.file_field :avatar, id: "avatarInput" %>
<a id="avatarInput_alt">Change avatar</a>
<%= f.submit id: "submitAvatar"%>
<% end %>
</div>
The dialog is being showed when I click on the 'Choose file' button, but after I choose a file it still shows no file selected.
When I call document.getElementById("avatarInput").files the output is FileList {length: 0, item: function}.
Does anyone know what is wrong?
Thank you in advance

Related

Why does Rails form multiple: true change submitted param form

I'm playing with ActiveStorage and trying to upload some files locally. Everything works great with the code below, but only if I remove multiple: true from the form. When it is on the form, I get an unpermitted param "files" error in the console. The unpermitted param comes from the way the form is submitting the hash.
Without multiple: true the hash lists attachments as an array (this is the working version):
"article"=>{"files"=>[#<ActionDispatch::Http::UploadedFile:0x007fb4e8e287f0
But with it turned on it it removes the array:
"article"=>{"files"=>#<ActionDispatch::Http::UploadedFile:0x007fb4eb07b7d0
What is causing this form behavior and how can I fix it?
I got the code sample from Engine Yard and here is the project code:
<h3>Attach files to this post</h3>
<%= form_with model: #article, local: true do |f| %>
<div class="form-row">
<%= f.label :file_upload, 'Attach a file' %>
<%= f.file_field :files, multiple: true %>
</div>
<%= f.submit %>
<% end %>
<h3>Attached files</h3>
<% #article.files.each do |file| %>
<%= link_to file.blob.filename, url_for(file) %>
<% end %>
When you use multiple: true you need to permit an array explicit in the article_params for :files:
For example:
params.require(:article).permit(:author, :text, files: [])
You can read more under Action Controller
Good luck!

Rails - updating a partial from a nested model on ajax file upload

I have a rails app with a model, products that has a nested model, productdocuments. I'm using Carrierwave to upload PDFs, Word Docs, etc .. as the documents.
In my edit.html.erb I have my form field rendering in a partial;
<%= f.file_field :resource, name: "product[productdocuments_attributes][][resource]", multiple: true, :placeholder => "Resource", id: "product_productdocuments_attributes" %>
And an array of the uploaded docs in a partial:
<%= render #product.productdocuments %>
The issue I'm seeing is that when I upload a file, rails is triggering a render of update.js.erb on the Product NOT the create.js.erb from productdocument This makes it harder to append the partial with the new productdocument.
Any idea how to to trigger create.js.erb from Productdocuments?
I guess in the edit.html.erb there's something like:
<%= form_for #product, remote: true do |f| %>
<%= f.file_field :resource, name: "product[productdocuments_attributes][][resource]", multiple: true, :placeholder => "Resource", id: "product_productdocuments_attributes" %>
<%= f.submit %>
<% end %>
which on submit sends request to the ProductsController, not to ProductDocumentsController as you expect. Since #product is a persisted record, the request goes to update method.
In this case, an additional form for product documents is going solve the problem:
<%= form_for #product.productdocuments.build, remote: true do |f| %>
<%= f.file_field :resource, , multiple: true, :placeholder => "Resource" %>
<%= f.submit %>
<% end %>
#product.productdocuments.build builds a new object, therefore request is going to create method.
Hope it helps.

TinyMCE: using maxchars plugin in Rails

I've got a Rails app that is using TinyMCE, via the TinyMCE-Rails gem. I'm trying to use the maxchars plugin to get word count going. I've added the plugin to config/tinymce.yml
plugins:
- fullscreen
- maxchars
And I've added the max_chars and max_chars_indicators to my view.
<%= simple_form_for #project |f| %>
<%= f.input :overview, input_html: { :class => "tinymce", :rows => 70, :cols => 140 } %>
<%= tinymce max_chars: 6000, max_chars_indicator: "characterCounter" %>
<div id="characterCounter">
</div>
<div class="form-actions add-top">
<hr>
<p>
<%= f.button :submit, :class => 'inline-block large_button add-bottom' %>
</p>
</div>
<% end %>
But now the editor doesn't even appear. Is there something I'm missing when it comes to adding this plugin?
Complex solution for this problem: had to actually read the documentation! (Outrageous, right?!)
I had neglected to actually download the plugin files. I created a directory, app/assets/javascripts/tinymce/plugins/ and popped in the folder containing the necessary JS files. Once I started my server, everything worked great.

CarrierWave and Nested Form Gem Redisplay :: HTML File Input Icon

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.

carrierwave doesn't render, path exists but image doesn't show up

ive been trying to render a picture from carrierwave. i believe it is uploaded correctly because when i view the page source, i see
<img alt="Photo_44" src="/uploads/user/image/59/Photo_44.jpg" />
however by clicking on that src url, i get
No route matches [GET] "/uploads/user/image/59/Photo_44.jpg"
im using the default settings for carrierwave. the image does correctly get uploaded to my image column in my users table and locally, the path
sasha/Desktop/rails_projects/myproject/public/uploads/user/image/59/Photo_44.jpg
exists as well. however it won't display correctly. ive been following the railscasts
http://railscasts.com/episodes/253-carrierwave-file-uploads?autoplay=true
and reading
https://github.com/jnicklas/carrierwave
but i cant seem to figure out what is wrong. where i upload the image is here
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name),
:html => { :method => :put, :multipart => true }) do |f| %>
<%= devise_error_messages! %>
<%= render '/shared/fields', object: f.object, f: f %>
<%= f.submit "Save changes", :class => "btn btn-large btn-primary", :style =>"display:block;" %>
<div class="edit_avatar" >
<%= f.file_field :image %>
</div>
<% end %>
and im trying to render it by...
<%= image_tag #user.image_url.to_s %>
what am i doing wrong?
help would be appreciated = )
thank you
Try set config.serve_static_assets = true if you haven't yet, should help.

Resources