Ruby on rails form not working with multiple files - ruby-on-rails

Well, I'm searching this for a couple of days and I really couldn't find a solution. I'm trying to send multiple files and store it locally(or maybe in the future in a s3 bucket) and save also to the db.
I noticed that I'm getting a string instead of the file itself!
I'm using rails 5.1 and ruby 3.2
Here is my code:
Controller:
all_files = params[:files]
all_files.each do |fil|
File.open(Rails.root.join('public', 'uploads', all_files.original_filename), 'wb') do |filea|
filea.write(all_files.read)
end
end
form
<%= form_for #docs, :url => docs_create_path, :html => { :multipart => true } do |f| %>
<%= f.file_field :files, :multiple => 'multiple', :name => 'files[]'%>
<%= f.submit( "Upload file" ) %> <% end %>
Common errors: undefined method `original_filename' for #Array:0x0000000006aeb338>

there is small mistake here replace all_files.orginal with file.orginal
all_files = params[:files]
all_files.each do |file|
File.open(Rails.root.join('public', 'uploads', file.original_filename), 'wb') do |temp_file|
temp_file.write(temp_file.read)
end
end

Related

why is form_ tag code is not working in ruby mine 5.4.3.2.1?

I am building an ROR application in ruby mine .But the following doesn't seems to run `
<%= form_tag upload_index_path({:action => 'uploadFile'},
:multipart => true) do %>
<p><label for="upload_file">Select File</label> :
<%= file_field 'upload', 'datafile' %></p>
<%= submit_tag "Upload" %>
<% end %>`
This code is in my view file. I am trying to build a site where i can upload files. Everything appears good but when i click the upload button the file doesn't get uploaded only the url changes. Why is that??
This is my controller code
class UploadController < ApplicationController
def index
render :file => 'app\views\upload\uploadfile.html.erb'
end
def uploadFile
post = DataFile.save(params[:upload])
render :text => "File has been uploaded successfully"
end
end
This is my model code
class DataFile < ActiveRecord::Base
# attr_accessible :title, :body
def self.save(upload)
name = upload['datafile'].original_filename
directory = "public/data"
# create the file path
path = File.join(directory, name)
# write the file
File.open(path, "wb") { |f| f.write(upload['datafile'].read) }
end
end
I have not been able to find solution. I have been trying for a week.
In your form_tag declaration, remove the multipart from the path options as follows:
<%= form_tag upload_index_path({:action => 'uploadFile'}) , :multipart => true do %>
<p>
<label for="upload_file">Select File</label> :
<%= file_field 'upload', 'datafile' %>
</p>
<%= submit_tag "Upload" %>
<% end %>
http://api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html#method-i-form_tag

File Upload By form_tag form on Ruby on Rails

I'd like to make a simple file uploader using tag_form on Rails 3.2.8.
But when I try to submit a image file, I get an error saying
Error Message (when I try to submit a image file)
NoMethodError in CoursesController#attachment
undefined method `original_filename' for "2012-03-02 21.53.55.jpg":String
----- BEGIN P.S.(20121216 19:32) -----
or
Error Message (when I added ":multipart => true" on show.html.erb)
Encoding::UndefinedConversionError in CoursesController#attachment
"\xFF" from ASCII-8BIT to UTF-8
----- END P.S. -----
It seems that the program consider the file as String?
There might be some problem in the view file.
I'd appreciate it if you help me with this problem. Here's my codes.
app/view/show.html.erb
<%= form_tag(attachment_course_path, :action=>'attachment') do %>
<div class="field">
<%= label_tag :file %>
<%= file_field_tag :file %>
</div>
<div class="actions">
<%= submit_tag 'Submit' %>
</div>
<% end %>
app/controller/courses_controller.rb
def attachment
t = Time.now.strftime("%Y%m%d%H%M%S")
uploaded_io = params[:file]
File.open(Rails.root.join('public', 'upload', uploaded_io.original_filename), 'w') do |file|
file.write(uploaded_io.read)
end
end
config/routes.rb
resources :courses, :only => [ :show ] do
member do
post :attachment
end
end
seems the form is not sending files with request. you need to set :multipart => true in form_tag.
The problem seems similar to RoR upload Undefined encoding conversion
After setting :multipart => true in form_tag you need to open the file in binary mode ('wb' instead of 'w'):
app/controller/courses_controller.rb
...
File.open(Rails.root.join('public', 'upload', uploaded_io.original_filename), 'wb') do |file|
file.write(uploaded_io.read)
end
...

Error, Ruby on Rails: Encoding::UndefinedConversionError in CoursesController#attachment "\xFF" from ASCII-8BIT to UTF-8

I'd like to make a simple file uploader using tag_form on Rails 3.2.8.
But when I try to submit a image file, I get an error saying
Error Message (when I try to submit a image file)
Encoding::UndefinedConversionError in CoursesController#attachment
"\xFF" from ASCII-8BIT to UTF-8
I'd appreciate it if you help me with this problem.
Here's my codes.
app/view/show.html.erb
<%= form_tag(attachment_course_path, :action=>'attachment', :multipart => true) do %>
<div class="field">
<%= label_tag :file %>
<%= file_field_tag :file %>
</div>
<div class="actions">
<%= submit_tag 'Submit' %>
</div>
<% end %>
app/controller/courses_controller.rb
def attachment
t = Time.now.strftime("%Y%m%d%H%M%S")
uploaded_io = params[:file]
File.open(Rails.root.join('public', 'upload', uploaded_io.original_filename), 'w') do |file|
file.write(uploaded_io.read)
end
end
config/routes.rb
resources :courses, :only => [ :show ] do
member do
post :attachment
end
end
Try to open the file in binary mode ('wb' instead of 'w'):
...
File.open(Rails.root.join('public', 'upload', uploaded_io.original_filename), 'wb') do |file|
file.write(uploaded_io.read)
end
Ruby Docs IO Open Mode

How do I upload files in Rails without plugins

I am extremely new to rails and I have been looking all over on how to upload a file to a directory in Rails I found this Upload Files but I don't really understand it and I can't get it to work.
This is my View:
<%= form_for :upload, :html => {:multipart => true} do |f| %>
<%= f.file_field :my_file %>
<%= f.submit "Upload" %>
<% end %>
This is my Controller:
def upload
path = File.join("public/folder", upload["datafile"].original_filename)
File.open(path, "wb") { |f| f.write(upload["datafile"].read) }
end
I have also tried the Upload file section of Rails Guides
It says Stack level too deep, can somebody please help and try and explain this to me as simply as possible?
Thanks
uploading in ROR similar
name = upload['datafile'].original_filename
directory = "public/data"
# create the file path
path = File.join(directory, name)
upload_file = File.new(upload['datafile'], "rb").read
# write the file
File.open(path, "wb") {|f| f.write(upload_file) };
use this it may be help you...........

Problem using 'responds_to_parent' with an iframe and AJAX to upload a file

I followed instructions from the "AJAX file uploads in Rails using attachment_fu and responds_to_parent" article. In my case I am using Ruby on Rails 3 and Paperclip.
What I made is the following:
I have installed the 'respons_to_parent' plugin running this command in the Terminal:
rails plugin install git://github.com/itkin/respond_to_parent.git
After installing, I restart Apache.
In my view I have:
<%= form_for(#user, :html => { :multipart => true, :target => 'upload_frame' }) do |f| %>
<%= f.file_field :avatar %>
<%= f.submit "Submit" %>
<% end %>
<div id="test">Here is a test</div>
<div id="stuff">Here is some stuff</div>
<iframe id='upload_frame' name="upload_frame" style="width:1px;height:1px;border:0px" ></iframe>
In my controller I have
def action
respond_to do |format|
...
format.js {
responds_to_parent do
render :update do |page|
page.replace_html :test, "This is the resulting test"
page << "alert($('stuff').innerHTML)"
end
end
end
end
end
Trying to submit the form, all about the file uploading works (Paperclip handle correclty files, ...) and in the log file there aren't errors.
The only thing that isn't working is the AJAX part. In the example
page.replace_html :test, "This is the resulting test"
page << "alert($('stuff').innerHTML)"
don't update the page (BTW: where these should have effect? in the "main view" or in the "iframe view"?). It doesn't work also if I try to delete the 'respons_to_parent' statement or if I put them in the 'action.js.rjs' file.
Where I am wrong?
SOLVED
I had forgotten to add :url => users_account_path +. "js". So the view become:
<%= form_for(#user, :url => users_account_path +. "js", :html => { :multipart => true, :target => 'upload_frame' }) do |f| %>

Resources