When I run the rails application, I get the following error:
undefined local variable or method "config" for main:Object
How can I resolve it?
My guess is that you found some code examples from an older version of rails, which called for you to place a config.gem * in your environment.rb file. To fix this add the gem requirement to your gemfile.
in your enviroment.rb file, cut any line starting with config and paste into your production.rb/development.rb/test.rb instead.
This is what worked for me when I had a similar problem.
I had this problem when an application had been upgraded from Rails 2 to Rails 3. It worked in development but I got this error when running in production mode for the first time because config/environments/production.rb contained Rails 2-style lines such as:
config.cache_classes = true
..which needed converting to Rails 3-style:
<<NameOfYourApp>>::Application.configure do
config.cache_classes = true
end
Related
I have just created a new rails app (on the CL, using rails new), I am on 4.2.6, but it seems like before I can do anything to the app that i've hit errors.
first...
/config/environments/development.rb:53:in `block in <top (required)>':
uninitialized constant ActiveSupport::EventedFileUpdateChecker (NameError)
then once I comment that out...
/config/initializers/new_framework_defaults.rb:15:in `<top (required)>':
undefined method `to_time_preserves_timezone=' for ActiveSupport:Module (NoMethodError)
and once that is commented out...
/config/initializers/new_framework_defaults.rb:21:in `<top (required)>':
undefined method `halt_callback_chains_on_return_false=' for ActiveSupport:Module (NoMethodError)
and lastly...
.gem/ruby/2.2.3/gems/actionmailer-4.2.5/lib/action_mailer/base.rb:569:in `method_missing':
undefined method `perform_caching=' for ActionMailer::Base:Class (NoMethodError)
everything I can turn up on Google suggests these are Rails 5 related things. I'm not sure how to get around them, or how to create an app that is still specific to 4.2.6.
If still you want to use Rails 5 comment the below line in development.rb file:
config.action_mailer.perform_caching = false
And after doing that you may get:
ActionView::Template::Error (couldn't find file 'ation_cable' with type 'application/javascript'
Then remove = from //= require action_cable in your cable.js file.
tl;dr: Re-create your Rails project, providing the specific Rails version.
To elaborate on Peter's answer: often this error occurs because you have a newer version of Rails installed than the one called for in your .railsrc or template.rb.
If this is the case, when you run rails new my_new_app, the newest version is used by default for the earlier steps of the process, but then once the template/railsrc version is installed, latter steps use this version. This causes compatibility issues.
You can verify that this is your problem by comparing the output of rails -v (in the directory where you called rails new) with what's in your .railsrc or template.rb. If they are different, there is an easy fix:
Recreate your Rails app, specifying the same Rails version from your .railsrc or template.rb in the command line call:
rails _4.2.5.1_ new my_new_app
I'm working on a rails gem, in which I have some logic that I'd like to be conditional based on the rails environment.
The following code errors out:
if Rails.env.production?
When running in the test app this gives me:
undefined method .env for Gemname::Rails::Module
So, how do you find the Rails environment from a method call in a module that's in a gem?
You have a Rails module in your project, and the constant lookup is finding it, rather than the top-level Rails module. You can either use the top-level constant:
::Rails.env.production?
Or you can just check the environment variable:
ENV['RAILS_ENV']
i try to deploy a rails 3.1.3 app to heroku but keep getting the following error when browsing the pages:
ActionView::Template::Error (undefined method `asset_path' for #<#<Class:0x00000002ecd630>:0x00000002e52480>):
i use the "cedar" stack as recommended by heroku. any idea why i get this issue? do i have to make some more configuration?
thanks in advance!
EDIT: works again. i forgot to add "require 'sprockets/railtie'" in the application.rb...
You forgot to add "require 'sprockets/railtie'" in the application.rb
I'm learning Rails with the awesome Ruby on Rails Tutorial by Michael Hartl. I'm on section 3.2.2 (Test Driven Development) in which I need to run the following command to run the rspec tests for my Rails project:
bundle exec rspec spec/
But it doesn't work. Instead I get this error:
/Users/mh/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/gems/1.9.1/gems/
activerecord-3.1.3/lib/active_record/base.rb:1088:in `method_missing':
undefined method `mass_assignment_sanitizer=' for
ActiveRecord::Base:Class (NoMethodError)
I've tried reinstalling rspec and changing my Gemfile, but nothing appeases the undefined method error!
Did you downgrade from Rails 3.2 RC1? Comment out the following two lines from your development.rb:
config.active_record.mass_assignment_sanitizer = :strict
config.active_record.auto_explain_threshold_in_seconds = 0.5
While m818's answer will solve the problem, you might still get errors if you are tyring to use deprecated methods elsewhere in your code.
I had the same problem, commenting out those lines got rid of some errors, but not all of them, anywhere I was using attr_accessible gave me the same error.
It turned out to be the `active_record' gem that was updated to 4.0 when I didn't want it to. Since I'm using a Padrino app, I had to do this in the Gemfile:
gem 'activerecord', '= 3.2.12', :require => "active_record"
That solved all issues and I didn't have to comment out the lines in database.rb.
I feel like this should be an easy answer but I'm totally stumped.
I've added mimetype_fu to my gemfile and it installed it when I ran bundle install. When I try to use File.mime_type? in my application I get an error:
NoMethodError: undefined method `mime_type?' for File:Class
In the rails console when I run
gem 'mimetype-fu'
it returns true
I'm on windows, if that matters
Any ideas?
If you are using Bundler, you can also just add require to the gem line like this.
gem 'mimetype-fu', :require => 'mimetype_fu'
You may need to manually require it inside your rails app. You can do this by adding an file to config/initializers/ if you want it to be available globally.
EDIT | Also, you did restart the rails server, right? ;)