[英]How do I add an attribute to a rails select dropdown?
好吧,所以我正在嘗試在軌道上的一些ajax並且擊中了一個巨大的磚牆:我無法在我的選擇框中設置data-remote
屬性。 無論我嘗試什么形式,它總是表現得好像告訴它關於該屬性的部分甚至不存在。 例:
<%= f.select(:image, options_from_collection_for_select(@images, 'id', 'name'), data: {remote: true}) %>
<%= f.select( [...] , :'data-remote' => 'true') %>
<%= f.select( [...] , :data => {remote: true}) %>
<%= f.select( [...] , data: {remote: true}) %>
<%= f.select( [...] , remote: true) %>
我甚至有工作代碼在其他事情上實現前三個,但不是那個f.select
那么有誰知道我如何正確地將屬性應用於f.select
?
data
鍵需要位於HTML選項哈希中,這是要select
的第三個參數。 從文檔:
select(method, choices, options = {}, html_options = {})
因此你需要:
f.select(:image, options_from_collection_for_select(@images, 'id', 'name'), {}, { data: {remote: true} })
注意空選項哈希。
嘗試這個:
<%= f.select :image, options_for_select(@images.map{ |i| [i.name, i.id, {'data-remote'=>true}] }) %>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.