When I started to learn Ruby on Rails, my biggest problem was how to understand what I have done wrong. Some of you may know Microsoft’s IDE called Visual Studio. This is a very powerful tool that includes debugger tool. You can simply ‘toggle breakpoint’ in the line you want and when the program steps into this line, it will stop and you will have immediate lookup on all the variables at this particullar moment.
Some time ago, my friend showed me Pry gem. Wow. That was like the missing link. It works similarly to the Visual Studio debugger. When you want your application to stop in a particullar moment you simply type:
When an application steps into this line it will stop. And in the terminal where you have started your rails server you will have something like ‘live console’. You can easly debug your variables there instead of making ‘puts’ or ‘p’ everywhere. Example of how it works below:
Of course this is not the only feature of this gem. It will ‘rebuild’ your irb console also. Just give it a try and I promise, from now on you will always have it in your Gemfile.
Let’s stick to the debuging area for a while. You often get an rails app error screen. What if there is a gem that shows you this error in a beautiful way? Fortunately there is Better errors. Moreover you can have a live shell if you add Binding of caller. Errors with those gems just seem more friendly and don’t bother me that much.
There are projects where you need to build some crazy form that has some nested attributes, some terms accepting and stuff like that. It’s a good practice to build a form object in order to handle this easily. Reform gives you maximum options for minimum code. Every form object inherits from Reform::Form. You just define fields (as properties) and write validations to them. I can write a whole blog post about the possibilities that this gem gives you, but it’s not the point. Go check it out yourself.
Do your haml views contain many lines like this:
-if @user.something %p User is something -else %p User is not something
[email protected]_date.strftime("at %I:%M%p")
then you definitely would like to use some presenter pattern or in this case – decorator. By usingDraper you can have a place for some logic that needs to be included in the view. A view should be easy to read and adding more and more ‘if-else’ or some ‘polishing’ methods won’t help at all. Thanks to draper, all view-model logic can go into the decorator and the view itself will be only responsible for displaying the data.
This is a must for all developers. But don’t tell it to anybody. This will be our secret. It’s a key to write typo-less application without even bother to check typos. Here’s some code that will illustrate what happens when you start using this gem: