rails: enforce validation check with form with multiple submit buttons - ruby-on-rails

I have a form with multiple submit buttons that trigger different functionality in controller action. One submit button allows user to save csv with format. Another submit button allows user to load csv file with specified format from dropdown. Then a final submit button allows user to save actual formatted records to database. In order to save records, the user must either save csv format first or load a csv format. How can I enforce this validation check? How can I determine one of the other two submit buttons were triggered before the create call?
The problem with using session variable will be if I add a boolean in session after one button submitted, then when they import another csv file, that value still will be in session and give false positive.

Related

Ruby on rails AngularJS - create multiple models from direct upload

I recently am given a task by upper management to implement a mass upload coupon task. The goal is to allow users to upload an list of coupon codes (excel or csv), and subsequently giving these coupons codes attributes such as start date, expiry, quantity and so on.
I have already implemented a form to input these attributes. The current implementation method I am doing is as follows:
1.I upload a list of coupon codes (eg: 333245454dfee) and I do not write them directly to the database, instead I converted them to string and display on a page for the user to view. (done)
2.from that view page, there is a form with all the attributes. The user can then input these attributes. (done)
3.The user can create all of these coupons codes with the attributes set.
However, I am stuck now because I am unsure of how to mass create multiple codes and attach all the attributes to them. Right now, I can only create one coupon at a time.
So, to summarize, I would like to ask is it possible to
have a field that contains all the codes I have uploaded
I have others fields for different attributes
how to create all the codes I have uploaded as separate models.
I do not need the codes, I would like to hear what approach there are. I am thinking of creating a variable to store these coupons codes first, then loop them. But I have no idea how to do all of that by pressing one single button.
Thanks in advance.
Hmm, how I would approach this problem would be to have the user upload an csv file with 1 column and multiple rows of coupon. Using carrierwave to upload the file and parse the csv file. After parsing it, you should have an array of coupon and you can just input them into the database.
A good reference for your problem would be http://railscasts.com/episodes/396-importing-csv-and-excel

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.

Select or input

In my app I have a form for creating new trip. Model Trip has field finish_address. For setup the finish_address in I use select in simple form to select address from user's addressbook. But I want to make a better form: if there isn't necessary address, user can add it using input field.
So I need to make form with to types of setup finish_address. How can I make it?
You have 2 basic options.
You can use autocomplete on a text input, populating a dynamic crop-down with know values.
There are several gems available to get you started in this direction, like https://github.com/crowdint/rails3-jquery-autocomplete.
Alternatively, you can add an "other" option to a select input, populated with your known values. When the "other" item is selected, display a previously hidden text input with the same name below the select input. The "lowest" element will take precedence, for what gets sent to the controller.
In the controller, just do a find_or_create_by, using your provided value.
These options both require javascript, but you can eliminate the need for javascript if you make your select a non-db-backed attribute, and manipulate your params accordingly, as they come in to the model. This might help with validations, as well.

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.

Confirm multiply data before save

In my app I sometimes need to import some CSV data and I wanna get a view that is shown before save imported data to provide user possibility to check that data is correct. How to achieve it in Rails?
in the first form field, have a hidden field name "confirm" with value 0.
In the rails controller, if the confirm is 0 you just populate with the csv data, show the form again with confirm = 1
if the confirm is 1, then it means the csv was seen by the user, so you can save it.
bonus points-
Instead of showing the csv, make a textfield out of each csv cell- the user will be able to update the fields, not just check if the data is correct.

Resources