2012-07-19 81 views
0

我有導軌應用程序,它使用Rails 3.2.6 & Ruby 1.9.3p0版本。我有一些CSS & JS(使用JQuery)文件在資產管道中的資產文件夾。一切工作正常,但我從開發轉向生產與壓縮名爲然後application.css &的application.js我最jQuery代碼不工作單個文件。我嘗試了很多解決方案,但沒有任何幫助。從rails開發切換到生產3.2.6

請help.Thanks提前。

這是我application.html.erb

<!DOCTYPE html> 
<html> 
<head> 
<title>Tritinum <%#= controller.action_name %></title> 
<%= stylesheet_link_tag "application", :media => "all" %> 
<%= javascript_include_tag "application" %> 
<%= javascript_include_tag "http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.17/jquery-  ui.min.js", "jquery.rails.js" %> 
<%= stylesheet_link_tag 'http://fonts.googleapis.com/css?family=Droid+Sans:400,700'%> 

<%= csrf_meta_tags %> 
</head> 
<body> 
<!-- BEGIN MAIN --> 

<section id="main" class="container_16"> 
<p class="notice"><%#= notice %></p> 
    <p class="alert"><%= alert %></p> 


    <%= yield %> 


<!-- BEGIN FOOTER --> 
     <section class="footer"> 

     <p> 
<span class="left">�Copyright 2012 - All rights reserved. </span> 
    <span class="right">Web Tool v1.0 - Product By<a href="http://www.google.com">Personal.</a></span> 
     </p> 

     <div class="clear"></div><!-- CLEAR BOTH --> 

    </section><!-- end section .footer --> 
    <!-- END FOOTER --> 

     </section><!-- end section #main --> 
    <!-- END MAIN --> 

    <!-- BEGIN LOADING OVERLAY --> 
    <div id="loading_overlay"> 
    <div class="loading_message round_bottom"> 
     <%= image_tag "loading.gif" %> 
    </div> 
</div> 
<!-- END LOADING OVERLAY --> 
     </body> 
    </html> 

application.rb中

require File.expand_path('../boot', __FILE__) 

require 'rails/all' 

if defined?(Bundler) 
# If you precompile assets before deploying to production, use this line 
Bundler.require(*Rails.groups(:assets => %w(development test))) 
# If you want your assets lazily compiled in production, use this line 
# Bundler.require(:default, :assets, Rails.env) 
    end 

module Tritinum 
class Application < Rails::Application 
# Settings in config/environments/* take precedence over those specified here. 
# Application configuration should go into files in config/initializers 
# -- all .rb files in that directory are automatically loaded. 

# Custom directories with classes and modules you want to be autoloadable. 
# config.autoload_paths += %W(#{config.root}/extras) 

# Only load the plugins named here, in the order given (default is alphabetical). 
# :all can be used as a placeholder for all plugins not explicitly named. 
# config.plugins = [ :exception_notification, :ssl_requirement, :all ] 

# Activate observers that should always be running. 
# config.active_record.observers = :cacher, :garbage_collector, :forum_observer 

# Set Time.zone default to the specified zone and make Active Record auto-convert to this zone. 
# Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC. 
# config.time_zone = 'Central Time (US & Canada)' 

# The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded. 
# config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s] 
# config.i18n.default_locale = :de 

# Configure the default encoding used in templates for Ruby 1.9. 
config.encoding = "utf-8" 

# Configure sensitive parameters which will be filtered from the log file. 
config.filter_parameters += [:password] 

# Use SQL instead of Active Record's schema dumper when creating the database. 
# This is necessary if your schema can't be completely dumped by the schema dumper, 
# like if you have constraints or database-specific column types 
# config.active_record.schema_format = :sql 

# Enforce whitelist mode for mass assignment. 
# This will create an empty whitelist of attributes available for mass-assignment for all models 
# in your app. As such, your models will need to explicitly whitelist or blacklist accessible 
# parameters by using an attr_accessible or attr_protected declaration. 
config.active_record.whitelist_attributes = true 

# Enable the asset pipeline 
    config.assets.enabled = true 
    config.assets.initialize_on_precompile = false 
# config.assets.precompile = ['*.css'] 
# Version of your assets, change this if you want to expire all your assets 
    config.assets.version = '1.0' 
