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?

--
Posted via http://www.ruby-forum.com/.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

Search Discussions

  • Colin Law at Dec 17, 2011 at 8:27 pm

    On 17 December 2011 20:06, Jay Chaun wrote:
    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.

    Colin

    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
  • Jay Chaun at Dec 17, 2011 at 8:43 pm
    Javascript should be a better idea here. I am new to Rails and do not
    know how to write javascript inside rails 3. More specifically, two
    questions:

    1. which event should I respond to? keypress, keydown,keyup?
    2. Rails 3 uses Unobtrusive Javascript. All I can think of now is old
    obtrusive one. How can I use unobtrusive one to do this.

    Any help with pointing to the right direction would be greatly
    appreciated.

    Colin Law wrote in post #1037179:
    On 17 December 2011 20:06, Jay Chaun wrote:
    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.

    Colin
    --
    Posted via http://www.ruby-forum.com/.

    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
  • Stephan Wehner at Dec 18, 2011 at 6:04 pm

    On Dec 17, 12:06 pm, Jay Chaun wrote:
    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:
    I think there are jquery / prototype - plugins available; did you
    check?

    Stephan

    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
  • Timothy G. at Dec 21, 2011 at 1:01 pm

    Jay Chaun wrote in post #1037174:
    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/

    --
    Posted via http://www.ruby-forum.com/.

    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
  • Greg Akins at Dec 21, 2011 at 2:47 pm
    Jay Chaun wrote in post #1037174:
    /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?
    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.

    --
    Greg Akins
    http://twitter.com/akinsgre

    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
  • Gomzi Pai at Dec 22, 2011 at 5:47 pm
    try this link, is this what you are looking for?

    http://cssglobe.com/lab/charcount/01.html

    You will find complete implementation details and link to download here

    http://cssglobe.com/post/7161/jquery-plugin-simplest-twitterlike-dynamic-character-count-for-textareas


    Gautam Pai
    On Wed, Dec 21, 2011 at 8:16 PM, Greg Akins wrote:

    Jay Chaun wrote in post #1037174:
    /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?
    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.

    --
    Greg Akins
    http://twitter.com/akinsgre

    --
    You received this message because you are subscribed to the Google Groups
    "Ruby on Rails: Talk" group.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To unsubscribe from this group, send email to
    rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at
    http://groups.google.com/group/rubyonrails-talk?hl=en.
    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
  • Jay Chaun at Dec 24, 2011 at 12:59 am
    Thanks to Colin Law, Timothy G., Stephan Wehner, Greg Akins, Gautam Pai.
    Here is what I got.

    1. 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.

    2. 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.

    --
    Posted via http://www.ruby-forum.com/.

    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouprubyonrails-talk @
categoriesrubyonrails
postedDec 17, '11 at 8:06p
activeDec 24, '11 at 12:59a
posts8
users5
websiterubyonrails.org
irc#RubyOnRails

People

Translate

site design / logo © 2022 Grokbase