使用Rails在JavaScript响应中渲染Haml文件 [英] Rendering Haml file in a javascript response with rails

查看:97
本文介绍了使用Rails在JavaScript响应中渲染Haml文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在javascript响应中渲染haml文件,如下所示:

I am trying to render a haml file in a javascript response like so:

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

要呈现的文件的示例是:

An example of the file that would render is:

- 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,现在对我来说会出现两个问题:

Now two problems occur for me if we look at the javascript that is given in the response:

$('#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可以解决此问题,但是不能解决这个问题.

The major problem is that the response has newlines in it. This breaks the request. I presumed using j in front of my render call would fix that, but it doesn't.

另一个问题是在haml文件的第三行上:

The other problem is that on the third line of the haml file I have:

#{@user.first_name}'s assessment

,并且单引号也中断了请求. (我知道这是因为我使用了javascript函数来删除所有新行,并且在我也删除了单引号之前,请求仍然中断了)

and that apostrophe also breaks the request. (I know this because I used a javascript function to delete all the new lines and the request was still broken until I took out that apostrophe as well)

有没有比链接javascript函数更简单的方法来清理javascript响应了?

Is there a simpler way to clean up the javascript response than chaining on javascript functions to clean it up for me?

推荐答案

我遇到了类似的问题.存在问题是因为'escape_javascript'和'json_escape'这两种方法都被别名为'j'(https://github.com/rails/rails/pull/3578).

I experienced a similar problem. Problem exists because both methods, 'escape_javascript' and 'json_escape' are aliased as 'j' (https://github.com/rails/rails/pull/3578).

解决方案: 使用"escape_javascript"代替"j".

Solution: Use 'escape_javascript' instead of 'j'.

这篇关于使用Rails在JavaScript响应中渲染Haml文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