Rails: Reloading code in development environment gives a "ApplicationHelper has been removed from the module tree but is still active!" error - ruby-on-rails

I upgraded to Rails 4 and now when some changes are made to the code, or when I do a reload! in rails console, I will get an ArgumentError: A copy of ApplicationHelper has been removed from the module tree but is still active! error.
I'm not the only one with this problem but still cant find existing solutions that work yet.

I've never encountered that problem before. I'm using Rails4 as well.
I think you have to call your controller(which is shown in the ArgumentError) explicitly in your dependent controllers.
Can you try adding this line on top inside the class?
require "_controller"
Then try rails server -e production
See if this helps. Sorry I wish I could give you more detailed solution.
If all else fails, rollback might be an option to consider.

Related

How can I find out what prevents a rails app from loading a page?

I have a rails which seemed to be working previously but now after some changes when I go to the root page it takes it infinitely to load it, it just doesn't load it. There're nothing useful in the console either. How can I find out what prevents it from loading the main page? Is it about profiling?
Check your Rails logs, eg. development.rb. You can put logger.info, or puts statements in your environment.rb, development.rb and application.rb files to see how far Rails is getting in the boot process. You can also create a dumb initializer named 00_start_init.rb with a logger.info or puts statement to see if you're getting as far as initialization. I've found that useful before.
To really understand where you application is hanging, you need to understand the Rails initialization process. Here is the documentation for Rails version 4.2. http://guides.rubyonrails.org/v4.2/initialization.html. Similar documentation exists for every version of Rails. You can take advantage of understanding the boot sequence by placing log statement at various point in the process.
I'm assuming you're in the development environment. If so, and the console loads, it's likely not a configuration problem. It's more likely a problem with your controllers or models. If the console won't load to a prompt, then it's likely a configuration problem in application.rb, development.rb, an initializer, etc.
You mention profiling, but provide no details about it. I can't even guess what you're referring to, so the answer is "maybe?". If you can post the code changes you made since the app last loaded in the browser, that would make it much easier to help you trouble-shoot.

Rails: Nested Namespace'd Controller in Rails 3.2 fails to load (fine in 3.0)

I have a controller named Reports::Accountant::ApprovedTimeOffRequestsController. It's in the proper directory, and the class name is correct in the file its self. Rails console and unicorn load just fine, but rspec can't seem to load the file during testing.
Here's the error.
/Users/mdarby/.rvm/gems/ruby-1.9.3-p125-perf/gems/activesupport-3.2.1/lib/active_support/dependencies.rb:503:in load_missing_constant': Expected /Users/mdarby/Documents/Code/ccw_32/app/controllers/reports/accountant/approved_time_off_requests_controller.rb to define Reports::Accountant::ApprovedTimeOffRequestsController (LoadError)
from /Users/mdarby/.rvm/gems/ruby-1.9.3-p125-perf/gems/activesupport-3.2.1/lib/active_support/dependencies.rb:192:in block in const_missing'
This file was find on Rails 3.0 stack, but now that I've upgraded to 3.2/rspec 2.9 this is bombing out.
The definition of the class:
class Reports::Accountant::ApprovedTimeOffRequestsController < ApplicationController
EDIT:
If anyone comes across this, I solved it by removing the specs :)
I bet you're using Spork. I ran into this exact problem. I removed Spork and I was on my way. Give that a whack and let me know if it fixed it. Might be worth filing an issue on spork if that's the case.
Are you doing anything fancy with your tests? I had a case in the app itself where multiple threads were trying to load the same module. I can only guess that one marks it as loaded before it actually runs it, and then the constant isn't available to the next. In any case, my solution was to add a prepare block outside the threading.
development.rb (you might need this in test.rb)
ActionDispatch::Reloader.to_prepare do
Reports::Accountant::ApprovedTimeOffRequestsController
end

Rails: How to make require reload on every refresh?

