您可以在rails的自定义javascript文件中使用嵌入式ruby吗? [英] can you use embedded ruby in custom javascript files in rails?

查看:108
本文介绍了您可以在rails的自定义javascript文件中使用嵌入式ruby吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将红宝石代码嵌入javascript的Rails中?我知道您可以在javascript响应器中轻松地执行操作方法(例如,在create.erb.js中).但是,是否可以使用javascript_include_tag对应用程序中包含的其他自定义javascript文件执行此操作?

How does one embed ruby code in javascript for rails? I know you can do this easily in javascript responders for action methods (for e.g. in create.erb.js). But is there a way to do this for other custom javascript files that are included in the application using javascript_include_tag?

例如在create.erb.js中,我可以编写<%=这样的代码,如果current_user_name ="xyz"%>

For e.g. in create.erb.js I can write code such as <%= if current_user_name ="xyz" %>

有没有一种方法可以在自定义js文件(例如custom.js)中嵌入此类代码,该文件可在整个应用程序中使用,而不仅仅是生成format.js的响应.

Is there a way to have this type of embedding in a custom js file such as custom.js that is used across the application and not just for generating a response for format.js.

谢谢

塔布雷斯

推荐答案

您可以在views/shared文件夹中创建一个_application.js.erb文件(随意命名),然后将其包含在应用程序的布局文件中. /p>

You can just create an _application.js.erb file (name it whatever you wish) in the views/shared folder and then include it in your application's layout file.

<%= render '/shared/application.js' %>

确保像平常一样将其包装在script标签中,并确保像通常一样在该JS文件中使用DOM ready包装器.

Make sure you wrap it in script tags like normal and ensure that you use the DOM ready wrapper within that JS file like you normally would as well.

这篇关于您可以在rails的自定义javascript文件中使用嵌入式ruby吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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