How do I set a blank value for an f.select form field

前端 未结 5 1830
花落未央
花落未央 2021-02-01 12:24

I am using the following to allow my users to select their sex in their profile.

<%= f.select (:sex, %w{ Male Female }) %>

How would I c

5条回答
  •  你的背包
    2021-02-01 12:48

    There are two possibilities, depending on what you're after:

    include_blank

    <%= f.select (:sex, %w{ Male Female }, :include_blank => true) %>
    

    This will always include a blank option in the select, which will allow people to set the value back to the blank value if they're seeing this on an edit form.

    prompt

    <%= f.select (:sex, %w{ Male Female }, :prompt => "Gender...") %>
    

    This will include the specified prompt value, so long as the field hasn't already been set. If it has (on an edit form for example), there's no need to remind the user that they need to select a value so the prompt doesn't appear

提交回复
热议问题