2012-02-27 141 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