【问题标题】:How to make the label title and input value the same, but id is different如何使标签标题和输入值相同,但id不同
【发布时间】:2014-10-08 12:38:34
【问题描述】:

我有一个问题,如何使标签标题和输入值相同,但 id 不同。下面的代码显示了我想要的。

<%= f.input :building_type, :label_html => {:class => "ui-priority n1"}, 
    label: "Building type", collection: Post::Flat::BUILDING_TYPES, as: :radio %>


Post::Flat::BUILDING_TYPES = [["Flat-super", 1], ["Flat-middle", 2]]

输出代码:

<div class="radio">
  <label class="choice" for="post_flat_building_type-1">
    <input id="post_flat_building_type-1" name="post_flat[building_type]" type="radio" value="1">
    Flat-super
  </label>
</div>
<div class="radio">
  <label class="choice" for="post_flat_building_type-2">
    <input id="post_flat_building_type-2" name="post_flat[building_type]" type="radio" value="2">
    Flat-middle
  </label>
</div>

但我想让 value 等于 Flat-super,Flat-middle,我该如何实现呢?

感谢您的帮助!

Formtastic 版本 - 2.3 Formtastic-bootstrap 版本 - 3.0

----- 一些补充----

有人告诉我可以通过将初始数组更改为来解决这个问题

Post::Flat::BUILDING_TYPES = [["Flat-super", "Flat-super"], ["Flat-middle", "Flat-middle"]]

但是如果我有西里尔字母就不行了

Post::Flat::BUILDING_TYPES = [["Кирпичный", "Кирпичный"]

<div class="radio">
  <label class="choice" for="post_flat_building_type">
   <input id="post_flat_building_type" name="post_flat[building_type]" type="radio" value="кирпичный">
    кирпичный
  </label>
</div>

您可以观察到输出包含错误的 id,它会影响我的数组中是否有 1 个以上的对象。所有输入都将具有相同的 id。

【问题讨论】:

  • 您是否尝试过:Post::Flat::BUILDING_TYPES = [["Flat-super", "Flat-super"], ["Flat-middle", "Flat-middle"]]
  • 如果我尝试使用西里尔字母,这将不起作用。
  • 例如 - Post::Flat::BUILDING_TYPES = [["Кирпичный", "Кирпичный"]]

标签: ruby-on-rails ruby formtastic


【解决方案1】:

如果不需要“漂亮”的html和id\进行命名,可以使用.hash方法使其唯一。

这是我正在使用的示例(无需转译任何内容)

module Formtastic
  module Inputs
    module Base
      module Choices

        def choice_html_safe_value(choice)
          choice_value(choice).hash
        end

      end # Choices
    end
  end
end

【讨论】:

    【解决方案2】:

    如果问题仅在创建复选框和单选按钮时出现,请尝试在创建 input 的属性 idlabel 的属性 for 时破解 formtastic,将字符从西里尔语转换为拉丁语。

    module Formtastic
      module Inputs
        module Base
          module Choices
    
            def choice_html_safe_value(choice)
              name = Translit.convert(choice_value(choice).to_s, :english)
              name.gsub(/\s/, '_').gsub(/[^\w-]/, '').downcase
            end
    
          end # Choices
        end
      end
    end
    

    您可以使用 gem translit 或自己制作转译。破解后,formtastic 会生成如下内容:

    <div class="radio">
      <label class="choice" for="post_flat_building_type_kirpichnyj">
        <input id="post_flat_building_type_kirpichnyj" name="post_flat[building_type]" type="radio" value="кирпичный">
        кирпичный
      </label>
    </div>
    

    【讨论】:

      【解决方案3】:

      使用简单数组,而不是数组数组

      [ "Flat-super", "Flat-super" ]
      

      【讨论】:

        猜你喜欢
        • 2015-03-12
        • 2021-09-14
        • 2018-11-07
        • 2018-10-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-10-24
        • 1970-01-01
        相关资源
        最近更新 更多