# Newbie: Can I preselect pulldown option from passed in var?

**URL:** <https://www.ruby-forum.com/t/newbie-can-i-preselect-pulldown-option-from-passed-in-var/74340>\
**Category:** Rails\
**Created:** [November 4, 2006, 10:42pm UTC](https://www.ruby-forum.com/t/newbie-can-i-preselect-pulldown-option-from-passed-in-var/74340 "2006-11-04T22:42:56Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/eada6e/32.png) [@dan](https://www.ruby-forum.com/u/dan)\
**Post date:** [November 4, 2006, 10:42pm UTC](https://www.ruby-forum.com/t/newbie-can-i-preselect-pulldown-option-from-passed-in-var/74340/1 "2006-11-04T22:42:56Z")

</div>

Currently I have a list of clients with a link next to each one so I can  
add a new task for that particular client.

When I click the link to go to the new task view page I’d like to retain  
the clients\_id so that it can be pre-selected in the pulldown menu which  
allows me to assign a client otherwise.

I can see the client\_id being passed in the url and I can render it on  
the new task form page by using \<%= @client\_id %\> but using a select  
menu or collection\_select the client\_id is not preselected.

Specifically,

In my client list view I have this:  
\<%= link\_to ‘add task’, { :action =\> ‘new’, :controller =\> “tasks”,  
:client\_id =\> client.id } %\>

In my task controller in the new function definition I have:  
@client\_id = params[:client\_id]

And in the task form I’m using this to create a pulldown menu of client  
company names:  
\<%= select(‘task’, ‘client\_id’, all\_clients.collect {|p| [  
p.company\_name, p.id ] } ) %\>

At first I was trying to use collection\_select since it seemed easier  
but that didn’t work either.

I hope this doesn’t seem too vague or basic or something to answer. Any  
help or pointers appreciated.

DAN

---

<div class="post-metadata">

**Author:** ![dan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/eada6e/32.png) [@dan](https://www.ruby-forum.com/u/dan)\
**Post date:** [November 4, 2006, 10:53pm UTC](https://www.ruby-forum.com/t/newbie-can-i-preselect-pulldown-option-from-passed-in-var/74340/2 "2006-11-04T22:53:00Z")

</div>

If you use select(‘task’, ‘client\_id’, …)  
Then the drop down will preselect the value given by calling the  
client\_id method on @task, so set @task.client\_id to the value passed  
through the params and you should be ok.

Fred

---

<div class="post-metadata">

**Author:** ![dan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/eada6e/32.png) [@dan](https://www.ruby-forum.com/u/dan)\
**Post date:** [November 4, 2006, 11:39pm UTC](https://www.ruby-forum.com/t/newbie-can-i-preselect-pulldown-option-from-passed-in-var/74340/3 "2006-11-04T23:39:15Z")

</div>

Thanks!

Adding \<% @task.client\_id = @client\_id %\> above the form did the trick.

DAN

---

<div class="post-metadata">

**Author:** ![dan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/eada6e/32.png) [@dan](https://www.ruby-forum.com/u/dan)\
**Post date:** [November 7, 2006, 11:05am UTC](https://www.ruby-forum.com/t/newbie-can-i-preselect-pulldown-option-from-passed-in-var/74340/4 "2006-11-07T11:05:05Z")

</div>

:select =\> @client\_id in the html options hash

> **[ActionView::Helpers::FormOptionsHelper](https://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#M000399)**
>
> Provides a number of methods for turning different kinds of containers into a set of option tags.
