# JavaScript code call method in controller?

**URL:** <https://www.ruby-forum.com/t/javascript-code-call-method-in-controller/136977>\
**Category:** Rails\
**Created:** [May 7, 2008, 9:52pm UTC](https://www.ruby-forum.com/t/javascript-code-call-method-in-controller/136977 "2008-05-07T21:52:58Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![snantz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c2a13f/32.png) [@snantz](https://www.ruby-forum.com/u/snantz)\
**Post date:** [May 7, 2008, 9:52pm UTC](https://www.ruby-forum.com/t/javascript-code-call-method-in-controller/136977/1 "2008-05-07T21:52:58Z")

</div>

I have this text field that once a certain number of character are  
entered I would like it to call a method in the controller and save the  
data. To make a long story short, I do this because I don’t want the  
user to hit a button to submit, but want this to be a quick edit option  
for entering a telephone number.

What I am doing is to check the number of character in my javascript, if  
the length equals the max I would like my method call to happen. Is  
there a way to call a controller method from a javascript method?  
Thanks,

-S

---

<div class="post-metadata">

**Author:** ![snantz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c2a13f/32.png) [@snantz](https://www.ruby-forum.com/u/snantz)\
**Post date:** [May 7, 2008, 10:27pm UTC](https://www.ruby-forum.com/t/javascript-code-call-method-in-controller/136977/2 "2008-05-07T22:27:20Z")

</div>

Shandy N. wrote:

> I have this text field that once a certain number of character are  
> entered I would like it to call a method in the controller and save the  
> data. To make a long story short, I do this because I don’t want the  
> user to hit a button to submit, but want this to be a quick edit option  
> for entering a telephone number.
> 
> What I am doing is to check the number of character in my javascript, if  
> the length equals the max I would like my method call to happen. Is  
> there a way to call a controller method from a javascript method?  
> Thanks,
> 
> -S

I just did something like this today. In my case, when a certain event  
happens, I fire off an AJAX call to update a session variable. Here’s  
how I did it:

In my event code, I included a call to this function

function sidebar\_menu\_store\_state(menu\_id, expanded)  
{  
new Ajax.Request(’/person/store\_sidebar\_menu\_state’, {  
method: ‘post’,  
parameters: {  
menu\_id: menu\_id,  
expanded: expanded  
}  
});  
}

then in my controller, I have

def store\_sidebar\_menu\_state  
return unless request.xhr?  
return unless request.post?

[do the logic]

render :nothing =\> true  
end # store\_sidebar\_menu\_state

There is probably a much better way to do it, but this works well for  
what I need it to do. So if you already have the character count  
happening, all you’d have to do is tack on the logic to hit the server  
once the max is encountered.

I might add, though, that you could accomplish the same thing in an  
onblur. When the user finishes typing in the text and focus leaves the  
field, update the database.

Peace,  
Phillip

---

<div class="post-metadata">

**Author:** ![snantz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c2a13f/32.png) [@snantz](https://www.ruby-forum.com/u/snantz)\
**Post date:** [May 7, 2008, 11:44pm UTC](https://www.ruby-forum.com/t/javascript-code-call-method-in-controller/136977/3 "2008-05-07T23:44:53Z")

</div>

On Wed, May 7, 2008 at 1:27 PM, Phillip K.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I might add, though, that you could accomplish the same thing in an  
> onblur. When the user finishes typing in the text and focus leaves the  
> field, update the database.

…which is probably better, or at least more universal, since a “phone  
number” can be an arbitrary number of digits. 🙂

–  
Hassan S. ------------------------ removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![snantz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c2a13f/32.png) [@snantz](https://www.ruby-forum.com/u/snantz)\
**Post date:** [May 7, 2008, 11:49pm UTC](https://www.ruby-forum.com/t/javascript-code-call-method-in-controller/136977/4 "2008-05-07T23:49:45Z")

</div>

Phillip K. wrote:

> I might add, though, that you could accomplish the same thing in an  
> onblur. When the user finishes typing in the text and focus leaves the  
> field, update the database.
> 
> Peace,  
> Phillip

This is what I ended up doing and then submitting the data with a  
remote\_function call. Thanks,

-S
