I am learning Rails 3 and need some help on Ajax part.
I am trying to accomplish the following:
There is a textarea on the page which has a length of letters
constraint, say 100. Every time a symbol is typed there,a nearby label
tells you how many letters you have left.
I plan to use Ajax to do this.
I googled for a few hours and could not figure out how to do this.
So far what I have done is the follows:
Step 1. Add :remote => true to the form_for method. This form_for method
lives in a partial which is called in app/views/foos/home.html.erb
Step 2. Add
respond_to do |format|
format.html
format.js
end
to FoosController’s corresponding Home action.
Step 3.create home.js.erb under app/views/foos/
But I do not know how to add something so when a letter is typed in a
textarea inside the form generated by form_for method that home.js.erb
will process.
I am learning Rails 3 and need some help on Ajax part.
I am trying to accomplish the following:
There is a textarea on the page which has a length of letters
constraint, say 100. Every time a symbol is typed there,a nearby label
tells you how many letters you have left.
I plan to use Ajax to do this.
If you do that then every time a letter is typed it will have to send
a request to the server, which is a bad idea. Use javascript instead.
I am learning Rails 3 and need some help on Ajax part.
I am trying to accomplish the following:
There is a textarea on the page which has a length of letters
constraint, say 100. Every time a symbol is typed there,a nearby label
tells you how many letters you have left.
I plan to use Ajax to do this.
If you do that then every time a letter is typed it will have to send
a request to the server, which is a bad idea. Use javascript instead.
I am learning Rails 3 and need some help on Ajax part.
I am trying to accomplish the following:
There is a textarea on the page which has a length of letters
constraint, say 100. Every time a symbol is typed there,a nearby label
tells you how many letters you have left.
I plan to use Ajax to do this.
I googled for a few hours and could not figure out how to do this.
So far what I have done is the follows:
Step 1. Add :remote => true to the form_for method. This form_for method
lives in a partial which is called in app/views/foos/home.html.erb
Step 2. Add
respond_to do |format|
format.html
format.js
end
to FoosController’s corresponding Home action.
Step 3.create home.js.erb under app/views/foos/
But I do not know how to add something so when a letter is typed in a
textarea inside the form generated by form_for method that home.js.erb
will process.
Could someone help me out here?
hi Jay
here is a link to a type of your example i noticed at a website that
allows users to post classified ads with a word length of 25 characters
and adjusts the character count as you type your advert text
description.
the site is a free classified ads paper http://www.startrader.com.au/
But I do not know how to add something so when a letter is typed in a
textarea inside the form generated by form_for method that home.js.erb
will process.
Could someone help me out here?
Check out JQuery for this. I don’t have any specific examples, but
you need to have javascript react to onclick. I think if you use the
form_for submit tag, you’ll need to submit the form to get the results
you want.
However, I believe you only want to query for characters during typing
and do something different when you submit the form.
Thanks to Colin L., Timothy G., Stephan W., Greg A., Gautam P…
Here is what I got.
Ajax is a bad idea for this. It will waste bandwidth and more
importantly makes the character counting unreliable. You do not know
when the response will be.
Javascript is supposed to be the tool to handle this type of tasks.
Both jquery and prototype does the work. I did not know which event to
respond to and how to attach event handlers to the element. Now I know.
a) Default place for the javascript code. “application.js”. No other
changes is needed. However, if you would like to load code like this
dynamically into your application.js, you are welcome to do so.
b). For jquery, the code could be the following:
$(document).ready(function () {
$(’#id_of_text_area’).keyup(function(){
var s = 100 - $(this).val().length;
$(’#id_of_message’).text(“You have " + s + " characters left”);
});
});
The key thing which frustrated me was the $(document).ready. That makes
sure the code is executed at the time when the page is ready, thus
establishing keyup event handler for .keyup event of the element of
concern, i.e. the one whose input needs to be counted. The same is true
for any event handler if it needs to be established by the time the page
is ready.
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.