如何加载特定于页面的 rails 4 js 文件? [英] How to load page specific rails 4 js files?

查看:34
本文介绍了如何加载特定于页面的 rails 4 js 文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读有关资产管道的 Rails 指南文档.它指出,coffeescript 页面特定生成的文件如果清单上有 require_tree 指令,则默认情况下已准备好供用户使用.这对我不起作用,我必须包括这个

I am reading rails guides documentation for asset pipeline. It states that coffeescript page specific generated files are by default ready to user if there is a require_tree directive on the manifest. This is not working with me I have to do include this

<%= javascript_include_tag params[:controller] %>

在特定控制器上.我错过了什么?

on the specific controller. What am I missing ?

推荐答案

资产管道会将您的所有 JS 压缩到一个文件 application.js 中.为了为特定页面调用 JS,您需要通过控制器和操作来组织您的 JS.有一个 gem,RailsS​​cript 可以自动执行此操作,并且它与 Turbolinks 兼容,可以为您提供单页应用程序的感觉.

The asset pipeline will compress all of your JS into a single file, application.js. In order to call JS for a specific page, you will need to organize your JS by controller and action. There is a gem, RailsScript that does this automatically and it's compatible with Turbolinks which can give you a single page application feel.

RailsS​​cript 只需几分钟即可学会,https://github.com/gemgento/rails_script.

RailsScript only takes a few minutes to learn, https://github.com/gemgento/rails_script.

使用 rails 脚本的具体示例:

A specific example using rails script:

# app/assets/javascripts/users.js.coffee

window.App ||= {}
class App.Users extends App.Base

   show: ->
      alert('The users#show action!')

这篇关于如何加载特定于页面的 rails 4 js 文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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