# Prompt in select\_tag

**URL:** <https://www.ruby-forum.com/t/prompt-in-select-tag/141144>\
**Category:** Rails\
**Created:** [June 22, 2008, 5:49pm UTC](https://www.ruby-forum.com/t/prompt-in-select-tag/141144 "2008-06-22T17:49:20Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mailbox\_abhishek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e480ec/32.png) [@mailbox\_abhishek](https://www.ruby-forum.com/u/mailbox_abhishek)\
**Post date:** [June 22, 2008, 5:49pm UTC](https://www.ruby-forum.com/t/prompt-in-select-tag/141144/1 "2008-06-22T17:49:20Z")

</div>

How can I use select\_tag to

1. Have a prompt like “Select a Value” ( selected by default )

I tried this but did not work:

```
    <%= select_tag :current_course,
             options_for_select( Course.find(:all).map {|p|

```

[p.title,p.id]} ),  
{ :prompt =\> “Select a Value” }  
%\>

1. Generate a ‘selected’ event so that some action can be done like  
submit the form or refresh the page?

Thanks in advance

- AJ

---

<div class="post-metadata">

**Author:** ![mailbox\_abhishek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e480ec/32.png) [@mailbox\_abhishek](https://www.ruby-forum.com/u/mailbox_abhishek)\
**Post date:** [June 22, 2008, 7:26pm UTC](https://www.ruby-forum.com/t/prompt-in-select-tag/141144/2 "2008-06-22T19:26:48Z")

</div>

Abhishek J. wrote:

> How can I use select\_tag to
> 
> 1. Have a prompt like “Select a Value” ( selected by default )  
> I tried this but did not work:  
> \<%= select\_tag :current\_course,  
> options\_for\_select( Course.find(:all).map {|p|  
> [p.title,p.id]} ),  
> { :prompt =\> “Select a Value” }  
> %\>

I do the find in the controller and feed the options\_for\_select two  
instance  
variables: the options collection and the selected value.  
e.g. select\_tag(:priority, options\_for\_select(@courses,  
@selected\_course))  
@courses is just an array so you just put your “Select a Value” as the  
first  
entry.  
e.g., @courses = [[“Select a Value”, “Select a Value”]]  
Then push the results of the find onto @courses.

> 1. Generate a ‘selected’ event so that some action can be done like  
> submit the form or refresh the page?

If I understand your question, you’ll want to use observe\_field on the  
select\_tag. It will fire an Ajax call back to the server when the value  
in  
the select changes.

HTH,  
Bill

---

<div class="post-metadata">

**Author:** ![mailbox\_abhishek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e480ec/32.png) [@mailbox\_abhishek](https://www.ruby-forum.com/u/mailbox_abhishek)\
**Post date:** [June 24, 2008, 4:00am UTC](https://www.ruby-forum.com/t/prompt-in-select-tag/141144/3 "2008-06-24T04:00:18Z")

</div>

Bill W. wrote:

> Abhishek J. wrote:
> 
> > How can I use select\_tag to
> > 
> > 1. Have a prompt like “Select a Value” ( selected by default )  
> > I tried this but did not work:  
> > \<%= select\_tag :current\_course,  
> > options\_for\_select( Course.find(:all).map {|p|  
> > [p.title,p.id]} ),  
> > { :prompt =\> “Select a Value” }  
> > %\>
> 
> I do the find in the controller and feed the options\_for\_select two  
> instance  
> variables: the options collection and the selected value.  
> e.g. select\_tag(:priority, options\_for\_select(@courses,  
> @selected\_course))  
> @courses is just an array so you just put your “Select a Value” as the  
> first  
> entry.  
> e.g., @courses = [[“Select a Value”, “Select a Value”]]  
> Then push the results of the find onto @courses.
> 
> > 1. Generate a ‘selected’ event so that some action can be done like  
> > submit the form or refresh the page?
> 
> If I understand your question, you’ll want to use observe\_field on the  
> select\_tag. It will fire an Ajax call back to the server when the value  
> in  
> the select changes.
> 
> HTH,  
> Bill

Hey Bill

Thanks for the help. I could get the “prompt” in my selection list. I  
havent tried observe\_field yet but will give that a shot. Thanks again -  
AJ
