Question

What I need- some kind of way to grab the number entered into the form in order to check it against previous records PRIOR to updating, so that if a validation error occurs, the user can be prompted to confirm before the form is submitted. Params would work, but are only returned after the form is posted- so no help. Is there an ajax call that I can pass into a ruby variable? Or perhaps some kind of ruby code that will read the input in the text box without submitting or linking?

What I'm doing- I'm trying to set up a 'manual validation' because I don't want the validation to 'prevent' from saving. Instead, it should be more like a confirmation.

If you care for context, Here's the run-down- I have a client that pays monthly deposits. We confirm these deposits over the phone through a third party. Naturally, in order to get the most accurate data as possible, we have to account for human error and other factors. A deposit this month should never be less than a deposit last month- but deposits can be "moved" from one account to another, which would make it seem like it was less. I have a form that new data is input on, and I want it to check against previous records to see if the deposit is more or less than reported previously. If less, it should ask for confirmation- an "are you sure?" kind of thing.

The code is old & outdated, should be changed from the ground up, but would take months when I have days to do this. I'm just looking for a patch.

What I have so far- note that cur_deposit is this months and rec_deposit is last months.

<%
arr1 = []
arr2 = []
is_less = false

r = @recent_inquiries.last
r.inquiry_deposits.order(:id).each do |t|
  arr1 << t.cur_deposit.to_f
  arr1 << t.rec_deposit.to_f
end
@inquiry.inquiry_deposits.order(:id).each do |td|
    #============THIS is the part that needs help
    arr2 << params["cur_deposit_text_box"]
    arr2 << params["rec_deposit_text_box"]
end
i = 0
while i < (arr1.size - 1)
  comp_arr1 = []
  comp_arr2 = []
  comp_arr1 << arr1[i]
  comp_arr1 << arr1[i + 1]
  comp_arr2 << arr2[i]
  comp_arr2 << arr2[i + 1]
  if Inquiry.compare_deposits(comp_arr1, comp_arr2) != nil then is_less = true end
  i = i + 2
end

if is_less
  strConf = "A deposit from last month is greater than the same deposit this month, which should not happen.  Are you sure?"
end
%>
<%= submit_tag "Save Inquiry", :onclick=>"$('#submit_form').val('Save Inquiry summary');", :class => 'tgButton3', :id => 'save_inquiry_button_bottom', :confirm => strConf %>

When I get this code working, I will stash all the functioning code into a model- I just have it in the view for testing. It is safe to assume that all the 'custom methods' this script calls to are functioning. If you need code from them, I'll happily share it.

Rails version 3.0.20

Was it helpful?

Solution 2

In order to close this question out, and in case anybody else is wondering, I will answer my own question. This is what I've found out.

Because of the nature of the relationship between client & server, there is really no way to get the value of the text input, store it in a ruby variable, and check it against another ruby variable. Ruby script only runs once and then is rendered, so while ajax may be able to continually run in the background and gather inputs, etc, the integration with ruby falls short when talking client-side only interaction. (Correct me if I'm wrong- after all, I posted the question to get everybody's input!)

The fix: I created a switch using hidden tags. When the form loads, the hidden tag is blank. After submitting the form, the update action checks the params of the newly entered data against the numbers from last month. If the conditions check out, it saves. If not, it re-loads the page with a message. If the message is confirmed, an ajax command changes the hidden tag to "true" which bypasses the comparison once it hits the update action again. Otherwise, the data is not saved. Problem solved!

I'm making this a community wiki answer in case anybody would like to add their two cents.

OTHER TIPS

Can you use jQuery on your website? (if not it is doable in plain javascript)

$('#id-of-your-field').change(function(e){
 //do here your client side logic if any needed
 var yourfirstvalue = $(e.target).val();
 //now take the value and send it to server (your ruby stuff)
 $.ajax({
   url: yourURL + "/" + yourfirstvalue,
   success: function(data){
      //this data can be sent as JSON in structure which suits the best to you
      //so you can use it to populate your second dropdown
      var values = JSON.parse(data);
      //use your values
   }
 });
});

Google "combo box example" it might help you.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top