views:

1255

answers:

1

I want to create a form for making a reservation for borrowed items. A reservation consists of pick up time, return time and items the reserver wants to borrow and their amounts. For example, I might want to reserve three plates, three knives and three forks for the rest of the week.

In the form, I want to do an AJAX validation that checks whether there is enough items available. Next to each item I have a text box with which the reserver inputs the item amount. After the input, I want to do an onchange call that checks if the amount of items is available for the given dates. Thus, I need to pass the remote function called in the onchange following parameters: item id, item amount (value of the current textfield) and pick up time and return time which are both given in datetime_select fields above. This is my code:

<% with = "'amount='+value+'&item=#{item.id.to_s}&pick_up_time=#{@reservation.pick_up_time.to_s}&return_time=#{@reservation.return_time.to_s}'" %>
<%= text_field_tag "reservation[#{prefix}reserved_items][#{item.id}]", get_amount_value(item, @reservation), :size => 3, :onchange => "#{remote_function(:url => { :controller => :items, :action => :availability }, :with => with, :update => availability_url) }" %>

Obviously, this does not work since @reservation.return_time and @reservation.pick_up_time are not yet set because the form is not yet sent. My question is: how do I get those values? I believe that it should be done via a javascript call, but I didn't manage to insert a javascript call in the "with" variable or at least didn't get it to work. Does anybody have any idea on what I should do?

+1  A: 

use prototype selectors $(#reservations_pick_up_time).value

the :with attribute is just a place for you to write JS code that it will display inline

ErsatzRyan
I tried it, but there is a weird error: when I try to ask for the value of the one of the pick_up_time fields (it's a rails datetime_select field that consists of five dropdowns) in javascript, it responds: "TypeError: $("conversation_pick_up_time_3i").value.toS is not a function". I can get values from any other field and I can also get for example the type of that specific field too, but for some reason it does not give the value. Any idea on this?
Kusti
you don't need the .toS should just be able to do $("conversation_pick_up_time_2i").value + "/" + $("conversation_pick_up_time_3i").value + "/" + $("conversation_pick_up_time_1i").value gives me MM/DD/YYYY then you can just keep going to do the time
ErsatzRyan
I wasn't trying to do the .toS call, it just kept saying that... but it turned out that the error was caused by some mystical variable naming things, more info in my other question related to the subject:http://stackoverflow.com/questions/1136688/how-to-get-the-value-of-a-datetimeselect-field-with-javascript-in-ruby-on-railsAnyway, thanks for help, your answer gave me the courage to believe that I was doing the right thing :)
Kusti