Collection_select default selected value

Hiall,

Unfortunately I just can’t find out how to setup a default selected
value when using collection_select. My call is like so:

<%= collection_select(:consultant, :lastname, @consultants, :id,
:lastname, { :selected => @current_consultant.id } ) %>

which is not working, I debugged so far that I know that
@current_consultant.id contains the correct value.

Any tips?

cheers
Martin

On Monday, May 22, 2006, at 7:56 PM, Martin G. wrote:

Any tips?

cheers
Martin


Rails mailing list
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails

What does the generated HTML look like?

_Kevin

Hi Kevin,

Sorry for replying late! The generated HTML looks like:

Auinger

Katterl

On 22 May 2006 18:22:50 -0000, Kevin O.

On Monday, May 22, 2006, at 9:35 PM, Martin G. wrote:

:lastname, { :selected => @current_consultant.id } ) %>
Rails mailing list
Rails mailing list
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails


Rails mailing list
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails

try this…

:selected => @current_consultant.id.to_s

The ‘values’ that the select compares against are really strings. Not
sure if they get properly cast for the purposes of the comparison needed
to set the selected one.

_Kevin

Thx for the tip but that didn’t work. In the meantime I found out that a
call to
<% options = options_from_collection_for_select(@consultants, :id,
:lastname, @current_consultant.id) %>

produces correct option tags like:

Auinger Katterl

However, I find no way to wrap this into a select tag. …

There must be an easier way???

cheers
Martin

On 22 May 2006 20:01:42 -0000, Kevin O.

I also tried to give the parameters as a hash with keys corresponding
to parameters of options_from_collection_for_select. This also doesn’t
lead to anything …

I’ve tried that before :slight_smile: This leads to very strange:

<option value=“1”>Auinger</option>

<option value="2" selected="selected">Katterl</option>

showing the whole option strings in the dropdown …

On 22 May 2006 20:34:13 -0000, Kevin O.

Going through the AWDWR book I decided to go another way. I now have

@consultants = CommunalAuditConsultant.find(:all, :order => “lastname”
).map {|c| [c.lastname, c.id] }

in my controller, and then

<%= select(:communal_audit_consultant, :id, @consultants) %>

in my partial

This works perfectly!

Thx for your patience and tips, Kevin!

cheers
Martin

On Monday, May 22, 2006, at 10:17 PM, Martin G. wrote:

However, I find no way to wrap this into a select tag. …

Hi Kevin,
On 22 May 2006 18:22:50 -0000, Kevin O.

[email protected]
[email protected]


Rails mailing list
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails


Rails mailing list
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails

Try
<%= select_tag ‘communal_audit_consultant_lastname’, options %>

_Kevin

You could add in your controller method…

@consultants.lastname = ‘bla’

Craig

Hey All:

Martin G. wrote:

I’ve tried that before :slight_smile: This leads to very strange:

<option value=“1”>Auinger</option>

<option value="2" selected="selected">Katterl</option>

showing the whole option strings in the dropdown …

On 22 May 2006 20:34:13 -0000, Kevin O.

This has always worked for me - just follow the docs :wink:

<%= select_tag ‘my[select]’,
options_from_collection_for_select(Model.find(:all),‘id’,‘name’,some_string)
%>

where some_string = an actual string (i.e. ‘Joe’) or a variable (ala
@person.name).

Works like a charm - everytime.

Cheers,
divotdave

Martin is right, but would like to add a comment. If you do

<%= select_tag ‘my[select]’,
options_from_collection_for_select(Model.find(:all),‘id’,‘name’,some_string)
%>

Then you do need to pass some_string as string. However, if you are
passing symbols, the you would need to convert to the symbol’s type:

<%= select_tag ‘my[select]’,
options_from_collection_for_select(Model.find(:all),:id,:name,some_string.to_i)
%>

Hope that helps someone

Riclaso

divotdave wrote:

Hey All:

Martin G. wrote:

I’ve tried that before :slight_smile: This leads to very strange:

<option value=“1”>Auinger</option>

<option value="2" selected="selected">Katterl</option>

showing the whole option strings in the dropdown …

On 22 May 2006 20:34:13 -0000, Kevin O.

This has always worked for me - just follow the docs :wink:

<%= select_tag ‘my[select]’,
options_from_collection_for_select(Model.find(:all),‘id’,‘name’,some_string)
%>

where some_string = an actual string (i.e. ‘Joe’) or a variable (ala
@person.name).

Works like a charm - everytime.

Cheers,
divotdave

Hi,

I have some issues with setting the selected value. On my page, user
selects a State from the drop down and other attributes and click on
“Find”. I want to set the default value of the drop down to the value
selected prior to clicking “Find”.

<%=collection_select(:state, :state_id, @states, :state_id, :state_name,
{:prompt => true, :selected => @selected_state},
{:style => ‘width:200px;’,
:onchange => remote_function(:url =>
{:action => ‘update_cities’},:with =>
“‘state_id=’+value”)})%>

In my controller, if I have @selected_state = 1, the dropdown default
value is shown in the UI. However, if I have @selected_state =
state_id, the default value is not set.

Snippet of the code in my controller:

def find
state_id = params[:state][:state_id]

@selected_state = state_id

/* Some logic here */

@states = State.find(:all)

respond_to do |format|
format.html { render :action => “index” }
end
end

Any pointers are appreciated. Thanks.

This is useful about collection_select :slight_smile:

http://shiningthrough.co.uk/blog/show/6

On Sep 24, 12:13 am, Eunice T. [email protected]

Agree with you . Someone should merge the documentation there to the
Rails API. The rails api’s collection_select doc is somewhat
incomplete or too brief. In short, it’s not helpful.

Thanks for the link Zt Di.

@selected_state = state_id.to_i works.

This is because state_id = params[:state][:state_id] returns a string.