I've just been doing something similar. This works, but might not be
as Railsy as it could be:

<h3>Number of Tickets?</h3>
<%= select :tickets, :quantity, {"1" => "1", "2" => "2"}, {:prompt =>
'Select quantity...'}, {:onchange => "if(this.options
[this.selectedIndex].value=='2')
  {Effect.BlindDown('guest_name', {duration: 0.5}); return false;}
     else
  {Effect.BlindUp('guest_name', {duration: 0.5}); return false;}"} %>

<div id="guest_name" style="display:none;">
  <h3>Name of guest?</h3>
  <%= f.text_field :guest_name %>
</div>

Notes:
1. This is not using Ajax - it is using JavaScript to show and hide a
div based on a selected value. You're not fetching data, so why use
Ajax to go to the server in this case?
2. You need to have Scriptaculous installed to use the effects (good
idea to give feedback to the user?). If you have <%=
javascript_include_tag :defaults %> in the head of your html page then
Scriptaculous is good to go.
3. Even if the guest_name field is hidden, it will still be submitted,
so test the value from your quantity select before saving the value
from guest_name.
4. I made a small change to your select to get your prompt ('Select
quantity') to work correctly.
5. Be careful with the formatting of the onchange part when copying
the code here: there should be new lines before {Effect.BlindDown,
else, and {Effect.BlindUp, but nowhere else.

Good luck.


On Jan 23, 3:13 am, Tony Tony <[email protected]>
wrote:
> Hi all,
>
> 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 viahttp://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 [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to