I need to upload a file in rails without any gems, so I decided just to use
<%= file_field 'upload', 'datafile' %></p>
without any form and etc. Just file_field and in controller I tried to catch it with
name = params[:upload][:datafile].original_filename
it shows me an error:
undefined method `original_filename' for "me.jpg":String
my params:
"upload"=>{"datafile"=>"me.jpg"}
It seems that I cannot use only file_field alone without any form, can I? Or I should alway include it into a form? If yes, is it possible to do it without using a form?
Please verify you have added multipart: true option in your form_tag or not.
Syntax:-
<%= form_tag '/upload', multipart: true do %>
<label for="file">File to Upload</label> <%= file_field_tag "file" %>
<%= submit_tag %>
<% end %>
if you are not using form you should use FormTagHelper file_field_tag
this might help you FormTagHelper/file_field_tag
Related
I am trying to get the name of the file uploaded by a user before ActiveStorage goes on to save it. The form is generated using form_with and is shown below:
<%= form_with model: upload do |form| %>
<div class="">
<%= form.file_field :files, multiple: true, direct_upload: true, required: true %>
<%= form.label :files, '', class: 'icon ion-ios-cloud-upload' do %>
<span>click the icon to select files</span>
<% end %>
<div class="actions">
<%= form.submit "Upload", class: "btn btn-primary" %>
</div>
</div>
<% end %>
I have tried accessing params[:upload][files] and calling .original_filename on it as described here but I get the error NoMethodError: undefined method `original_filename' for #<String:0x007fac77fd18c8>.
The file does come back as a string when I inspect the params, so how do I get the filename or how do I get original_filename to work?
I was finally able to get the file name by doing file.blob.filename after the file had been attached.
Based on the ActiveStorage documentation, I found this worked for me:
file.filename.to_s
In my case I have a model with
has_one_attached :file
OP's situation is slightly different and your situation may differ too, so adjust accordingly.
Troy's solution did not work for me as-is. I had to add the .to_s to get the file name instead of an ActiveStorage::Filename object. But I'm not sure why he's using .blob in there. I didn't need it, but perhaps there's a good reason.
You need to add multipart: true to your form.
https://guides.rubyonrails.org/form_helpers.html#uploading-files
The other thing is that if you have multiple files you'll have multiple filenames.
params[:upload][files].each do |file|
file.original_filename
end
Just running
rake assets:precompile
worked for me.
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!
I have a form for which I am using checkboxes (Not using radio buttons for my purpose). The problem I run into is when I submit a form, I get an error saying params is missing or value is empty:checkup. I am trying to use hidden filed but get the same error. HOw to have an option of sending only one if selected?
def checkup_params
params.require(:checkup).permit(:eye, :heart)
end
my form:
<%= form_for(#checkup) do |f| %>
<%= hidden_field_tag "checkup[eye]", nil %>
<%= check_box_tag :eye, "eye" %>
<%= hidden_field_tag "checkup[heart]", nil %>
<%= check_box_tag :heart, "heart" %>
<%= f.submit %>
<% end %>
I suggest reading this guide http://guides.rubyonrails.org/form_helpers.html#dealing-with-model-objects
Most specifically section 2.2. You really shouldn't be using tag helpers here.
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!
With Cloudinary and their Carrierwave plugin I can write a form in my view that will upload an image to their cloud and bind it to a model attribute called picture, like so:
<%= form_for(#post) do |post_form| %>
<%= post_form.hidden_field(:picture_cache) %>
<%= post_form.file_field(:picture) %>
<% end %>
This works. But I can't figure out how to bind the attribute to the model while following their documentation for direct uploads in Rails. Their example uses a form_tag that isn't bound to a model:
<%= form_tag(some_path, :method => :post) do %>
<%= cl_image_upload_tag(:image_id) %>
...
<%= end %>
I'm looking for some example that's like <%= post_form.some_upload_method(:picture) %>. Any chance someone else has done this for direct uploads for their models and knows what I'm looking for?
You can use the following syntax:
<%= post_form.cl_image_upload(:picture) %>