How do I debug errors with ActiveAdmin with Ruby on Rails? - ruby-on-rails

There is a rails application that uses bunch of gems one of which is ActiveAdmin.
Dashboard works OK, but other custom tabs gets a 500 response.
How do I enabled debugging/logging inside ActiveAdmin gem to get to the bottom of this problem?
Corresponding generic question is this: what are the ways to enable debugging on rails apps with multiple gem dependencies?

1) Check your development.log file after hitting the custom tab page and see where the error was. The log should give you a stack trace or at least tell you the last controller that was hit. You can use this information to work backwards and find out where you code is breaking.
2) Once you have found the broken spot in the code you can use raise or pry debugger to inspect variables/methods to help you debug.

This turned out to be a version mismatch between the mongoid interface and the active admin interface.Thanks for all the help.

Related

How can I disable ActiveSupport::Notifications on Rails 7?

I'm trying to upgrade my API from Rails 6 to Rails 7, and got this error after solving dependences and running my app:
Error:
ActiveSupport::Notifications::InstrumentationSubscriberError (Exception(s) occurred within instrumentation subscribers: NoMethodError, NoMethodError)
I never use this feature, searching all my files doesn't exist any mention to this Notifications.
Couldn't identify who and why is calling this notifications and neither find on Rails 7 Docs how can I disable this feature to solve my problem. Anyone know how can I do it?
Tried remove completely the ahoy_matey gem that appears in traceback and change de API mode to false on config/application.rb but both doesn't work.
I expect disabling this feature can fix my problem.
I was getting this exact same error, and the listed solution didn't work for me.
My specific error was an old version of the Airbrake notifier library (v10, updating the v13 fixed it), but to track it down I looked at the error page, which included the NoMethodError error traces below the main application trace.
So if this happens to you, hopefully you won't lose two days to it like I just have. 😬
after spend few hours searching the docs of rails 7, I finally found it. Right here, where it says:
Controls behavior when parameters that are not explicitly permitted are found. The default value is :log in test and development environments.
:log to emit an ActiveSupport::Notifications.instrument event on the unpermitted_parameters.action_controller topic and log at the DEBUG level
The solution was add config.action_controller.action_on_unpermitted_parameters = false on my config/application.rb file.

Getting a white page when testing new gem/engine. Logs show views rendering, but the page is empty

I've been developing a gem/engine for a little bit, and using an existing app of mine to test it out. Everything has worked great. Then I went to add it to another app and suddenly it wasn't working. I figured it was because that app had some weird set up, but I've tried on a couple of other apps now and they all do the same thing.
When I go to visit the gem in the browser, the page is completely empty. It receives nothing from the server. No head, style, script, body. Just a blank page. I can visit other routes for these apps just fine and the pages load as expected. However, any of the routes added by the engine do this same load issue. The strangest part is: In my server logs, I see the correct controller hit, I see the views being rendered, I get the 200 OK at the end. I can do puts in the gem views and it will show up in the logs during the load process. There is absolutely no sign that anything went wrong anywhere, but yet... White page. I'm at a loss as where to even start debugging this. Does anybody have any experience with anything of the sort?
I created a new test app to verify and things worked as expected, but for some reason other existing apps have this issue.
Working apps:
Rails 5.0.2
ruby 2.7.2p137
Fresh/blank app: (Working)
Rails 6.1.4
ruby 2.7.2p137
Apps that don't work:
Rails 6.1.3.1
ruby 2.7.2p137
Rails 6.1.4
ruby 3.0.2p107
I'm not even sure where to start debugging this as there is no "error" so I'm at a bit of a loss.
The repo for the gem is here: https://github.com/Rockster160/command_proposal but I'm mostly looking for thoughts on how to work through why this issue is happening and out how to debug it.
Turns out this was caused because the app it was added in had links inside the layout. (Like a nav bar) I had a rescue block that caught those issues and attempted to look up the route in the main_app, however, that rescue still recognized that an error was thrown so rendered the empty file. It seems odd that would be the result, but I finally discovered that was the case.
To fix the routing issue, I had to include the main app routes as a helper in my main engine controller:
helper Rails.application.routes.url_helpers
However, this broke my engine routes, so I had to go through each of my engine's routes and explicitly call them off the engine.
tasks_path # Old way
command_proposal.tasks_path # New way

