{"id":933,"date":"2015-01-09T15:08:06","date_gmt":"2015-01-09T15:08:06","guid":{"rendered":"https:\/\/intelligentbee.com\/blog\/?p=933"},"modified":"2024-02-10T06:16:43","modified_gmt":"2024-02-10T06:16:43","slug":"how-to-easily-debug-a-rails-application","status":"publish","type":"post","link":"https:\/\/intelligentbee.com\/blog\/how-to-easily-debug-a-rails-application\/","title":{"rendered":"How to (Easily) Debug a Rails Application"},"content":{"rendered":"<p>Hello all you party people! This article will show you how you can easily debug your Rails code.<br \/>\nIf you&#8217;re using the default Rails Rack server, Webrick &#8211; the best debugging tool I&#8217;ve been able to find out there is <a href=\"https:\/\/github.com\/pry\/pry\" target=\"_blank\" rel=\"noopener\">pry<\/a>. Its use is pretty straightforward:<\/p>\n<p>First off you&#8217;d need to add the <code>pry<\/code> gem to your <code><span style=\"color: #993300;\">Gemfile<\/span><\/code>.\u00a0Since this is used for development purposes, it should be added in the <code>:development<\/code> group<span style=\"color: #000000;\">:<\/span><!--more--><\/p>\n<pre class=\"\" style=\"padding-left: 30px;\">group :development, :test do\r\n .......\r\n gem 'pry'\r\nend\r\n\r\n<\/pre>\n<p>Next we&#8217;d need to update our bundle:<\/p>\n<pre class=\"\">$ bundle<\/pre>\n<p>After that&#8217;s done, all that&#8217;s left to do is to actually use the gem&#8217;s functionality in our code.\u00a0Basically, this will be of use whenever one of your\u00a0actions crashes and you can&#8217;t figure out straight away what caused it to crash. All you need to do is invoke\u00a0binding.pry somewhere in your code &#8211; which will create a Pry session at the point it&#8217;s called.\u00a0This will pretty much open a &#8216;developer console&#8217; where you can play around with all the objects that have been defined up until you called binding.pry. You can then easily debug your action, like you&#8217;d regularly do in an irb console. When you&#8217;re done with the Pry session the program will resume.<\/p>\n<p>Here&#8217;s how this would work in a regular create action from a Users controller:<\/p>\n<pre>def create\r\n  user = User.create(user_params)\r\n  binding.pry #Start a pry session to see if the new user looks the way we want\r\n  if user.save\r\n     flash.now[:success] = \"Welcome\"\r\n     sign_in user\r\n     redirect_to user\r\n  else\r\n     flash.now[:error] = \"Something went wrong: #{user.errors.full_messages}\"\r\n     render 'new'\r\n  end\r\nend\r\n<\/pre>\n<p>Voila! After you run the create action &#8211; you should view the Pry session in the console, where your rails server is running.<\/p>\n<p>If you are using a zero-config Rack server, like <a href=\"http:\/\/pow.cx\/\">Pow<\/a> (which I personally love), you would pretty much need a way to start Pry remotely. There&#8217;s a gem for that as well, called <a href=\"https:\/\/github.com\/Mon-Ouie\/pry-remote\">Pry-Remote<\/a>, which works pretty much the same way. The only difference is that we need to invoke <code>binding.remote_pry<\/code> instead of <code>binding.pry<\/code>. Easy peasy. Here&#8217;s how the same code from before would look like in this scenario:<\/p>\n<pre class=\"\">\u00a0def show\r\n   binding.remote_pry\r\n   @groups = current_user.groups\r\n end\r\n<\/pre>\n<p>Now, all that&#8217;s left is to run the <code>pry-remote <\/code>command to start your pry session after\u00a0the action has been\u00a0called:<\/p>\n<p>&nbsp;<\/p>\n<p>Happy debugging.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hello all you party people! This article will show you how you can easily debug your Rails code. If you&#8217;re [&hellip;]<\/p>\n","protected":false},"author":4,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[84],"tags":[96,119,200,205,214,215],"yst_prominent_words":[394,729,798,1440,1450,1948,2067],"post_mailing_queue_ids":[],"_links":{"self":[{"href":"https:\/\/intelligentbee.com\/blog\/wp-json\/wp\/v2\/posts\/933"}],"collection":[{"href":"https:\/\/intelligentbee.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/intelligentbee.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/intelligentbee.com\/blog\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/intelligentbee.com\/blog\/wp-json\/wp\/v2\/comments?post=933"}],"version-history":[{"count":3,"href":"https:\/\/intelligentbee.com\/blog\/wp-json\/wp\/v2\/posts\/933\/revisions"}],"predecessor-version":[{"id":133264,"href":"https:\/\/intelligentbee.com\/blog\/wp-json\/wp\/v2\/posts\/933\/revisions\/133264"}],"wp:attachment":[{"href":"https:\/\/intelligentbee.com\/blog\/wp-json\/wp\/v2\/media?parent=933"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/intelligentbee.com\/blog\/wp-json\/wp\/v2\/categories?post=933"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/intelligentbee.com\/blog\/wp-json\/wp\/v2\/tags?post=933"},{"taxonomy":"yst_prominent_words","embeddable":true,"href":"https:\/\/intelligentbee.com\/blog\/wp-json\/wp\/v2\/yst_prominent_words?post=933"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}