【问题标题】:Use image as radio button in rails在rails中使用图像作为单选按钮
【发布时间】:2017-10-07 02:21:57
【问题描述】:

如何在 rails 中使用本地图像作为单选按钮?

我猜想可以使用 collection_radio_buttons 帮助器,但是如何集成本地图像?

这个 sn-p 显示带有以下文本的单选按钮:

    <%= f.collection_radio_buttons(:paket, [[1, 'One'] ,[2, 'Two'], [3, 'Three']], :first, :last) do |b| %>
      <%= b.label { b.radio_button + b.text } %>
    <% end %>

我是否必须将 b.radio_button 替换为 image_tag

【问题讨论】:

  • 检查我的解决方案

标签: ruby-on-rails image radio-button


【解决方案1】:

是的,你可以使用 image_tag 代替 b.text 来显示图像而不是文本

#I have image in app/assets/images   as One.png,Two.png and so on.

<%= f.collection_radio_buttons(:paket, [[1, 'One'] ,[2, 'Two'], [3, 'Three']], :first, :last) do |b| %>
  <%= b.label { b.radio_button + image_tag('/'+b.text+'.png') } %>
<% end %>

【讨论】:

  • 感谢您的回复。它可以工作,但现在所有单选按钮都有相同的图像。如何为每个单选按钮获取单独的图像?
  • 应用/资产/图像
  • 有哪些名字??
  • sample1.png, sample2.png, sample3.png
  • 重命名图像以匹配标签,即 One.png、Two.png。等等。我更新了代码检查。
猜你喜欢
  • 2014-08-26
  • 2013-09-25
  • 2013-09-03
  • 2020-04-28
  • 1970-01-01
  • 2012-12-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多