【问题标题】:Ruby on Rails 4: Trouble with adding Javascript files in Rails web applicationRuby on Rails 4:在 Rails Web 应用程序中添加 Javascript 文件时遇到问题
【发布时间】:2016-04-25 22:04:19
【问题描述】:

我在将 javascript 添加到我的 rails 应用程序时遇到问题。我已将 javascript 直接添加到我的 application.html.erb 文件中,它只适用于文件。这是下面的脚本:

//Javascript//

$( "#accordion" ).accordion();



var availableTags = [

];
$( "#autocomplete" ).autocomplete({
    source: availableTags
});



$( "#button" ).button();
$( "#radioset" ).buttonset();



$( "#tabs" ).tabs();



$( "#dialog" ).dialog({
    autoOpen: false,
    width: 400,
    buttons: [
        {
            text: "Ok",
            click: function() {
                $( this ).dialog( "close" );
            }
        },
        {
            text: "Cancel",
            click: function() {
                $( this ).dialog( "close" );
            }
        }
    ]
});

// Link to open the dialog
$( "#dialog-link" ).click(function( event ) {
    $( "#dialog" ).dialog( "open" );
    event.preventDefault();
});



$( "#datepicker" ).datepicker({
    inline: true
});



$( "#slider" ).slider({
    range: true,
    values: [ 17, 67 ]
});



$( "#progressbar" ).progressbar({
    value: 20
});



$( "#spinner" ).spinner();



$( "#menu" ).menu();



$( "#tooltip" ).tooltip();



$( "#selectmenu" ).selectmenu();


// Hover states on the static widgets
$( "#dialog-link, #icons li" ).hover(
    function() {
        $( this ).addClass( "ui-state-hover" );
    },
    function() {
        $( this ).removeClass( "ui-state-hover" );
    }
);

这在我的 application.html.erb 文件中被 <script> 标签包裹,没有任何问题。一旦我创建了一个名为 theme.js 的文件并将其放入“/vendor/assets/javascripts”并更新了我的 application.js 文件:

// This is a manifest file that'll be compiled into application.js, which will include all the files
// listed below.
//
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
// or any plugin's vendor/assets/javascripts directory can be referenced here using a relative path.
//
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
// compiled file.
//
// Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details
// about supported directives.
//
//= require jquery
//= require jquery_ujs
//= require_tree .
//= require theme

它停止工作。我也尝试将 theme.js 放在“app/assets/javascripts”中,但也没有用。

这是 application.html.erb 文件的头部:

<!DOCTYPE html>
<html class="bootstrap-layout">
<head>
  <title>App</title>

  <!-- Material Design Icons  -->
  <link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
  <!-- Roboto Web Font -->
  <link href="https://fonts.googleapis.com/css?family=Roboto:regular,bold,italic,thin,light,bolditalic,black,medium&amp;lang=en" rel="stylesheet">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.6.1/css/font-awesome.min.css">



  <%= stylesheet_link_tag    'application', media: 'all', 'data-turbolinks-track' => true %>
  <%= javascript_include_tag 'application', "data-turbolinks-track" => true %>
  <%= javascript_include_tag 'https://code.jquery.com/jquery-2.2.3.js' %> 
  <%= javascript_include_tag 'https://code.jquery.com/ui/1.11.4/jquery-ui.js' %>

  <%= csrf_meta_tags %>
</head>

我确定这是我缺少的基本内容,我只是不知道它是什么。我还删除了认为这可能是问题的 turbolinks,但是删除它并没有做任何事情。

【问题讨论】:

标签: javascript jquery html css ruby-on-rails


【解决方案1】:

由于它不是一个库,而是一组自定义 js,因此您应该将其放回您拥有它的 app/assets/theme.js 中,而不是您的供应商 js 文件夹中。然后,您不必特别要求它,因为在您的 application.js 文件中,您有“require tree”语句,它将自动包含您的 assets/js 文件夹中的所有文件。

另外,将所有这些都包含在一个

中通常是个好主意
$( document ).ready(function() {
    all your js here...
});

可能是您的 js 在页面之前加载,因为您将其移入资产管道,此时浏览器尚未编译 DOM,导致所有 js 损坏。

【讨论】:

猜你喜欢
  • 2014-04-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-25
  • 2016-10-05
  • 1970-01-01
  • 2014-04-16
相关资源
最近更新 更多