How to retrive full path of browse file in rails3 - ruby-on-rails

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.

Related

Rails CSV file upload problems

I'm trying to upload a CSV file(without headers) to a form in Rails, and then change my database based on that file's contents.
Now, the problem I'm having is that I know my routes must be correct, because I'm able to get to the correct method...but, my #imported variable is saying it is nil even when I upload a file.
Is there any help I can get for this? This will be my first file upload in rails, so we can cross this milestone together...
Here is the form in my view, mapped to the #ship action:
<%= form_for #import, :url => {:action => "ship", :controller => "imports"} do |f| %>
<%= f.file_field :import %>
<%= f.submit %>
<% end %>
And here is the corresponding method in my controller:
def ship
#import = CSV.read(params[:file])
#import.each do |i|
Product.ship(#import[i][0]) #I believe the #read method imports
end #CSV files as an array of arrays
redirect_to "/" #But I have yet to get past the
end #first line, so I'm unsure
Sergio Tulentsev is right. You need to use params[:import][:import]. The first import being the name of your resource (comes from form_for #import, the second import being the field name (coming from f.file_field :import).
More info at http://guides.rubyonrails.org/form_helpers.html#uploading-files

Upload file from the specific directory (authomatically) [ruby on rails]

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 ':'

Post XML file content from client browser to server

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.

Rails 3.0.1 set default file on a file_field

I have two rails applications which are connected through an API. In the first rails app I have many users with avatar images. Now I want to send them through a file_field (in form_for) to the second rails app.
The problem is that I don't want manualy select the image-path for each user. Better would be if the image_path would be automaticaly inserted when the page is loaded. At the moment there is for each user a page where I select his avatar image and click on send.
The location of the images is on the first rails server. So this means that I want to send avatar images from one rails server to another with a file_field.
Is that somehow possible?
If you are trying to set a default image when opening a record for editing you have to do the following
In your edit view:
<%= form_for(#photo, :html => { :multipart => true }) do |f| %>
<%= f.text_field :name %>
<%= f.file_field :image %>
<%= f.submit "Change" %>
<% end %>
In your controller:
#photo = Photo.find(params[:id])
When you form gets displayed it will automatically assign the image in the database to the file_field.
Although the form might show "No File Selected", When you update your form your original image will still be there.

Create an upload controller action for Rails app

I'm making a Rails app, and want to add an upload feature with allows users to upload multiple entries at once through an Excel spreadsheet as opposed to entering one entry at a time.
Ideally, I was hoping to add a separate Upload/Submit portion to the bottom of the new.html.erb file (the bold portion being the Upload HTML.erb):
...
<div class="actions">
<%= f.submit %>
</div>
<% end %>
**<div class="field">
Or Upload Multiple Entries <br />
<%= form_tag({:action => :upload}, :multipart => true) do %>
<%= file_field_tag 'multi_doc' %>
<% end %>
</div>**
Here is my routes.rb file:
Dataway::Application.routes.draw do
devise_for :users
resources :revenue_models do
get 'upload', :on => :collection
end
root :to => "home#index"
end
And my revenue_models_controller (haven't developed the action at all yet, now just a redirect):
...
def upload
redirect_to revenue_models_path
end
I have followed the rails guides for Uploading files as well as for Routing files and I keep getting an error when I attempt to open the /new view I have modified:
Routing Error
No route matches {:action=>"upload", :controller=>"revenue_models"}
Try running rake routes
When I run rake route, I get an entry for the upload action:
upload_revenue_models GET /revenue_models/upload(.:format) revenue_models#upload
In the end, what I would like to do is upload an excel file with multiple entries, parse it, and conditionally add each data row to my database, which I was under the impression I could specify in the upload action. Please help!
Please use form_for instead form_tag.
Second What is multi_doc is a field/attribute of revenue_model?. I guess is a field/attribute of revenue_model.
Third, Where is your instance variable revenue_model in the form?
Fourth, You have to create first a instance variable of your Model, on your action/controller where you are showing the form, for example #revenue_model = RevenueModel.new.
After try this code:
<%= form_for(#revenue_model, :method => :get, :html => {:multipart => true}, :url => { :controller => "revenue_models", :action => "upload"})) do |f| %>
<%= f.file_field :multi_doc %>
<%= f.submit :id => "any_id" %>
<% end %>
The question is very confusing, trying to clarify your question specifying the name of your actions and the names of the files in your views.
Regards!

Resources