Ruby-on-rails 如何为 f.select 表单字段设置空白值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4444523/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 23:53:53  来源:igfitidea点击:

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

ruby-on-railsrubyselect

提问by bgadoci

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 create a blank value that the list would default to if nothing has been passed to the user.sex column? I am simply passing male or female as a string.

如果没有任何内容传递给 user.sex 列,我将如何创建列表默认为的空白值?我只是将男性或女性作为字符串传递。

The purpose is I want a blank value so a validation can make sure they are aware they have to select it.

目的是我想要一个空白值,以便验证可以确保他们知道他们必须选择它。

回答by Gareth

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

有两种可能性,具体取决于您的追求:

include_blank

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

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

这将包括指定的提示值,只要该字段尚未设置。如果它有(例如在编辑表单上),则无需提醒用户他们需要选择一个值,这样就不会出现提示

回答by jyoseph

I think you can do something like this:

我认为你可以做这样的事情:

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

回答by Ziv Galili

in Rails 4 you can achieve prompt like this:

在 Rails 4 中,您可以实现如下提示:

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

its working for me with simple form.

它以简单的形式对我来说有效。

回答by Hardik Hardiya

You go with

你跟

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

回答by Donato

I tried to use 'prompt' as a string. But in the rendered output, the new option prompt was not appearing. The select_tag method searches only for a symbol. It appears to be the case for :include_blank as well. Check out the options.delete:

我尝试使用 'prompt' 作为字符串。但是在渲染的输出中,没有出现新的选项提示。select_tag 方法只搜索一个符号。:include_blank 似乎也是如此。查看options.delete:

  def select_tag(name, option_tags = nil, options = {})
    option_tags ||= ""
    html_name = (options[:multiple] == true && !name.to_s.ends_with?("[]")) ? "#{name}[]" : name

    if options.include?(:include_blank)
      include_blank = options.delete(:include_blank)

      if include_blank == true
        include_blank = ''
      end

      if include_blank
        option_tags = content_tag(:option, include_blank, value: '').safe_concat(option_tags)
      end
    end

    if prompt = options.delete(:prompt)
      option_tags = content_tag(:option, prompt, value: '').safe_concat(option_tags)
    end

    content_tag :select, option_tags, { "name" => html_name, "id" => sanitize_to_id(name) }.update(options.stringify_keys)
  end

Also note that :include_blank and :prompt will be options of the select or select_tag, not the options_for_select.

还要注意 :include_blank 和 :prompt 将是 select 或 select_tag 的选项,而不是 options_for_select。