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. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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 Crumley On Thu, Apr 8, 2010 at 10:45 AM, Joshua Martin <josmar52789-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>wrote:> 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. > > -- > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> > . > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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 Crumley On Thu, Apr 8, 2010 at 10:57 AM, Anthony Crumley <anthony.crumley-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>wrote:> 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 Crumley > > On Thu, Apr 8, 2010 at 10:45 AM, Joshua Martin <josmar52789-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>wrote: > >> 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. >> >> -- >> 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org >> To unsubscribe from this group, send email to >> rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> >> . >> 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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 Crumley On Thu, Apr 8, 2010 at 10:59 AM, Anthony Crumley <anthony.crumley-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>wrote:> 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 Crumley > > > On Thu, Apr 8, 2010 at 10:57 AM, Anthony Crumley < > anthony.crumley-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > >> 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 Crumley >> >> On Thu, Apr 8, 2010 at 10:45 AM, Joshua Martin <josmar52789-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>wrote: >> >>> 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. >>> >>> -- >>> 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org >>> To unsubscribe from this group, send email to >>> rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> >>> . >>> 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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 Crumley <anthony.crumley-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>wrote:> 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 Crumley > > > On Thu, Apr 8, 2010 at 10:59 AM, Anthony Crumley < > anthony.crumley-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > >> 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 Crumley >> >> >> On Thu, Apr 8, 2010 at 10:57 AM, Anthony Crumley < >> anthony.crumley-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> >>> 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 Crumley >>> >>> On Thu, Apr 8, 2010 at 10:45 AM, Joshua Martin <josmar52789-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>wrote: >>> >>>> 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. >>>> >>>> -- >>>> 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org >>>> To unsubscribe from this group, send email to >>>> rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> >>>> . >>>> 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> > . > For more options, visit this group at > 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. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.