end 
end 

production.rb

Tritinum::Application.configure do 
# Settings specified here will take precedence over those in config/application.rb 

# Code is not reloaded between requests 
config.cache_classes = true 

# C hoose the compressors to use 
config.assets.js_compressor = :uglifier 
    config.assets.css_compressor = :yui 
    # Full error reports are disabled and caching is turned on 
    config.consider_all_requests_local  = false 
    config.action_controller.perform_caching = true 

# Disable Rails's static asset server (Apache or nginx will already do this) 
    config.serve_static_assets = false 

    # Compress JavaScripts and CSS 
    config.assets.compress = false 

    # Don't fallback to assets pipeline if a precompiled asset is missed 
    config.assets.compile = true 

    # Generate digests for assets URLs 
     config.assets.digest = true 

    # Defaults to Rails.root.join("public/assets") 
    # config.assets.manifest = YOUR_PATH 

    # Specifies the header that your server uses for sending files 
    # config.action_dispatch.x_sendfile_header = "X-Sendfile" # for apache 
    # config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect' # for nginx 

     # Force all access to the app over SSL, use Strict-Transport-Security, and use secure cookies. 
    # config.force_ssl = true 

     # See everything in the log (default is :info) 
     # config.log_level = :debug 

     # Prepend all log lines with the following tags 
    # config.log_tags = [ :subdomain, :uuid ] 

     # Use a different logger for distributed setups 
     # config.logger = ActiveSupport::TaggedLogging.new(SyslogLogger.new) 

     # Use a different cache store in production 
      # config.cache_store = :mem_cache_store 

      # Enable serving of images, stylesheets, and JavaScripts from an asset server 
      # config.action_controller.asset_host = "http://assets.example.com" 

     # Precompile additional assets (application.js, application.css, and all non-JS/CSS are already added) 
     # config.assets.precompile += %w(search.js) 

     # Disable delivery errors, bad email addresses will be ignored 
     # config.action_mailer.raise_delivery_errors = false 

     # Enable threaded mode 
       # config.threadsafe! 

     # Enable locale fallbacks for I18n (makes lookups for any locale fall back to 
     # the I18n.default_locale when a translation can not be found) 
     config.i18n.fallbacks = true 

    # Send deprecation notices to registered listeners 
     config.active_support.deprecation = :notify 

      # Log the query plan for queries taking more than this (works 
      # with SQLite, MySQL, and PostgreSQL) 
      # config.active_record.auto_explain_threshold_in_seconds = 0.5 
     end 

雖然,我開始軌服務器在生產後編譯用此命令

RAILS_ENV=production bundle exec rake assets:precompile 

我大部分的jQuery代碼停止工作。

的Gemfile

source 'https://rubygems.org' 

gem 'rails', '3.2.3' 
gem 'haml' 

# Bundle edge Rails instead: 
# gem 'rails', :git => 'git://github.com/rails/rails.git' 
gem 'wkhtmltopdf-binary' 
gem 'sqlite3' 
gem 'devise' 
gem 'cancan' 
gem 'faker' 
gem 'rb-readline' 
gem 'paperclip' 
gem 'wicked_pdf' 
gem "nested_form", :git => 'https://github.com/ryanb/nested_form.git' 



# Gems used only for assets and not required 
# in production environments by default. 
group :assets do 
gem 'sass-rails', '~> 3.2.3' 
gem 'coffee-rails', '~> 3.2.1' 

# See https://github.com/sstephenson/execjs#readme for more supported runtimes 
    # gem 'therubyracer', :platform => :ruby 

    gem 'uglifier', '>= 1.0.3' 
    end 

gem 'jquery-rails' 

# To use ActiveModel has_secure_password 
    # gem 'bcrypt-ruby', '~> 3.0.0' 

     # To use Jbuilder templates for JSON 
     # gem 'jbuilder' 

    # Use unicorn as the app server 
     # gem 'unicorn' 

     # Deploy with Capistrano 
     # gem 'capistrano' 

    # To use debugger 
     # gem 'ruby-debug19', :require => 'ruby-debug' 

