can we use collection_select with :selected option if yes ,plz let me know.if not than plz tell me how to use select instead of collection_select and where to write query in select if i dont want to find all records.

thnx in advance.

Recommended Answers

All 3 Replies

can we use collection_select with :selected option if yes ,plz let me know.if not than plz tell me how to use select instead of collection_select and where to write query in select if i dont want to find all records.

thnx in advance.

You need to give more information. What do you want to be selected and when?

If you want a default value put

t.column :favourite_colour_id, :integer, :default => 1

in your migration.

If you are loading an existing record (for editing) the correct value should already be selected in the select box.

If you are still stuck please paste your controller and view code.

thanx buddy for quick reply,

now i am explaning you the whole scenario.what i have done is ,i have a sample form with collection_select on it with multiple true(suppose five values).on submit selected values goes into database(suppose two) coreesponding to particular id.now when i again open the page i want the values from database comes as selected(two values that are coming from database as highlighted or selected).and thanx for all your help till now and in future.

thanx buddy for quick reply,

now i am explaning you the whole scenario.what i have done is ,i have a sample form with collection_select on it with multiple true(suppose five values).on submit selected values goes into database(suppose two) coreesponding to particular id.now when i again open the page i want the values from database comes as selected(two values that are coming from database as highlighted or selected).and thanx for all your help till now and in future.

Ah - I see what you mean now.

Try something like this (for this i'm assuming a person has many jobs):

<%= collection_select 
	:person, 
	:job_ids,
	Job.find(:all),
	:id, 
	:description,
	{},
	:multiple => true,
	:name => 'person[job_ids][]' 
%>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.