我想從視圖中獲取文章ID。如何將rails數據傳遞給JS或Coffeescript?
<%= f.text_area :content, rows: 5, placeholder: 'input....', class: 'form-control',
id: "tags", data: {article_id: @article.id } %>
然後傳遞到的article_id CoffeeScript的:
$ ->
id = $("#tags").data('article_id')
console.log "test-----------#{id}"
$("#tags").autocomplete(
source: '/articles/' + id + '/autocomplete.json')
但輸出是:
test-----------undefined
如何軌數據傳遞給JS或CoffeeScript的?有多少種方法可以做到這一點?
在此先感謝!
爲@muistooshort和@JoelL說:data: {article_id: @article.id} will produce data-article-id="xxxx"
HTML輸出:
<textarea rows="5" placeholder="input..." class="form-control" id="tags" data-article-id="{"$oid":"560017ddbd172d16d6000001"}" name="comment[content]">
,但我從瀏覽器控制檯日誌中的信息:
test----------[object Object]
它不是一個字符串。
試試'$(「#tags」)。data('article-id')'。我認爲rails將'_'轉換爲'-'以獲取數據屬性 – j03w
您是否嘗試過查看HTML以查看生成的內容? –
@muistooshort'