将背景图像或图像标签添加到具有集合的 Formtastic 单选按钮
我在用户输入信用卡数据的表单中有这个格式字段:
<%= 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Formtastic 可根据您的需求进行定制。您应该能够在 config/initializers 中找到 Formtastic 配置文件。添加如下行:
然后在 lib 目录中创建一个名为 Semantic_custom_form_builder.rb 的文件
您可以在表单中使用自定义定义,如下所示:
Formtastic can be customized for your needs. You should be able to find Formtastic configuration file in config/initializers. Add a line like this:
Then create in your lib directory a file named semantic_custom_form_builder.rb
You can use your custom definition like this in your forms: