2012-02-27 5 views
0

私はそうのようなjavascriptの応答にHAMLファイルをレンダリングしようとしています:レンダリング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を使ってそれを修正すると推定していましたが、そうはしません。

#{@user.first_name}'s assessment 

とそのアポストロフィもリクエストを破る:

他の問題は、HAMLファイルの3行目に、私が持っているということです。 (私はこれを知っているので、私はすべての新しい行を削除するために、javascript関数を使用して、要求はまだアポストロフィを取り出して壊れていた)

javascriptのチェーンよりも簡単な方法は、私のためにそれをきれいにする機能?

答えて

3

同様の問題が発生しました。 「escape_javascript」と「json_escape」の両方のメソッドが「j」(https://github.com/rails/rails/pull/3578)としてエイリアスされているため、問題が存在します。

解決策: 「j」の代わりに「escape_javascript」を使用してください。

+0

実際に問題になるでしょう。ありがとう! – dchapman