8
posts.js.coffee.erb紅寶石內的CoffeeScript
$('.list').infinitescroll {url: '<%= list_posts_path %>', triggerAt: 700, container: $('.container'), appendTo: $('.container'), page: 1}
這使得一個例外:
throw Error("NameError: undefined local variable or method `list_posts_path' for #<#:0x00000003557438>\n ...
list_posts_path的回報,如果我使用的控制器正確的路徑。我做錯了什麼?
有沒有辦法如何獲得js的路徑? – Jonas
@Jonas爲JS提供服務器端數據的最佳方式是在頁面中(通過視圖或佈局)放置一個'
另一種選擇是設置一個數據屬性持有一些相關元素在你的路徑...然後用JS /咖啡代碼抓住它
來源
2012-08-06 22:47:05 lunaclaire
包括
在CoffeeScript的文件的開頭然後你就可以訪問路線
來源
2013-07-31 07:48:25 Rahul
我有同樣的問題,並儘早提到的,你可以這樣做:
your_layout.html.erb
<%= render partial: 'your_partial.html.erb', locals: { action_url: list_posts_path } %>
_your_partial.html.erb
<div id='container' data-action-url="<%= action_url %>" .... >
posts.js.coffee.erb
jQuery -> url = $('#container').data('action-url') console.log "loading url: #{url} !!!"
來源
2014-03-13 04:16:09 evbruno