将背景图像或图像标签添加到具有集合的 Formtastic 单选按钮

发布于 2024-12-02 15:39:56 字数 1443 浏览 1 评论 0原文

我在用户输入信用卡数据的表单中有这个格式字段:

<%= form.input :credit_card_type, :as => :radio, :collection => User.credit_card_types %>

User.credit_card_types 看起来像这样:

  def self.credit_card_types
    {'Visa' => VISA, 'Master Card' => MASTER_CARD, 'American Express' => AMERICAN_EXPRESS}
  end

这给了我以下 html:

<li id="user_credit_card_type_input" class="radio optional">
  <fieldset>
    <legend class="label">
      <label>Tipo de tarjeta</label>
    </legend>
    <ol>
      <li>
        <label for="user_credit_card_type_1">
          <input type="radio" value="1" name="user[credit_card_type]" id="user_credit_card_type_1"> Master Card
        </label>
      </li>
      <li>
        <label for="user_credit_card_type_0">
          <input type="radio" value="0" name="user[credit_card_type]" id="user_credit_card_type_0"> Visa</label>
      </li>

      <li>
        <label for="user_credit_card_type_2">
          <input type="radio" value="2" name="user[credit_card_type]" id="user_credit_card_type_2"> American Express</label>
      </li>
    </ol>
  </fieldset>
</li>

鉴于此,我的问题是,如何向每个单选按钮添加特定的背景图像标签?我不能用 CSS 做到这一点,因为标签没有任何 id,而且我不能(或者我不知道如何)添加一个。我尝试添加 image_tag 但它转义了 html。有什么建议吗?

I have this formtastic field in a form where an user input his credit card data:

<%= form.input :credit_card_type, :as => :radio, :collection => User.credit_card_types %>

User.credit_card_types look like this:

  def self.credit_card_types
    {'Visa' => VISA, 'Master Card' => MASTER_CARD, 'American Express' => AMERICAN_EXPRESS}
  end

Which give me the following html:

<li id="user_credit_card_type_input" class="radio optional">
  <fieldset>
    <legend class="label">
      <label>Tipo de tarjeta</label>
    </legend>
    <ol>
      <li>
        <label for="user_credit_card_type_1">
          <input type="radio" value="1" name="user[credit_card_type]" id="user_credit_card_type_1"> Master Card
        </label>
      </li>
      <li>
        <label for="user_credit_card_type_0">
          <input type="radio" value="0" name="user[credit_card_type]" id="user_credit_card_type_0"> Visa</label>
      </li>

      <li>
        <label for="user_credit_card_type_2">
          <input type="radio" value="2" name="user[credit_card_type]" id="user_credit_card_type_2"> American Express</label>
      </li>
    </ol>
  </fieldset>
</li>

Given that, my question is, how can I add an specific background image to each radio button label? I can't do it with CSS because the labels have not any id and I can't (or I don't know how) put one to then. I tried to add an image_tag but it escapes the html. Any suggestion ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

粉红×色少女 2024-12-09 15:39:56

Formtastic 可根据您的需求进行定制。您应该能够在 config/initializers 中找到 Formtastic 配置文件。添加如下行:

Formtastic::SemanticFormHelper.builder = SemanticCustomFormBuilder

然后在 lib 目录中创建一个名为 Semantic_custom_form_builder.rb 的文件

class SemanticCustomFormBuilder < Formtastic::SemanticFormBuilder

  def cicloon_special_radio_button_input
    #define your radiobuttons here
  end

end

您可以在表单中使用自定义定义,如下所示:

<%= form.input :credit_card_type, :as => :cicloon_special_radio_button %>

Formtastic can be customized for your needs. You should be able to find Formtastic configuration file in config/initializers. Add a line like this:

Formtastic::SemanticFormHelper.builder = SemanticCustomFormBuilder

Then create in your lib directory a file named semantic_custom_form_builder.rb

class SemanticCustomFormBuilder < Formtastic::SemanticFormBuilder

  def cicloon_special_radio_button_input
    #define your radiobuttons here
  end

end

You can use your custom definition like this in your forms:

<%= form.input :credit_card_type, :as => :cicloon_special_radio_button %>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文