Reputation: 37
Okay I have an extensive ruby application running behind Ruby on Rails right now. This application needs form input but I don't want to refresh the page when I update the text box. What I mean is:
I have a form that has two text boxes, one is called "starttime" and one is called "endtime". I somehow need to get the value of these text boxes (what a user has typed in) into ruby when a button is pressed. Is there some sort of equivalent to javascript's document.form.starttime.value command but for ruby? Or is there some way to submit the form data, retrieve it, but not refresh the page such as using ajax? I know ajax is used to manipulate the DOM dynamically without refreshing the page, but I really just need to retrieve data. Any help would be greatly appreciated, I simply cannot find this anywhere.
Upvotes: 0
Views: 496
Reputation: 4277
You can do this quite easily with jQuery. Give the start time and end time form elements a distinct ID (I use startDate
and endDate
below) and you can easily retrieve them, then send the data to a Rails action that can process them in the background and if necessary, update the DOM with any relevant data when you click on a button (below assumes an input button with the ID SendDateValues
).
Something like this: (Not tested)
<script type="text/javascript">
//<![CDATA[
$("#SendDateValues").click(function() {
$.ajax({
url: '/SomeController/ProcessDateData/",
data: 'startDate=' + $("#startdate").val() + "&endDate=" + $("#endDate").val(),
success: function(html) { //DO SOMETHING WITH THE RESULT },
type: 'post'
});
});
//]]>$
</script>
Play with the results... Use Firebug or something similar to check what gets passed. Check your Rails event log to see what data your action gets.
Upvotes: 1