【问题标题】:Include blank for first item in select list in options_for_select tag在 options_for_select 标记的选择列表中包含第一项的空白
【发布时间】:2011-10-06 08:38:49
【问题描述】:

我试过:include_blank => true,但没用。

<select>
    <%= options_for_select Model.all.collect{|mt| [mt.name, mt.id]} %>
</select>

如果我需要将它添加到集合中,你会怎么做?

【问题讨论】:

  • 我相信 Rails 2 和 3 是一样的?您可能想要使用 collection_select 帮助程序而不是 html 选择标记 - 有关 include_blank 的更多信息 - house9.blogspot.com/2009/04/…

标签: ruby-on-rails ruby-on-rails-3 erb html-select


【解决方案1】:

我想你想要这种格式:

select("model_name", "model_id", Model.all.collect {|mt| [ mt.name, mt.id ] }, {:include_blank => 'name of your blank prompt'})

顺便说一句:假设模型应该是模型。使用 collection_select:

collection_select(:model, :model_id, Model.all, :id, :name, :prompt => true)

【讨论】:

  • 它也适用于许多其他 Rails 助手。我刚试过grouped_collection_select,它可以工作。谢谢@chris
  • 仅供参考,如果您使用 select_tag 并且想要为空白选项添加一些文本,则需要使用 prompt,因为 include_blank 仅是真/假。跨度>
  • @JoshPinter include_blank 也可以设置为每个 api.rubyonrails.org/classes/ActionView/Helpers/… ":include_blank 的字符串 - 如果选择元素的第一个选项元素为空白,则设置为 true 或提示字符串。如果有不是选择元素所需的默认值。”这适用于 Rails 4.2 甚至更早版本。
  • @Maltiriel Using select_tag on Rails 3 不显示字符串,如果你通过它。我期待它像其他选择方法一样,但事实并非如此。这就是为什么我留下评论以帮助他人。你可以在这里看到源只需删除include_blank值:apidock.com/rails/v3.2.13/ActionView/Helpers/FormTagHelper/…
  • 编辑不工作。它给了我相同的价值,而不是空白的!
【解决方案2】:

我相信:include_blank 选项只存在于与模型相关的select 字段。

假设您想使用普通的 &lt;select&gt; 标签而不是绑定到模型的 &lt;%= select(...) %&gt;,您可以在结果前面插入一个空白条目:

<%= options_for_select Modle.all.collect{|mt| [mt.name, mt.id]}.insert(0, "") %>

【讨论】:

  • 这是错误的。 include_blank: 是 select_tag 方法的一个选项,所以无论如何它都会起作用。
  • @tubbo include_blank: 很棒,它是最好的 Rails 方式选项。但是当只更新集合参数时,这个棘手的代码非常有用。
【解决方案3】:

由于您已标记为 select-tag,您可以将选项 include_blankselect_tag 一起使用。

来自文档:

select_tag "people", options_from_collection_for_select(@people, "id", "name"), :include_blank => true

# => <select id="people" name="people"><option value=""></option><option value="1">David</option></select>

或者你可以使用options_for_select:

<%= select_tag column.name, options_for_select(Model.all.collect{|mt| [mt.name, mt.id]}), :include_blank => true %>

【讨论】:

  • 这是问题的实际答案。其他涉及更改输入结构。
  • 仅供参考,带有select_tag(注意_tag 部分)include_blank 不会显示您传递给它的字符串。您将需要使用 prompt 而不是那个。
  • 这显示了一个空白选项,但存在一个问题:当编辑具有指定字段值的现有实体时,未加载数据库中存储的值,但显示的始终是空白选项.
【解决方案4】:
<%= options_for_select Model.all.collect{|x| [x.name,x.id]}.unshift(["",nil]) %>

【讨论】:

    【解决方案5】:
    = select_tag "some_value", options_for_select(Model.all.collect{ |x| [x.name, x.id]}.prepend([t('helpers.some_name'), nil]), :selected => params[:some_value])
    

    【讨论】:

      【解决方案6】:

      如果你想要一个巧妙的解决方案,你可以使用我的 gem rearmed_rails,它有一个安全的猴子补丁 options_for_selectoptions_for_collection_select 的功能

      rails g rearmed_rails:setup

      打开config/initializers/rearmed_rails.rb并将以下值更改为true

      options_for_select_include_blank: true,
      options_from_collection_for_select_include_blank: true
      


      现在,当您需要包含空白时,只需执行以下操作:

      <%= options_for_select(Model.all.map{|x| [x.name,x.id]}, include_blank: true) %>
      

      【讨论】:

        【解决方案7】:

        您可以使用以下猴子补丁将include_blank 参数添加到options_for_select

        module OptionsForSelectIncludeBlankPatch
        
          def options_for_select(container, selected = nil)
            if selected.is_a?(Hash)
              include_blank = selected[:include_blank] || selected['include_blank']
            end
        
            options = super
        
            if include_blank
              include_blank = '' if include_blank == true
        
              if Rails::VERSION::MAJOR >= 5 && Rails::VERSION::MINOR >= 1
                str = tag_builder.content_tag_string(:option, include_blank, {value: ''})
              else
                str = content_tag_string(:option, include_blank, {value: ''})
              end
        
              options.prepend(str)
            end
        
            options
          end
        
        end
        
        ActiveSupport.on_load(:action_view) do
          ActionView::Base.send(:include, RearmedRails::RailsHelpers)
        end
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-09-26
          相关资源
          最近更新 更多