Selected in select

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

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.

wrong # or arguments.

On Jul 2, 11:20 am, Onur G. [email protected]

Janna Brossard wrote:

wrong # or arguments.

On Jul 2, 11:20�am, Onur G. [email protected]

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.

“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. [email protected]

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. [email protected]

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. [email protected]

hi,

I am not sure about this either, maybe I must not answer this :slight_smile:

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

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. [email protected]