Hello to all! I’m trying to master Ruby on Rails and I don’t know how to look for errors correctly. If everything was simple in PHP (or an error displays the compiler, or you look at what variables contain, what this or that function returns, etc.), but RoR is not all that clear. Tell me, please, how you debug your code. I installed Nginx and Thin, on which my RoR project runs
- oneHave you ever worked with MVC PHP frameworks? In fact, they differ little in structure from the ROR framework. And so, read about Unit testing. - Alex Krass
- 2guides.rubyonrails.org/debugging_rails_applications.html rusrails.ru/debugging-rails-applications - etki
3 answers
First of all, I watch the console (if in progress) or logs (if at work). You can output variables both to the console (more conveniently) and to the browser (for debugging views).
It is convenient to display this gem here: https://github.com/michaeldv/awesome_print
Actually: http://www.rusrails.ru/debugging-rails-applications
There was still somewhere here: http://railstutorial.ru/chapters/4_0/beginning
Add examples:
In the gemfile:
group :development, :test do gem 'awesome_print' end In the controller (result in the console):
def show @item = Model.find(id) ap @item #awesome_print puts @item #ruby standart end View (result in browser):
<%= ap(@item).html_safe %> or
<%= debug @item %> And Nginx with thin not needed during development (sometimes they even interfere). Enough rails s (webrick).
In addition to these answers, it is sometimes useful to set gem 'pry' into the group :development in Gemfile and set it in code where you need to stop binding.pry , so you can see not only what you indicated for printing from the @mayar answer, but also anything . At this point, the code in the terminal window will appear familiar in appearance irb , and exit from it with the continuation of the server - Ctrl-D.
The byebug gem is also useful. It is very easy to use.
Here is a sample code:
def show byebug #место остановки дебага @comments = @link.comments.order(id: :DESC) end At the moment of calling the method, rails pause the robot and you can check the value of the variable in the console.
Example of use:
9: def show 10: byebug => 11: @comments = @link.comments.order(id: :DESC) 12: end 13: 14: def new 15: @link = current_user.links.new (byebug) @link #<Link id: 1181, title: "Conatus adultus quod aliqua.", url: "http://monahanberge.biz/dorothy.blick", user_id: 22, created_at: "2017-02-13 21:44:15", updated_at: "2017-02-13 21:44:15"> (byebug) To exit debug mode, you can use the continue command, or abbreviated с , or the jump to a new line in the code next or n .
More detailed information on the page in github from the developers of gem.