How to debug in Model?
Does anybody know how to debug in rails in the Model? I have a method that is now working and I was trying to check the variable's value in between typing
logger.debug va开发者_Python百科l_name
that's working in the controllers but not in the models does anyone know why? The model is not inherited from Active Record if it can be the problem. Is there any way to debug it?
Another way to log from anywhere in your application:
Rails.logger.debug "Show this message!"
This will always output to the log/development.log
file.
You can just do "puts" inside of your model method calls and it'll output. You can see it in the rails logs, or the console's output.
since your model is not an ActiveRecord it does not have the logger method mixed-in
you can add it like this (probably other better ways to do it as well)
class YourModelThatIsNotActiveRecord
def logger
RAILS_DEFAULT_LOGGER
end
end
a bunch of great information here as well - http://guides.rubyonrails.org/debugging_rails_applications.html
If you are spinning up Mongrel using script/server
in development, this might work:
- installe the ruby-debug gem, if you don't have it already:
gem install ruby-debug
; - add
require 'ruby-debug'
toconfig/environments/development.rb
; - put the line
debugger
in the source where you want to initiate a debugging session.
Look at this official guide for more information: http://guides.rubyonrails.org/debugging_rails_applications.html#debugging-with-ruby-debug
精彩评论