rails 3上的Rake任务 - 找不到配置

时间:2011-01-27 21:58:39

标签: ruby-on-rails rake task

发布于此:Rake on Rails 3 problem

我正在尝试将在rails 2下工作的rake任务复制到rails 3 app。

rake任务如下:

namespace :cached_assets do
  desc "Regenerate aggregate/cached files"
  task :regenerate => :environment do
    include ActionView::Helpers::TagHelper
    include ActionView::Helpers::UrlHelper
    include ActionView::Helpers::AssetTagHelper
    stylesheet_link_tag :all, :cache => CACHE_CSS_JS
    javascript_include_tag  "a.js", "b.js", 
              "c.js", :defaults, :cache => CACHE_CSS_JS
    javascript_include_tag  "q.js", "w.js", 
               "e.js", :cache => 'abc'
  end
end

在Rails 2上它正确地缓存资产,在rails 3上我有以下错误:

rake cached_assets:regenerate --trace
(in /var/www/apps/****)
** Invoke cached_assets:regenerate (first_time)
** Invoke environment (first_time)
** Execute environment
** Execute cached_assets:regenerate
rake aborted!
undefined local variable or method `config' for #<Object:0xa86290>
/opt/ruby-enterprise/lib/ruby/gems/1.8/gems/actionpack-3.0.3/lib/action_view/helpers/asset_tag_helper.rb:498:in `stylesheet_link_tag'
/var/www/apps/****/lib/tasks/cached_assets.rake:10
/opt/ruby-enterprise/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:636:in `call'
/opt/ruby-enterprise/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:636:in `execute'

查看stylesheet_link_tag的定义,我看到:

 def stylesheet_link_tag(*sources)
         options = sources.extract_options!.stringify_keys
         concat  = options.delete("concat")
         cache   = concat || options.delete("cache")
         recursive = options.delete("recursive")

         if concat || (config.perform_caching && cache)
           joined_stylesheet_name = (cache == true ? "all" : cache) + ".css"
           joined_stylesheet_path = File.join(joined_stylesheet_name ......
             ..... ....

我的config / application.rb:

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

require 'rails/all'

# If you have a Gemfile, require the gems listed there, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(:default, Rails.env) if defined?(Bundler)

module ****
  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

    # JavaScript files you want as :defaults (application.js is always included).
    # config.action_view.javascript_expansions[:defaults] = %w(jquery rails)

    # 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]

    config.time_zone = 'UTC'

    # 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}')]
    # config.i18n.default_locale = :de

    # use memcache
    config.cache_store = :mem_cache_store, 'localhost:11211', { :namespace => 'nar_' }

    config.after_initialize do
      require 'lib/core_extensions.rb'
    end

  end
end

我的config / environments / development.rb:

****::Application.configure do


  # Settings specified here will take precedence over those in config/environment.rb

# In the development environment your application's code is reloaded on
# every request.  This slows down response time but is perfect for development
# since you don't have to restart the webserver when you make code changes.
config.cache_classes = false

# Log error messages when you accidentally call methods on nil.
config.whiny_nils = true

# Show full error reports and disable caching
config.action_controller.consider_all_requests_local = true
config.action_view.debug_rjs                         = true
config.action_controller.perform_caching             = false

# EMAIL -> see also initializers/emailer_initializers.rb and /config/email.yml !!!!!!!!!!!!
EMAIL_ENABLED = false
config.action_mailer.raise_delivery_errors = true
MAIL_FROM = '****'
RCPT_TO_DEV = '****'

ENV['RAILS_ASSET_ID'] = '100'

PRODUCTION_DOMAIN_NAME = "*****"

#CSS CACHING
CACHE_CSS_JS = false


  # Print deprecation notices to the Rails logger
  config.active_support.deprecation = :log

  # Only use best-standards-support built into browsers
  config.action_dispatch.best_standards_support = :builtin
end

1 个答案:

答案 0 :(得分:0)

我遇到了同样的问题,并在此处找到了解决方案:http://railsforum.com/viewtopic.php?id=42282

只需添加前缀

stylesheet_link_tag

javascript_include_tag

ApplicationController.helpers.

那么你甚至不需要包含ActionView助手。