I'm having a hard time trying to find a good tutorial on how to generate
a field (with ajax) from a dropdown selection.
For example:
I have an app where you can select up to two tickets per person. Only if
the user selects '2' from the dropdown menu should a field pop
up/generate below the dropdown and ask the user for the name of the
second person.
So far I have the following code:
<h3>Number of Tickets?</h3>
<%= select :tickets, :quantity, { "Select quantity" => "", "1" => "1",
"2" => "2"} %>
<h3>Name of guest?</h3>
<%= f.text_field :guest_name %>
So in my case, only if quantity is selected as two should guest_name
appear (without refreshing the page).
Thanks in advance and THANK YOU FOR YOUR TIME!!
Best regards,
Tony
--
Posted via http://www.ruby-forum.com/.
Thanks for the tip Patrick.
I saw the article and I understand how it's SUPPOSED to work. I also saw
a railscast about the same topic (dynamic drop down which fills a second
drop down). Unfortunately my javascript skills are not so great, so it's
a challenge to make it work. I'm sure with some more time I'll get it
working.
If anyone else can offer some help I'd appreciate it. Thanks again!
-Tony
Thanks Chris! I was able to get it working!
You're right about the Ajax not being needed, I'm just so used to
thinking that anything that "pops up" is ajax, even though it doesn't
need to be a server request. Sorry about that.
Two issues I'm having with this implementation though:
First, if I go to edit the form, I can see the '2' is selected but the
guest_name field is hidden until I select a different number of tickets
(I guess this activates/reloads the javascript). I even tried doing an
if statement in the div: <div id="guest_name"<%= " style='hidden'" if
!@tickets.quantity == '2' %>> which works (I can see this from the page
source). However, the div is ALWAYS visible, until I select a different
number of tickets and it acvtivates/reloads the javascript. Any ideas on
how to get this to work?
Second, I ended up using a table structure for the form, wrapping the
DIV around the <TR> doesn't work (the guest_name field is always
visible). I'm guessing this is a table structure issue so I need to take
a more in depth look.
Thanks again for the code Chris! It was extremely helpful.
-Tony
Okay, obviously there was a typo which when fixed ended up working.
style="display:none;" not style="hidden".
Hope this helps someone.