I created some simple tool where you can upload .csv file and display output on the website.
To do it you have press 'Browse', select file, and then press button 'Generate' and it will display output from .csv file (you can delete/edit each record).
My question is it possible to avoid selecting file manually?
I want to have only button 'Generate', after pressing it will automatically upload file and display. Of course file will be stored in some directory (directory and file name once defined won't be changed, only info in .csv could be different).
Any idea?
I found a way to read from file instead of browsing, in index.html.erb:
<%= form_tag import_reports_path, multipart: true do %>
<%= File.open("public/uploadfile/a_ruby_csv.csv") {|f| f.read} %>
<%= submit_tag "Generate report" %>
Before that I have:
<%= file_field_tag :file %>
<%= submit_tag "Generate report" %
How can I connect it with file.path ( ':file' )?
after I use open/read I have error - syntax error, unexpected ':'
Related
I have an ActiveAdmin page I want to use to upload a variety of files to various controllers:
ActiveAdmin.register_page "Import" do
content do
columns do
column do
panel "Overview" do
para "This is the admin Import page."
end
end
column do
panel "Update/Import Matters" do
para "This is some info about what this form should take."
form_tag import_matters_path, multipart: true do
file_field_tag :file
submit_tag "Import"
end
end
end
end
end
end
The page displays, but the right hand column only displays the panel with the text "this is some info..." When I comment out that line, I get an import button but no way to upload a file. When I comment out the submit_tag and the para line, I get the 'choose file' button to select a file to import, but nothing else.
The panel seems to be unable to concatenate the html from the form properly, but I'm not clear how to do that myself.
I ended up using a partial which seemed to fix the issue. This ended up with the panel looking like this:
panel "Update/Import Matters" do
para "Some info about the form."
render 'matters_form'
end
And /app/views/admin/import/_matters_form.html.erb:
<%= form_tag import_matters_path, multipart: true do %>
<%= file_field_tag :file %>
<%= submit_tag "Import" %>
<% end %>
This might not be the only way, but partials definitely worked in this situation.
I'm building an app where users can upload csv files to load data onto our platform. I have a form that lets a user upload a file and a method that saves the file in a specified folder. I want to append the user_id to the filename. How would I do that?
my form:
<%= form_tag import_listings_path, multipart: true do %>
<%= file_field_tag :my_file %>
<%= hidden_field_tag :user_id, current_user.id %>
<%= submit_tag "Import CSV" %>
<% end %>
my controller method:
def import
tmp = params[:my_file].tempfile
file = File.join("public", params[:my_file].original_filename)
FileUtils.cp tmp.path, file
end
For example, if a user uploads test.csv and their user_id is 20. I want the new filename to be test20.csv
A bit ugly but this should do the job... basically extracting the file extension (obtaining the last string after the '.'), appending the user id and adding the extension back:
x = params[:my_file].original_filename.split('.')
x[x.length - 2] += params[:user_id]
file = File.join("public", x.join('.'))
I am using Carrierwave for file uploading and have got the following form, which allows me to submit several files:
<%= form_tag load_patterns_contacts_path, multipart: true, multiple: true do %>
<%= file_field_tag 'qqfile[]', id: "upload_pattern", multiple: true %>
<%= submit_tag "Load", id: "save_pattern", :class => 'btn btn-primary btn-success', multiple: true%>
<% end %>
Here is the code in my controller, which load submited files to the server:
#uploader = EmailPatternsUploader.new
params[:qqfile].each do |p|
tempfile = open(p.original_filename)
puts tempfile
#uploader.store!(tempfile)
end
redirect_to contacts_path
flash[:success] = "Uploaded successfully."
It works fine, if filename looks like "text.xlsx", "image.jpg" etc. But if it is contains special symbols like "_partial.html.erb" then I have got Errno:ENOENT (No such file or directory - _partial.html.erb)
I have tried to add
CarrierWave::SanitizedFile.sanitize_regexp = /[^[:word:]\.\_\-\+]/
in my carrierwave.rb initializer, but it gives no result.
Thanks in advance for help!
UPDATE:
I have understood, that the problem not in special symbol "_", but in the fact, that samples I am trying to upload contains two dots ("."). I think I need to modify regular expression in order to avoid two dots
UPDATE:
I am sorry for the last comments. I have understood, that the matter not in special symbols at all and not in a name of file. The problem that i can upload files only from {Rails.root} path. If I choose another directory, I have got aforementioned error and cannot upload a file. How can I configure Carrierwave path directory?
Finally find an answer on my question.
The error was in these strings of code:
params[:qqfile].each do |p|
tempfile = open(p.original_filename)
puts tempfile
#uploader.store!(tempfile)
end
I have understood, that I need to pass an object ActionDispatch::Http::UploadedFile in Carrierwave store! method. Thats why the mentioned above code shall be the following:
params[:qqfile].each do |p|
puts p.original_filename
puts p
#uploader.store!(p)
end
==================================================================================
Hope someone find this solution for multiple file uploading with Carrierwave and without JQuery useful.
1) Create an uploader, using Carrierwave.
rails g uploader EmailPatterns
2) Create a custom action for your controller (watch Railscast#35 and Railscast#38 to make it clear) and put there something like this (load_patterns in my case):
def load_patterns
#uploader = EmailPatternsUploader.new
params[:qqfile].each {|p| #uploader.store!(p)}
redirect_to contacts_path
flash[:success] = "Uploaded successfully"
end
To make it work you need to specify custom route(config/routes.rb) for your action:
resources :contacts do
collection { post :load_patterns}
end
and to create a form, where you will get params with your uploading files (see p.3)
3) Create form, where you need to specify the option multiple:true in order to allow user to select several files to load (param name with [ ] is a necessary requirement, because we are loading several files) :
<%= form_tag load_patterns_contacts_path, multipart: true, multiple: true do %>
<%= file_field_tag 'qqfile[]', id: "upload_pattern", multiple: true %>
<%= submit_tag "Load", id: "save_pattern", :class => 'btn btn-primary btn-success', multiple: true%>
<% end %>
Then your custom action will work.
How to get full files path of attached excel file in rails without any gem.
My view is
<%= form_for #hotel, :url => { :action => "create_by_excel_sheet" } do |f| %>
<%= f.file_field :excel_sheet %>
<%= f.submit %>
<%end%>
and when submit the form the attached excel file path I want to receive in create_by_excel_sheet action
I tried
params["hotel"]["excel_sheet"].tempfile.path then it return "/tmp/RackMultipart20130921-3387-1ffc97o" not a file path.
Ex: /file_path/file_name.file_extension
I am not sure but it seems to me that you get really full path and it starts from your system root /. You can easy to check it.
I think it is logical to put not yet saved files (but already uploaded) to system temp folder.
I want to get xml file content on the server by Ruby on Rails.
How can i do that without upload file. and send xml file content from client to server in user browser.
exactly what I want to do. writing an app to get gpx file content from a form. user should enter file path in the form and submit.
This is what happens when you upload a file in Rails, i.e. with a form like
<%= form_for #page, multipart: true do |f| %>
<%= f.file_field :name %>
<%= f.submit %>
<% end %>
you get a ActionDispatch::Http::UploadedFile object in params[:page][:name]. This UploadedFile contains among others a File object called tempfile.
You can read and parse the content of the uploaded XML file from this tempfile
Normally you would use a gem like paperclip or carrierwave to handle file uploads.