簡體   English   中英

Rails:如何在 collection_select 中添加自定義數據屬性

[英]Rails: How to add custom data-attributes in collection_select

我正在研究使用 Rails 中的collection_select表單助手將自定義數據屬性添加到選項標簽的解決方案。

我研究了 stackoverflow 上的一些帖子,並在查閱了一些 API 文檔后進行了大量的反復試驗。 我快到了,但遺憾的是我的解決方案只將屬性添加到select -tag 而不是option -tags。

這樣我就填充了 html-options-hash(第 6 位):

<%= f.collection_select(:parallax_id, @parallax.all, :id, :title, {}, { :"data-icon" => @parallax.map{ |p| "#{p.image}"}} ) %>

這會產生一個選擇標簽,如:

<select data-icon="/uploads/image/image/4/169_strecken-ausgang.jpg" name="game[parallax_id]" id="game_parallax_id">...</select>

但我想要獲取data-icon屬性的選項。 當我切換位置並將我的數據圖標添加到選項哈希(第 5 個位置)時,沒有任何輸出。

這是你想要的嗎?

= f.select :parallax_id, options_for_select(@parallax.map {|p| [p.title, p.id, {'data-icon' => p.image }]})

僅供參考,我正在研究這個問題,在這里找到了更合適的解決方案: How do I set the HTML options for collection_select in Rails? .

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM