# Rails/JQuery Collection Select

**URL:** <https://www.ruby-forum.com/t/rails-jquery-collection-select/186548>\
**Category:** Rails\
**Created:** [April 8, 2010, 5:46pm UTC](https://www.ruby-forum.com/t/rails-jquery-collection-select/186548 "2010-04-08T17:46:52Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [April 8, 2010, 5:46pm UTC](https://www.ruby-forum.com/t/rails-jquery-collection-select/186548/1 "2010-04-08T17:46:52Z")

</div>

I may be asking a very dumb question, but I thought it’d be worth the  
asking…

I’m using JQuery to dynamically create some form fields for entering  
phone  
numbers… Basically just two fields… One for the number, and a select  
field so that the user can choose whether it’s a Home/Work/etc. number.

My Phone Number Categories (Home/Work/etc.) are stored in the  
database… Is  
there any way for JQuery to access the @phone\_number\_categories Rails  
variable from within the JavaScript code to create the select?

–

* * *

Joshua S. Martin

CONFIDENTIALITY NOTE: This e-mail message, including any attachment(s),  
contains information that may be confidential, protected by the attorney  
client or other legal privileges, and or proprietary non public  
information.  
If you are not an intended recipient of this message or an authorized  
assistant to an intended recipient, please notify the sender by replying  
to  
this message and then delete it from your system. Use, dissemination,  
distribution, or reproduction of this message and or any of its  
attachments  
(if any) by unintended recipients is not authorized and may be unlawful.

---

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [April 8, 2010, 6:19pm UTC](https://www.ruby-forum.com/t/rails-jquery-collection-select/186548/2 "2010-04-08T18:19:05Z")

</div>

Joshua,

You can’t access that variable directly from jQuery because jQuery is  
Javascript running in the browser and @phone\_number\_categories is Ruby  
running on the server. If you want to do all this on the client side  
then  
you can save the categories into a Javascript variable that is in scope  
with  
the jQuery code. You can store them in an array or JSON object or an  
array  
of JSON objects or what ever makes the most sense.

Anthony C.

---

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [April 8, 2010, 6:19pm UTC](https://www.ruby-forum.com/t/rails-jquery-collection-select/186548/3 "2010-04-08T18:19:40Z")

</div>

Joshua,

BTW…I forgot to mention there is a to\_json method you can probably use  
to  
convert the @phone\_number\_categories collection to a Javascript value.

Anthony C.

On Thu, Apr 8, 2010 at 10:57 AM, Anthony C.

---

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [April 8, 2010, 6:23pm UTC](https://www.ruby-forum.com/t/rails-jquery-collection-select/186548/4 "2010-04-08T18:23:11Z")

</div>

Joshua,

Also consider using link\_to\_remote with the :update and :position  
options.  
Then you can keep all the view code on the server.

Anthony C.

On Thu, Apr 8, 2010 at 10:59 AM, Anthony C.

---

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [April 8, 2010, 8:41pm UTC](https://www.ruby-forum.com/t/rails-jquery-collection-select/186548/5 "2010-04-08T20:41:55Z")

</div>

Thanks Anthony! Your suggestion to use Rails’ to\_json method was right  
on  
target…

I ended up creating a controller for the PhoneNumberCategory and added  
the  
to\_json format on it’s index method. This way, my JQuery script can do a  
JSON request and then dynamically create a select based on the results.  
Then, when the user is ready to save, I get the phone numbers using the  
params, automagically populated by Rails…

Actually, my prior method was to use a link\_to\_remote, a partial, some  
controller methods to put an array of phone number hashes in the  
session,  
and other various things… But this was just way too much to maintain  
and  
was really kinda slow… I guess Rails has spoiled me in the fact that  
I’ve  
come to expect an easier way to do things - and coming from a Java/.NET  
background makes it all a bit of a vacation (:

Thanks again for your help - it’s people like you that make me excited  
about  
using/continuing to learn Rails! Bless you!

On Thu, Apr 8, 2010 at 12:21 PM, Anthony C.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)wrote:

> > > > My Phone Number Categories (Home/Work/etc.) are stored in the  
> > > > CONFIDENTIALITY NOTE: This e-mail message, including any attachment(s),  
> > > > Groups “Ruby on Rails: Talk” group.  
> > > > –  
> > > > You received this message because you are subscribed to the Google G.  
> > > > “Ruby on Rails: Talk” group.  
> > > > To post to this group, send email to removed\_email\_address@domain.invalid.  
> > > > To unsubscribe from this group, send email to  
> > > > removed\_email\_address@domain.invalid[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> > > > .  
> > > > For more options, visit this group at  
> > > > [http://groups.google.com/group/rubyonrails-talk?hl=en](http://groups.google.com/group/rubyonrails-talk?hl=en).

–

* * *

Joshua S. Martin

CONFIDENTIALITY NOTE: This e-mail message, including any attachment(s),  
contains information that may be confidential, protected by the attorney  
client or other legal privileges, and or proprietary non public  
information.  
If you are not an intended recipient of this message or an authorized  
assistant to an intended recipient, please notify the sender by replying  
to  
this message and then delete it from your system. Use, dissemination,  
distribution, or reproduction of this message and or any of its  
attachments  
(if any) by unintended recipients is not authorized and may be unlawful.
