# Keep collection\_select as selected after "submit"

**URL:** <https://www.ruby-forum.com/t/keep-collection-select-as-selected-after-submit/196006>\
**Category:** Rails\
**Created:** [October 5, 2010, 11:15am UTC](https://www.ruby-forum.com/t/keep-collection-select-as-selected-after-submit/196006 "2010-10-05T11:15:05Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![PalaniKannan\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/eb8c5e/32.png) [@PalaniKannan\_K](https://www.ruby-forum.com/u/PalaniKannan_K)\
**Post date:** [October 5, 2010, 11:15am UTC](https://www.ruby-forum.com/t/keep-collection-select-as-selected-after-submit/196006/1 "2010-10-05T11:15:05Z")

</div>

Dear All,

Anybody knows to keep the selected option as alive even after “submit”  
in  
collection\_select.

–  
With Regards  
Palani Kannan. K

---

<div class="post-metadata">

**Author:** ![PalaniKannan\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/eb8c5e/32.png) [@PalaniKannan\_K](https://www.ruby-forum.com/u/PalaniKannan_K)\
**Post date:** [October 5, 2010, 3:08pm UTC](https://www.ruby-forum.com/t/keep-collection-select-as-selected-after-submit/196006/2 "2010-10-05T15:08:04Z")

</div>

On 5 October 2010 11:14, PalaniKannan K [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Dear All,
> 
> Anybody knows to keep the selected option as alive even after “submit” in  
> collection\_select.

Hi,

I found the method. If I use like this I can keep as live.

\<%= collection\_select(@table, “gm”, @pop, “col1”, “col1”, :prompt =\>  
true, _:selected  
=\> params[:gm]_) %\>

The bold marked content, I inserted to my script and the selected value  
remains.

> –  
> With Regards  
> Palani Kannan. K

–  
With Regards  
Palani Kannan. K

---

<div class="post-metadata">

**Author:** ![PalaniKannan\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/eb8c5e/32.png) [@PalaniKannan\_K](https://www.ruby-forum.com/u/PalaniKannan_K)\
**Post date:** [October 5, 2010, 3:29pm UTC](https://www.ruby-forum.com/t/keep-collection-select-as-selected-after-submit/196006/3 "2010-10-05T15:29:43Z")

</div>

On 5 October 2010 14:06, PalaniKannan K [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I found the method. If I use like this I can keep as live.
> 
> \<%= collection\_select(@table, “gm”, @pop, “col1”, “col1”, :prompt =\> true,  
> :selected =\> params[:gm]) %\>
> 
> The bold marked content, I inserted to my script and the selected value  
> remains.

I think it is not considered good practice to directly reference  
params in the view, as that means that the view is directly dependent  
on the way the controller action was called. It might be better in  
the controller action to say  
@selected\_item = params[:gm]  
and then in the view use :selected = @selected\_item.  
It amounts to the same thing but achieves better separation of the  
different bits of code.

Colin
