2012-02-27 105 views
0

我試圖渲染HAML文件中像這樣一個javascript響應:渲染Haml的文件

$('#<%= @email.unique_name %> .preview .mail_content').html('<%=j render(:file => "user_mailer/#{@email.key}") %>'); 

這將使該文件的一個例子是:

- variables = { :contact_first_name => @contact.first_name, :user_full_name => @user.name, :user_first_name => @user.first_name } 

= @email.intro_html(variables) 

%p= "Please click the link below to go directly to the results of #{@user.first_name}'s assessment. You can also access an analysis of that assessment from that page." 

%p= share_results_url(@token) 

= @email.conclusion_html(variables) 

$('#launch_share_results .preview .mail_content').html('\u003Cp\u003EHi Jane,\u003C/p\u003E 
\u003Cp\u003EJohn Smith has taken a 360(deg) \u003Cspan style=color:red;\u003E\u003Cstrong\u003ENo such variable available!\u003C/strong\u003E\u003C/span\u003E assessment through myLAUNCHtools.com and would like to share the results with you.\u003C/p\u003E 
\u003Cp\u003EPlease click the link below to go directly to the results of John's assessment. You can also access an analysis of that assessment from that page.\u003C/p\u003E 
\u003Cp\u003Ehttp://lvh.me:3000/assessments/results/1\u003C/p\u003E 
\u003Cp\u003EThank you in advance for your time and interest in John\u0026#8217;s leadership.\u003C/p\u003E 
\u003Cp\u003ESincerely,\u003Cbr /\u003E 
Launch\u003C/p\u003E 
'); 

主要的:如果我們看看這是在響應中給出的JavaScript

現在對我來說會出現兩個問題問題在於響應中有新行。這打破了請求。我假設在我的渲染調用前使用j會解決這個問題,但事實並非如此。

的另一個問題是,在HAML文件的第三行,我有:

#{@user.first_name}'s assessment 

和撇號也打破了請求。 (我知道這是因爲我用JavaScript函數刪除所有新行,並且請求仍然被打破,直到我拿出撇號爲止)

有沒有簡單的方法來清理JavaScript響應而不是鏈接在JavaScript上函數爲我清理它?

回答

3

我遇到過類似的問題。存在問題是因爲'escape_javascript'和'json_escape'這兩個方法的別名都是'j'(https://github.com/rails/rails/pull/3578)。

解決方案: 使用'escape_javascript'而不是'j'。

+0

這確實是問題所在。謝謝! – dchapman 2012-02-29 14:54:27