# Selected in select

**URL:** <https://www.ruby-forum.com/t/selected-in-select/171448>\
**Category:** Rails\
**Created:** [July 2, 2009, 5:08pm UTC](https://www.ruby-forum.com/t/selected-in-select/171448 "2009-07-02T17:08:29Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![mistressjanna](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/3ab097/32.png) [@mistressjanna](https://www.ruby-forum.com/u/mistressjanna)\
**Post date:** [July 2, 2009, 5:08pm UTC](https://www.ruby-forum.com/t/selected-in-select/171448/1 "2009-07-02T17:08:29Z")

</div>

I have a partial in my view which has a select box:

\<% form\_for :channels, :html =\> {:name =\> ‘channelsform’},:url=\>  
{ :action =\>“newchannel”, :controller =\>“channels”} do |f| %\>

\<%= f.select(:id , Channel.find(:all, :order =\> ‘channel  
ASC’, :conditions =\> [‘deleted=0’]).collect {|p| [ p.channel,  
p.id ]}, {:include\_blank =\> true}, { :onchange =\>  
“document.channelsform.submit();”}) %\>

In my ChannelsController I pick up the value from this select box  
correctly as: @channel = Channel.find(params[:channels][:id])

However, when the partial rerenders, it doesn;t show the selected  
value. How can I make it show the selected value, given the syntax for  
the form.select I have used?

Thanks, Janna B

---

<div class="post-metadata">

**Author:** ![mistressjanna](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/3ab097/32.png) [@mistressjanna](https://www.ruby-forum.com/u/mistressjanna)\
**Post date:** [July 2, 2009, 5:20pm UTC](https://www.ruby-forum.com/t/selected-in-select/171448/2 "2009-07-02T17:20:46Z")

</div>

Janna Brossard wrote:

> I have a partial in my view which has a select box:
> 
> \<% form\_for :channels, :html =\> {:name =\> ‘channelsform’},:url=\>  
> { :action =\>“newchannel”, :controller =\>“channels”} do |f| %\>
> 
> \<%= f.select(:id , Channel.find(:all, :order =\> ‘channel  
> ASC’, :conditions =\> [‘deleted=0’]).collect {|p| [ p.channel,  
> p.id ]}, {:include\_blank =\> true}, { :onchange =\>  
> “document.channelsform.submit();”}) %\>
> 
> In my ChannelsController I pick up the value from this select box  
> correctly as: @channel = Channel.find(params[:channels][:id])
> 
> However, when the partial rerenders, it doesn;t show the selected  
> value. How can I make it show the selected value, given the syntax for  
> the form.select I have used?
> 
> Thanks, Janna B

What I read from the documentation: select  
(ActionView::Helpers::FormOptionsHelper)

“The option currently held by the object will be selected, provided that  
the object is available.”

Maybe you must use, while you have populated the object @channel in the  
controller rendering that view.

\<%= f.select(“channel”, “id” , Channel.find(:all, :order =\> ‘channel  
ASC’, :conditions =\> [‘deleted=0’]).collect {|p| [ p.channel,  
p.id ]}, {:include\_blank =\> true}, { :onchange =\>  
“document.channelsform.submit();”}) %\>

I am not sure but you may give it a ride.

---

<div class="post-metadata">

**Author:** ![mistressjanna](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/3ab097/32.png) [@mistressjanna](https://www.ruby-forum.com/u/mistressjanna)\
**Post date:** [July 2, 2009, 5:35pm UTC](https://www.ruby-forum.com/t/selected-in-select/171448/3 "2009-07-02T17:35:58Z")

</div>

wrong # or arguments.

On Jul 2, 11:20 am, Onur G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![mistressjanna](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/3ab097/32.png) [@mistressjanna](https://www.ruby-forum.com/u/mistressjanna)\
**Post date:** [July 2, 2009, 5:41pm UTC](https://www.ruby-forum.com/t/selected-in-select/171448/4 "2009-07-02T17:41:11Z")

</div>

Janna Brossard wrote:

> wrong # or arguments.
> 
> On Jul 2, 11:20ï¿½am, Onur G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

hmm.

I can’t try right now but did you try the method without the “f.”? I  
mean call the “select” method not the method of the class which the “f”  
object belongs.

---

<div class="post-metadata">

**Author:** ![mistressjanna](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/3ab097/32.png) [@mistressjanna](https://www.ruby-forum.com/u/mistressjanna)\
**Post date:** [July 2, 2009, 5:42pm UTC](https://www.ruby-forum.com/t/selected-in-select/171448/5 "2009-07-02T17:42:59Z")

</div>

“Wrong # of arguments.” Why answer a post if you don;t know the  
answer? It deters those who DO know from answering. Please don;t just  
speculate. By the time someone has posted, they have speculated as far  
as they can go and are seeking direction on where to find the real  
answer.

On Jul 2, 11:20 am, Onur G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![mistressjanna](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/3ab097/32.png) [@mistressjanna](https://www.ruby-forum.com/u/mistressjanna)\
**Post date:** [July 2, 2009, 5:55pm UTC](https://www.ruby-forum.com/t/selected-in-select/171448/6 "2009-07-02T17:55:15Z")

</div>

Why would my variable I have in my controller as @channel, set as:  
def newchannel  
@channel = Channel.find(params[:channel][:id])  
render :action =\> ‘display’  
end

Which occurs when the select box changes, be set to nil in another  
method in my controller? It acts as a local variable to the function  
newchannel – but I want it to be available throughout my controller.  
How can I achieve this? If I specify it as attr\_accessor :channel in  
my model, then the select box does not populate.  
-Janna B.

On Jul 2, 11:20 am, Onur G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![mistressjanna](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/3ab097/32.png) [@mistressjanna](https://www.ruby-forum.com/u/mistressjanna)\
**Post date:** [July 3, 2009, 12:22am UTC](https://www.ruby-forum.com/t/selected-in-select/171448/7 "2009-07-03T00:22:24Z")

</div>

Janna Brossard wrote:

> Why would my variable I have in my controller as @channel, set as:  
> def newchannel  
> @channel = Channel.find(params[:channel][:id])  
> render :action =\> ‘display’  
> end
> 
> Which occurs when the select box changes, be set to nil in another  
> method in my controller? It acts as a local variable to the function  
> newchannel – but I want it to be available throughout my controller.  
> How can I achieve this? If I specify it as attr\_accessor :channel in  
> my model, then the select box does not populate.  
> -Janna B.
> 
> On Jul 2, 11:20ï¿½am, Onur G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

hi,

I am not sure about this either, maybe I must not answer this 🙂

in fact, I thought twice before posting that answer, but decided that  
this kind of forums are for discussion and speculation for beginners.

we will learn together.

anyway I’ll answer, as far as I know, controllers are created and  
discarded for each request, so you have to initialize the variables  
again.

this is the way it is.

but, if you _really_ want to do this, you can achieve a _similar_ effect  
by using a class global variable i.e. @@myvar but this is a very bad  
idea, so don’t do it.

basically, initialize the variable in each controller method.

onur

---

<div class="post-metadata">

**Author:** ![mistressjanna](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/3ab097/32.png) [@mistressjanna](https://www.ruby-forum.com/u/mistressjanna)\
**Post date:** [July 2, 2009, 5:47pm UTC](https://www.ruby-forum.com/t/selected-in-select/171448/8 "2009-07-02T17:47:04Z")

</div>

My apologies to you – I am frustrated. Yes, it works when I do as you  
(guessed, heh heh) and take off the f so I have:

select(“channel”, “id”…

and in my controller, I specify now:  
@channel = Channel.find(params[:channel][:id])

Thank you Onur. -Janna

On Jul 2, 11:41 am, Onur G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