I've notice that when I include password hashing with require "digest/sha1" on top of my model, every time I make some change in my app, I have to restart server to see changes.
That is kind of annoying and it is slowing down development a lot, especially for beginner like me.
I've seen somewhere that with require_dependency "digest/sha1" it should work, but it is not working for me (saying no file error).
I'm not sure even where to put require_dependency as I haven't found any example.
There should be some way to make it work, as it is quite common problem, maybe I was just looking to wrong places.
Thanks
EDIT :
I've came to conclusion that error comes when my Ubuntu machine goes to sleep. After wake up, local server (tried thin and WEBrick) give that error.
EDIT :
It has nothing to do with Ubuntu sleep. I had a function named hash that was giving errors every time, complaining that it got wrong number of arguments, but that function was never called.
So, I've renamed it to encrypt and now it is working, but I'm not 100% sure that it is solution, I have to test is more.
If it will be ok, that would be a strange bug, I will post an answer.
In development mode, by default, anything in app/ or config/routes.rb is reloaded between requests. If this is not happening for you, it's probably not caused by the require. It's more likely that you've turned off reloading inadvertently.
I've used digest/sha1 in many projects before and have never had this problem, usually including it in the User model where it's used.
Can you replicate this problem in a brand new Rails project? Does the problem go away if you remove that line? If so that is very odd.
If you remove require "digest/sha1" all works fine?
Anyway check for config.cache_classes = false in config/environments/development.rb

Activerecord error .include? nil

I am writing a rails app, which has some modules and methods loaded via the lib directory, we have also added some methods in restful authentication module, now everytime when i run using mongrel server on production i dont get this error, although because model classes and controller classes are cached, but in development i do get this error, after the first time, so i start my server on development and run once i get the correct behavior, but when i refresh the page i get this nil.include error, while it displays that the each method is getting a nil to iterate, also i have done some homework, as i added the puts method, for all methods for that instance while iterating, i dont get any method definations for attributes of the table, like ID, name, title. i am unsure about this,
I am using Rails 2.2.2 and ruby 1.8.7.
Server is mongrel & webrick.
This situation happened to me, where I used to get the error only after the refresh (i.e., second call to the server).
It may be that you have defined a module in your /lib directory which does not matches the rails convention of file-naming. And you are using the require "filename.rb" statement to get the module.
Try using require_dependency "filename.rb" instead.
Let me know if problem still exists, we may look deeper.
You should look into the log/development.log or log/production.log files (depending on the environment you are investigating). The first line of the stacktrace shows the source of the error. Try to find the error there or post the relevant line (this and some above).

has_many_polymorphs tagging - works on development machine, not on production!

I'm having a weird problem, where tagging works fine on my development machine, but when I deploy to the production server, I get this error in the log:
ActionView::TemplateError (undefined method `tags' for #<Person:0x98bb9d4>) on line...
There is an entry in the production.log file that states that has_many_polymorphs is loaded, so it's not like the plugin isn't available on the production machine.
My Google-fu has failed me trying to find the answer, so if anyone knows what could be wrong it would be greatly appreciated!
Edit: I should have mentioned that on both production and development I'm using the same database. I downloaded the production one, and used it on the development machine and it works fine.
cap deploy:migrations
I've seen similar problems to this in which the polymorphic type field is not getting correctly filled in, or when there was some existing data prior to the polymorphic type tag getting added. Is person a subclass? does the _type field contain any null values on the polymorphs table?
Just stabbing in the dark here, but has_many_polymorphs doesn't natively add tagging functionality to your models. Instead, you use a generator to create a tagging extensions module that goes into lib/tagging_extensions.rb. The module file has helper methods that add tagging functions, built on top of the has_many_polymorphs base functionality.
So, is it possible that you have the plug-in installed, but not the tagging extensions file?
I spent some time with a consultant tracking this down, and eventually we discovered that for reasons unknown, the Tagging stuff just wasn't being loaded.
By adding a single line of code, just three letters, to the end of environment.rb, it was resolved. I commented it so that we'd never forget wtf was going on:
# Magic begins here.
# We need to force Rails to load the Tag record, or
# has_many_polymorphs doesn't work properly. I don't know
# if there's a better fix, but this one seems reasonable. :-/
Tag
That was it. I'm sure there's an elegant and proper solution to this, but this works. Weird.
I hope this helps someone out there.

Resources