Wednesday, May 20, 2009

select field with default value in rails

I wanted to make a select box from a list in the database, and I wanted the default value for a field to be the person logged in. Should be simple and it was, but the api didn't have any example, so here is what I used.
I'm using form_for so my code was different than the docs too, maybe this is in there somewhere..
<% form_for(@task) do |f| %>
<%= f.label :user_id %>
<%= f.collection_select(:user_id, User.find(:all), :id, :name, {:selected => session[:user_id]})

If there is a better way let me know.

No comments:

Post a Comment