Title: How do I access form element values using ruby without submitting the form?
Tags: javascript;ruby-on-rails;ruby
Question: 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.
Comment: Okay is there a way to do this using session variables?

Here is the accepted answer: 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"&gt;
//<![CDATA[
$("#SendDateValues").click(function() {
$.ajax({
url: '/SomeController/ProcessDateData/",
data: 'startDate=' + $("#startdate").val() + "&amp;endDate=" + $("#endDate").val(),
success: function(html) { //DO SOMETHING WITH THE RESULT },
type: 'post'
//]]&gt;$
</script&gt;
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.
Comment for this answer: Thanks for the quick response. If I use something like this, won't it move the browser to another page? I need to stay on the same page and manipulate another object after checking the values of the text boxes.
Comment for this answer: Okay I added:
to my layout. Of course I'll need to modify the code you posted but where exactly does this code go?
Comment for this answer: Oh I guess I don't need jrails, it's just something I found online.
Comment for this answer: That's the beauty of the AJAX call -- it will make the request in the background. The user may see activity in their status bar, but nothing will change on the page they're on unless you explicitly change it (somewhere in the success function).
Comment for this answer: If you've not used jQuery before, be aware that you'll need to include the jQuery libraries for the above to work. In theory you could write the AJAX call in ordinary Javascript, but the jQuery library abstracts this beautifully and makes it simple.
Comment for this answer: Just create a new  tag on the page that this applies to (put it at the bottom). I'll update my answer.


{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 1, 'ContentLicense': 'CC BY-SA 2.5', 'CreationDate': '2010:00:07/27/10', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<javascript><ruby-on-rails><ruby>', 'Title': 'How do I access form element values using ruby without submitting the form?', 'ViewCount': 413, 'id': 3348763, 'user_AboutMe': '', 'user_AccountId': 53633.0, 'user_DisplayName': 'Daryl', 'user_Reputation': 37.0, 'user_Views': 13.0, 'user_id': 160364.0}}