Ruby on Rails Runtime Console

I am trying to find a way to get a rails console for a program during runtime with all instantiated variables. The normal rails console does not have access to any instantiated variables of its respective running application. For example, when a rails application crashes during runtime, a webpage will load with the error listed in red text, a snippet of the code where the error was raised, and a console at the bottom with access to variables instantiated during runtime.
See image below for console im talking about
The best thing I could find was a gem called pry, which seems to allow you to access a console during runtime by adding the line 'binding.pry' in your code at the point where you want access to the console. I would be fine with this, but seeing as how rails already gives you access to this when your app crashes, I would think there is a "vanilla" way to do this. Unfortunately I can't find anything online about this feature in rails. This seems like such a valuable tool for debugging I can't see why rails doesn't implement this. Is there a better way to debug during runtime? a better gem?
I will suggest you to use Better Errors
Better Errors replaces the standard Rails error page with a much better and more useful error page. It is also usable outside of Rails in any Rack app as Rack middleware.

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

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.

Ruby / Rails debugging strategy

Please can you share your approach / methodology to debugging in Ruby / Rails.
I'm busy with the Rails tutorial, and am getting the error:
NoMethodError in UsersController#show
undefined method `microposts' for #<User:0x83b43e8>
And that got me thinking about debugging strategies. Does anyone have advice for a new Rails user (and new MVC user) on strategies to approach debugging. What path do you follow? Is there a generally accepted approach? Is there a way to step through the code?
Right now I am using unit testing as a kind of "lint" checker, but that only goes so far.
Although I want to solve it, the actual error I am getting right now is not the main thrust of this question.
(PS: The problem is not a duplicated "show" as documented in elsewhere on Stackoverflow
I haven't seen this mentioned yet but another option is to put a debugger statement in your code. Like this
def some_method
something = 3
debugger
# ... more code
end
If this is in a rails app when the code reaches debugger it will cause the terminal window running the web server to jump into something that looks like an irb session (I'm not exactly sure what it is). From there you can do puts something to see what the value is for example. You can even puts params to see what all the params values are. You can also step through the code, jump to a specific line, etc.
Pry seems to be a better way to go about this but it's how I used to debug before I knew about pry.
Note: You might need to do require 'ruby-debug' if you're doing this outside of rails.
i use a combination of irb, print statements, logging and pry bindings. (pry is a great gem)
irb is a great way to just play around with your ruby or rails app in the console. You could just enter the code from your controller (or similar) and see if it breaks in console for faster feedback loop. But remember you have to do reload! if you change anything in your class/module.
print statements are easy if you're running tests and just want it to output something a different points in your test. But if your testing in a browser I would recommend writing to the logger: Rails.logger.debug "...". But remember to set your logging level in your configuration to DEBUG -or- you can just do Rails.logger.info instead which should show up by default. Then you can just tail or view the logs in my_app/logs/development.rb.
My favorite method for really tricky bugs is that if the error is happening in a test you can just place binding.pry in the preceding line and then it will pause your test at that line and drop you into a console. I recommend watching the rails casts for more in-depth info: http://railscasts.com/episodes/280-pry-with-rails
I do not start Rails project without 'pry' gem.
Add gem to Genfile:
group :development, :test do
gem 'pry'
end
and stop request execution anywhere in your project, just put
binding.pry
to your model, controller, tests ..., or
<% binding.pry %>
in your view's, templates, partials.
Then you can check what ever you want objects, params, variables ...
Type exit to leave pry environment, and request will continue.
The Ruby on Rails Guide would be a great place to start, but there's plenty more.
I always have a rails console session or at minimum an irb session to play with to see if things do what I think they do.
I also use RubyMine which has an excellent integrated debugger http://www.jetbrains.com/ruby/
Beside pry gem, another option would be byebug. This gem enables you to temporarily stop code execution at a breakpoint, which is marked with keyword byebug inside the code. When execution reaches the breakpoint, a marker will be pointing to the current line, and you are able to type in commands.
This Gem offers a huge set of commands, the most commonly used ones are:
next - this command enables you to go to next line
step - goes into each invoked method step by step
break - it stops the execution of code
continue - continues code execution
This is a great article to check for debugging in rails.

Resources