views:

8053

answers:

5

Here a code I'm using now.

<%= f.select :project_id, @project_select %>

How to modify it to make it it's default value params[:pid] when page is loaded?

A: 

This should work for you. It just passes {:value => params[:pid] } to the html_options variable.

<%= f.select :project_id, @project_select, {}, {:value => params[:pid] } %>
epochwolf
but it won't select appropriate item in select :S
totocaster
+4  A: 

I've found solution and fount that I'm pretty unexperienced in RoR.

# in controller which manages view described above
@work.project_id = params[:pid] unless params[:pid].nil?
totocaster
+6  A: 

This should do it.

<%= f.select :project_id, @project_select, :selected => params[:pid] %>

htanata
+3  A: 

if params[:pid] is a string, which if it came from a form, it is, you'll probably need to use
params[:pid].to_i
for the correct item to be selected in the select list

danengle
Thanks for pointing this out
chrispix
+1  A: 

Use the right attribute of the current instance (e.g. @work.project_id):

<%= f.select :project_id, options_for_select(..., @work.project_id ) %>
lynx