How to send file from action to next action - ruby-on-rails

I'm new in RoR.
I want to make that logic:
User upload xml or csv file from /home (static_pages#home) and send it to elms#el_select
If file is xml and file contain more than one element I want to show page to user so him choose which element he want to show
If file is csv or xml with one element I want to show result page
So here is two use-cases:
- choose file - view results
- choose file - choose element - view results
In second case I upload file in static_pages#home, send it to elms#el_select and I don't know how to send it from elms#el_select to result page.
In first case I don't know how to skip page rendering and send file to another controller

Controllers do not store files for up/download or any other data. They just manipulating with instances of their models (with their behaviour). So, if I have understand your question in the right way, the order may be next:
StatiPagesController take the file from user and store it somewhere (/tmp or smth.) if the file is valid;
ElmsController take the file from storage and render the proper view (or redirect_to proper view if such already exist).

Finally, I've found answer on my question:
#key = SecureRandom.uuid
Rails.cache.write(#key, #file)

Related

Excel to pdf conversion in rails4 with libreoffice

In a rails 4 application, how can I convert an excel spreadsheet file into pdf.
When I am trying to implement Excel to pdf the contents are listed in different pages if excel column size is large.
How to generate the pdf without moving data to next pages in pdf.
Please help,
Thanks
So you basically have two options you can either implement this yourself and use a CSV gem/library (default CSV, faster CSV, or smarter CSV) assuming that by "excel" a CSV is acceptable. If a CSV is not acceptible you can use the axlsx gem instead. Then for pdf conversion you can use something like prawn. If you decide to build this yourself follow these steps.
Create a controller that will handle Reports, I suggest using the rails g controller report upload generate_table show generate_pdf generator to create a controller and a view for the upload process
Create a file upload form in the upload view.
On submit you will send the file to the generate action processing with one of the CSV or excel gems
Once processed your end product should be an array or hash (as an instance variable) and you can send that to the show action
In the show view you will iterate of that hash/array and incapsulate the contents in a html table.
On the show view you should have a button that will send that same hash/array to the generate_pdf controller action where you will use prawn to create a pdf, you can use something like send_data to the send the completed pdf file back to the user.
This is roughly how you could go about it less the low level details. Now if you wanted to use an out of the box solution you could use something like Ruport. Ruport will handle most of the heavy lifting for you the only thing is you need to have your models and associations set up to use it the way it is designed, and that may not be an option for you.

web2py pass parameters to controler and download a file

I have the following problem
On my page user can select date from and date to. This dates are to be send to controller on button click, which creates an excel file and user receives that file?
Any suggestions ??
Thank you
def excel_file():
form = SQLFORM.factory(Field('start_date', 'date'),
Field('end_date', 'date'))
if form.process(session=None).accepted:
excel_file = create_excel_file(form.vars.start_date, form.vars.end_date)
return response.stream(excel_file, filename='name_of_file.xlsx',
attachment=True)
return dict(form=form)
Note, the above sets session=None in the call to .process() in order to disable use of the _formkey hidden field (otherwise, you would only be able to submit the form once and would have to manually reload the page for a second submission). This means there is no CSRF protection, but that shouldn't be a problem here, as the form submission is just being used to request data rather than make any changes. If you need CSRF protection, you will have to implement it manually.
Also, note that excel_file can be a file-like object (such as StringIO), an open file object, or a string representing a full file path.
Alternatively, in the browser, you could add an event handler via Javascript to capture the button click and instead of allowing the form to post, call window.open() with a web2py URL that will create and serve the file (you would have to pass the values of the start and end dates from the form via the query string of the URL). You could optionally blank out the form fields after the submission.

mvc file upload and database insert

I'm just getting my head wrapped around MVC in .net using VS 2013.
I need a little direction in regards to uploading a file (which is easy) but also inserting data about that image into a database. Specifically I want to allow the end user to enter a description, title etc. about the file being uploaded. On the back-end I want to also add to the meta data a 'Date Created', 'Path to the file', 'Category', and the File Name and a couple other pieces of data that will help with presenting files in the views. I don't want to insert the files in the DB but just use the path in the generated HTML to point to the physical file so the end user can view or download it.
Multiple file types are being used, Audio, Video, Documents, Images.
I can get the file upload to work but writing the controller to accept a file, and end user input, then also add the other fields I need into the database that the user never sees is where I'm stuck. Blending the file upload with the user fields and beack end data is confusing me on how to get all the pieces to work together.
So in short getting File Upload + User Input + non-User Input values all in the same View, Controller, and Model is what I need direction on.
You have to upload your image plus data in a multi-part form.
In your view you will create a POST form that uses "multipart/form-data" encoding, you can then include inputs for model data and your file upload control within the body of the form. When it submits will will create a multi-part form, one part will contain the binary file and another part will contain your data.
On the controller action side you will receive the data with an action akin to
public ActionResult PostFile(MyModel model, HttpPostedFileBase file) {...}
There are numerous posts on SO with more details so I won't go into that.

Add field for each file in fine-uploader

Trying to implement Fine-Uploader for first time.
There's a way I can add a text-field for each file pending uploading?
I want the user to comment the content of each file and than save these informations.
Thanks for explanations.
Dario.
If you want to add an input text field next to each file represented in the UI (I'm assuming you are using FineUploader mode), you can do the following:
Set the autoUpload option to false. This seems obvious, but I thought I should list it anyway. If you don't set this to false, files will be uploaded immediately after the user selects them. I'm guessing this would not fit into your workflow.
Define an onSubmitted callback handler. In this handler, you can use the id parameter to get a handler on the associated getItemByFileId API method.
Once you have the element that represents the file in the UI, you can add an input field next to it. Perhaps you should add a data attribute or a css class that you can later use to associate this field with the file.
I assume you intend to create a button that a user will click when they are ready to start uploading all selected files, presumably after they have filled out any text fields associated with these files. So, you can add a click handler to this button that calls the uploadStoredFiles API method.
You will also need to contribute an onUpload callback handler. When your handler is invoked (once for each file, before it is uploaded), grab the value from the associated text field and then use the setParams API method to send this value with the upload (POST) request for this file as a parameter. Don't forget to include the file ID as the last parameter when you call setParams.

how to get html tags of page without URL

I want to save the HTML tags of whole page that is going to (render) client side in the database table with a unique id so next time I can fetch that page directly from database with id and without any complicate logic execution. All this without any URL means I don't want to pass a URL to any method that will get the HTML tag. I want to get HTML tags before even rendering the page because I have some status system once status changed that URL will not work.
Sorry if my question is not clear, but I tried to mention all details. I simply want HTML tag of my view before sending it to client so that I can save those tags with a unique id in database table and next time I can just fetch those tags from the database table with that unique id. My need is that the view contains many complex queries that I don't want to run each time.
It's a kind of certificate in that once it is printed, it cannot be changed, it can only be viewed by that unique ID in a faster way.
What you want to do is called output caching, use that instead.
If you need to save the HTML output, you need to add an action filter and intercept the Result and save the text in database.

Resources