應用程序/資產/ Java腳本/ 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 vendor/assets/javascripts of plugins, if any, 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 
// the compiled file. 
// 
// WARNING: THE FIRST BLANK LINE MARKS THE END OF WHAT'S TO BE PROCESSED, ANY BLANK LINE SHOULD 
// GO AFTER THE REQUIRES BELOW. 
// 
//= require jquery_nested_form 
//= require jquery 
//= require jquery_ujs 
//= require_tree . 
    function remove_fields(link) { 
    $(link).previous("input[type=hidden]").value = "1"; 
    $(link).up(".fields").hide(); 
    } 
    $(document).ready(function(){ 
    $("select#finding_category_id").change(function(){ 
     var id_value_string = $(this).val(); 
     if (id_value_string == "") { 
      // if the id is empty remove all the sub_selection options from being selectable and do not do any ajax 
      $("select#finding_finding_template_id option").remove(); 
      var row = "<option value=\"" + "" + "\">" + "" + "</option>"; 
      $(row).appendTo("select#finding_finding_template_id"); 
     } 
     else { 
      // Send the request and update sub category dropdown 
      $.ajax({ 
       dataType: "json", 
       cache: false, 
       url: '/finding_templates/for_categoryid/' + id_value_string, 
       error: function(XMLHttpRequest, errorTextStatus, error){ 
        alert("Failed to submit : "+ errorTextStatus+" ;"+error); 
       }, 
       success: function(data){      
        // Clear all options from sub category select 
        $("select#finding_finding_template_id option").remove(); 
        //put in a empty default line 
        var row = "<option value=\"" + "Select" + "\">" + "" + "</option>"; 
        $(row).appendTo("select#finding_finding_template_id");       
        // Fill sub category select 
        $.each(data, function(i, j){ 
         row = "<option value=\"" + j.finding_template.id + "\">" +   j.finding_template.name + "</option>"; 
         $(row).appendTo("select#finding_finding_template_id");      
        });    
       } 
      }); 
     }; 
      }); 
    }); 

請幫Somone的

+0

請顯示您的Gemfile – kr00lix 2012-07-19 06:01:14

+0

請檢查它。 – 2012-07-19 06:11:13

回答

2
config.assets.initialize_on_precompile = false 

^這條線在你application.rb是告訴預編譯功能,懶洋洋地預編譯。這意味着它只會在您的主應用程序中預編譯資產。如果將其設置爲true,則可以預編譯包含在其他引用的Gem中的資產。所以,除非你有jquery文件應用程序的assets文件夾明確,它可能得到錯過了預編譯 - 因此不包括在你的application.js

這行:config.assets.compile = true應該告訴你的應用程序回落到資產的管道,如果預編譯的資產錯過 - 理想,這將意味着它會發現從jquery的資產正常的管道。

但我的猜測是,由於技術上它找到application.js,你的應用程序不知道的是,已經錯過的資產,所以它加載預編譯application.js,然後就是沒有辦法就可以得到jquery代碼。

解決方案

對不起囉嗦的解釋,但請嘗試以下 - 在設置以下行的config/application.rb

config.assets.initialize_on_precompile 

true

,然後你會當然必須重建和重新部署:)讓我知道這是否有幫助!

+0

什麼意思**重建**在這裏。 – 2012-08-01 08:32:48

+0

通過重建我只是說你必須重新啓動(對不起,我使用maven打包WAR文件) 本質上,如果你在本地運行它,你需要重新啓動rails服務器,並且如果你正在運行它生產模式下,您需要創建一個新的WAR文件 – Krista 2012-08-10 16:02:44

0

寶石jquery-rails已經jquery-ui.min.js

你只需要添加這行到assets/javascript/application.js文件

//= require jquery //= require jquery_ujs

並且只有此行application.html.erb

<%= javascript_include_tag "application" %>

+0

所以我要從'application.html.erb'中刪除'jquery-ui.min.js'並將'// = require jquery // = require jquery_ujs'添加到'application.js' – 2012-07-19 06:26:18

+0

我已經將這些我的'application.js'我改變我的'appliction.html.erb'這行''%= javascript_include_tag「應用程序」%>' – 2012-07-19 06:36:05

+0

請給我提供解決這個問題的可行方法。 – 2012-07-20 12:55:55