2016-09-23 100 views
0

我想在發生事件時使用coffeescript到.append部分。問題是,當發生.change事件時,下面的代碼不會呈現部分內容,而是<%= j render(:partial => 'participant_data').html_safe %>在網站上顯示爲文本。Rails:在.coffee文件中渲染部分內容

$('input[type=number][id=quantity-select]').change -> 
    $('#insert-here').append('<%= j render(:partial => \'participant_data\') %>') 
    return 

當我運行$('#insert-here').append("Test")Test顯示在頁面上。所以我的目標是正確的div,並能夠append文字,但我想呈現部分而不是文字。

請讓我知道我需要如何改變代碼,使部分呈現。

+0

我沒有咖啡的工作,但js.erb看起來不錯。你能向我們展示你的咖啡文件和控制器嗎? – nobilik

回答

0

請繼續嘗試。

$('#insert-here').append("#{escape_javascript(render partial: 'participant_data')}") 

更新回答:試用單引號。

$('#insert-here').append('<%= j render(:partial => \'participant_data\') %>') 

參考:

How to escape while rendering a js.coffee file in response to an action

+0

當我使用此代碼時,我收到以下錯誤消息:'ReferenceError:escape_javascript未定義' –

+0

感謝您的更新。當我運行更新後的代碼時,出現以下錯誤:ExecJS :: RuntimeError SyntaxError:[stdin]:50:57:unexpected identifier' –