jshou
jshou

Reputation: 682

include_blank false doesn't work for collection_select in Rails 3

I'm trying to use collection_select for a form in Rails3 with a default value, but without a blank option. I'm using the following line, but the blank value keeps showing up.

f.collection_select(:user_id, @users, :id, :name, {:selected => current_user.id, :include_blank => false})

Upvotes: 0

Views: 779

Answers (1)

miked
miked

Reputation: 4499

Stick with:

f.collection_select(:user_id, @users, :id, :name)

and set the default in your controller:

def new
  @my_obj = MyObject.new(:user_id=>defaulted_user_id)
end

If that doesn't work, ensure the user you are defaulting is responding to user.name correctly or even if it has a name value set. Since you might actually be setting the id but the user doesn't have a "name" and would therefore show as a blank.

Upvotes: 1

Related Questions