使用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"answers"json_eescape"这两个方法都别名为"j"(https://github.com/rails/rails/pull/3578)。
解决方案:使用"escape_javascript"而不是"j"。
相关文章:
- 如何播放部分音频文件
- 从桌面读取python文件时高亮显示代码
- 使用JS如何动态更改显示的html文件中的文本背景颜色
- 如何使用WCF服务和javascript表单post上传.doc文件
- 当包含另一个asp文件时,是否也包含所有引用的样式和脚本页面
- 如何在生成下载文件时显示加载动画
- 有没有任何方法可以将控制器从文件加载到ui路由器$stateProvider中
- Javascript-如何读取json文件中的列并将其保存在Javascript数组中
- 将jsp文件下拉列表中的选定项分配给一个java变量(比如String selection)
- 正在SharePoint 2013母版页中添加JQuery移动文件
- 如何在php文件中获取$.post-ajax传递的值
- 直接下载文件,而不是从window.open(url)
- 如何在android中使用phonegap将文件从一个文件夹移动/复制到另一个文件夹
- 使用压缩的JavaScript文件(不是运行时压缩)
- 在使用Polymer'加载所有json文件后执行方法;s的核心ajax
- 如何在elfinder插件(一个文件管理器插件)上获得上传前事件
- Javascript运行php文件,然后下载文件
- 如何在js.haml文件中编写外部.js文件的内容(RubyonRails)
- 使用rails在javascript响应中呈现Haml文件
- 在将haml文件转换为html时复制整个文件夹结构(使用grunt)