簡體   English   中英

使用Rails在JavaScript響應中渲染Haml文件

[英]Rendering Haml file in a javascript response with rails

我正在嘗試在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)

現在,如果我們看一下響應中給出的javascript,對我來說會出現兩個問題:

$('#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
');

主要的問題是響應中包含換行符。 這中斷了請求。 我想在我的渲染調用之前使用j可以解決此問題,但事實並非如此。

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

#{@user.first_name}'s assessment

並且該撇號也破壞了請求。 (我知道這一點,因為我使用了javascript函數來刪除所有新行,並且請求也仍然中斷,直到我也取出了撇號為止)

有沒有比鏈接javascript函數更簡單的方法來清理javascript響應了?

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

解決方案:使用“ escape_javascript”代替“ j”。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM