【问题标题】:convert select to select_tag in rails在rails中将select转换为select_tag
【发布时间】:2014-04-23 14:50:16
【问题描述】:

有没有办法将select转换为这个语句的select_tag:

<%= select "invoice_product_code",
    @invoice_product_array.each_with_index.map {
        |jsondata, index| 
            [jsondata["invoice_product_code"],
            index]}, 
            {:selected => item["invoice_product_code"]},
            :style => "width:110px"

        %> 

记住,

  • 我想从数组中定义每个选项值和选项文本

  • 选择的选项

  • 风格

就像上面一样。

这是生成的 HTML:

 <select id="invoice_product_code_invoice_product_code" name="invoice_product_code[invoice_product_code]" style="width:110px"><option value="0">INVPRODA</option>
 <option value="1">INVPRODB</option>
 <option selected="selected" value="2">INVPRODC</option>
 <option value="3">INVPRODD</option>
 <option value="4">INVPRODE</option>
 <option value="5">INVPRODF</option>
 <option value="6">INVPRODG</option></select>

我觉得这是不可能的。从 API http://apidock.com/rails/ActionView/Helpers/FormTagHelper/select_tag 中找不到合适的例子@

这没用:

 <%= select_tag "invoice_product_code",  
    @invoice_product_array.each_with_index.map {
        |jsondata, index| 
            [jsondata["invoice_product_code"],
            index]} 


        %> 

这没有用: 它是正确的,因为值和显示文本是 json 字符串,而不是数组项中的特定键值。

        <%= select_tag "invoice_product_code",  
            options_for_select(@invoice_product_array)

        %> 

【问题讨论】:

    标签: html ruby-on-rails ruby select ruby-on-rails-4


    【解决方案1】:

    options_for_select 需要一个额外的参数来指定所选值,并且每个选项都可以将 HTML 属性作为最后一个元素。

    <%= select_tag "invoice_product_code", options_for_select(  
      @invoice_product_array.each_with_index.map {
        |jsondata, index| 
          [jsondata["invoice_product_code"],
          index,
          style: 'width: 110px;']},
      item["invoice_product_code"])
    %> 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-01-17
      • 1970-01-01
      • 1970-01-01
      • 2012-04-13
      • 1970-01-01
      • 2014-08-04
      • 1970-01-01
      相关资源
      最近更新 更多