In my rails 6 app running ruby 2.7, I am trying to eager load all the files using Zeitwerk. Here is a snippet of my application.rb file:
class Application < Rails::Application
config.load_defaults "6.0"
Zeitwerk::Loader.eager_load_all
# Application configuration can go into files in config/initializers
# -- all .rb files in that directory are automatically loaded after loading
# the framework and any gems in your application.
# Middlewares
config.middleware.insert 0, Rack::UTF8Sanitizer
config.middleware.use Rack::Attack
config.autoload_paths += Dir["#{config.root}/lib/**/*"]
config.autoload_paths += Dir["#{config.root}/app/models/**/*"]
config.autoload_paths += Dir["#{config.root}/app/observers/*"]
config.autoload_paths += Dir["#{config.root}/app/middleware/*"]
config.autoload_paths += Dir["#{config.root}/app/validators/**/*"]
config.autoload_paths += Dir["#{config.root}/app/**/concerns/*"]
config.autoload_paths += Dir["#{config.root}/app/operations/**/*"]
config.autoload_paths += Dir["#{config.root}/app/scrubbers/**/*"]
config.autoload_paths += Dir["#{config.root}/app/workers/**/*"]
config.paths.add "#{config.root}/app/operations/", eager_load: true
# 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 = "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('config', 'locales', '**', '*.{rb,yml}')].reject { |f| File.fnmatch("#{Rails.root.to_s}/config/locales/**/x_*.yml", f) }
config.i18n.available_locales = [:"en", :"fr", :"fr_en"]
config.i18n.default_locale = :en
# Precompile additional assets.
# application.js, application.css, and all non-JS/CSS in app/assets folder are already added.
config.assets.precompile += ["public.css", "public.js", "public_payments.js", "email.css", "pdf.css", "pronotif.css"]
config.middleware.insert_before 0, Rack::Cors do
allow do
origins '*'
resource '*', :headers => :any, :methods => [:get, :post, :options]
end
end
end
However, when I run this I get this error:
NoMethodError: undefined method `eager_load_paths' for #<I18n::Config:0x00007fceade8c0f0>
Any solution for this will be helpful
Problem
Aside from the fact that your application.rb is not in a good shape (as #max mentioned in the first comment to your question) the problem is known and already fixed for Rails.
Unfortunately (as time of writing, with Rails 6.0.2.2) the fix is currently not public.
Temporary Workaround
But until then you might copy the patched rake task to your app under a file named lib/tasks/local_zeitwerk.rake and change line 41 to:
namespace :local_zeitwerk do
and then execute the check task with
bin/rails local_zeitwerk:check
Related
I recently updated a few packages in my Rails application and now my assets aren't being served. Instead, I get the following error:
Failed to load resource: the server responded with a status of 404 (Not Found)
My assets are precompiled:
assets.rb
# Be sure to restart your server when you modify this file.
# Version of your assets, change this if you want to expire all your assets.
Rails.application.config.assets.version = '1.0'
# Add additional assets to the asset load path
# Rails.application.config.assets.paths << Emoji.images_path
# Precompile additional assets.
# application.js, application.css, and all non-JS/CSS in app/assets folder are already added.
# Rails.application.config.assets.precompile += %w( search.js )
Rails.application.config.assets.precompile += %w(ckeditor/*)
Rails.application.config.assets.precompile += %w(ckeditor/config.js)
Rails.application.config.assets.precompile += %w( *.js ^[^_]*.css *.css.erb )
application.rb
require_relative 'boot'
require 'rails/all'
# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(*Rails.groups)
module DeployTest
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.
config.assets.precompile += Ckeditor.assets
config.assets.precompile += %w( ckeditor/* )
config.autoload_paths += %W(#{config.root}/app/models/ckeditor)
config.active_record.default_timezone = :local
config.time_zone = 'Eastern Time (US & Canada)'
end
end
Before telling me to turn on compiling my assets, please understand that this is
a horrible idea. Thank you for any advice
UPDATE: I got it to work by adding:
config.assets.digest = true
to my config/environments/staging.rb file. Weird how I didn't need it before
At times you need to add both of these configurations in staging.rb or whichever environment you want the changes to reflect on.
config.assets.compile = true #set to false by default
config.assets.digest = true
I am a noob in Ruby/Rails and I am creating my first project now.
I am using Devise gem for authentication system. I have installed it and I am on fight to change the default messages from "en" (default language) to "pt-BR".
I have a file called devise.pt-BR.yml inside /config/locales/ with all translations for this language and I have followed a few tips but when I restart the server, I still get "en" as the default language.
I don't want to have two or more languages, I just need to work with "pt-BR" instead "en".
My environment:
Fedora 16
Ruby 1.9.2p320
Rails 3.2.6
Devise 2.1.2
/config/application.rb content (look at bottom):
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 Foo
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]
# Enable escaping HTML in JSON.
config.active_support.escape_html_entities_in_json = true
# 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
# Version of your assets, change this if you want to expire all your assets
config.assets.version = '1.0'
config.i18n.load_path += Dir[Rails.root.join('devise', 'locales', '*.{rb,yml}').to_s]
config.i18n.default_locale = :"pt-BR"
I18n.locale = :"pt-BR"
end
end
Change the line
config.i18n.default_locale = :"pt-BR"
to
config.i18n.default_locale = "pt-BR"
I was wrong, it's working!
I had to drop it inside application.rb:
config.i18n.load_path += Dir[Rails.root.join('locales', '*.{rb,yml}').to_s]
config.i18n.default_locale = :'pt-BR'
And put "devise.pt-BR.yml" inside /config/locales/
I was looking for "Sign in" and label from fields at "Log in" view but I think these strings are not translatable, I will generate these views and manually revise them.
Thank you.
Try renaming your locale file to pt-BR.yml, or else change your reference in application.rb to devise.pt-BR.yml.
I am absolutely new to Ruby and Rails and I am getting an error when running this command:
rails generate scaffold User name:string email:string
Here is the full error I am getting:
/Users/ad7863/rails_project/demo_app/config/application.rb:7: undefined method `groups' for Rails:Module (NoMethodError)
from /Library/Ruby/Gems/1.8/gems/railties-3.0.11/lib/rails/commands.rb:15:in `require'
from /Library/Ruby/Gems/1.8/gems/railties-3.0.11/lib/rails/commands.rb:15
from script/rails:6:in `require'
from script/rails:6
I am following the Ruby on Rails tutorial which can be found here.
I am using Rails version 3.0.11.
Anyone have any idea what I'm doing wrong?
Thanks.
Edit: the contents of my application.rb file:
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 DemoApp
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]
# Enable the asset pipeline
config.assets.enabled = true
# Version of your assets, change this if you want to expire all your assets
config.assets.version = '1.0'
end
end
On line 7 of your application.rb file, change:
Bundler.require(*Rails.groups(:assets => %w(development test)))
to:
# Bundler.require(*Rails.groups(:assets => %w(development test)))
I have a rails 3 app, and am trying to do testing. I do the following command rspec spec/controllers/ and get the following error:
/config/application.rb:42:in `<<': can't modify frozen array (TypeError)
from c:/Users/#####/documents/#####/config/application.rb:42
It is pointing to my config/application.rb file which I have provided below.
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 PadOnRails
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.autoload_paths << "#{config.root}/lib"
end
end
has anyone else experienced this exact kind of problem. If so how do you fix it because I have deleted my project and did a git clone all over and still get the same error.
Try the method the comments suggest:
config.autoload_paths += %W{#{config.root}/lib}
This doesn't modify the initial array, but adds the given array to config.autoload_paths and saves it into a new array in config.autoload_paths.
Releated to this: Rake on Rails 3 problem
I'm trying to copy a rake task working under rails 2, into a rails 3 app.
The rake task is the following:
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
On Rails 2 it cache the assets correctly, on rails 3 I have the following error:
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'
Looking at the definition of stylesheet_link_tag I see:
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 ......
..... ....
My 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
My 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
I had the same problem and found a solution here: http://railsforum.com/viewtopic.php?id=42282
Just prefix
stylesheet_link_tag
and
javascript_include_tag
with
ApplicationController.helpers.
then you shouldn't even have to include the ActionView helpers.