I’m a big fan of debugging. I find it a good tool to help you while writing
code and I use it a lot. However, it can be a bit annoying to debug rails
applications when you are running them on a web server like Passenger or Pow.
That’s why most of the times I use the great
rack-debug, which makes it possible to
debug this kind of applications.
The only problem with rack-debug is that you need to set it up every time
you want to use it on a new Rails application, thats why I created
debugger-rails, to do all this for
me.
The only thing debugger-rails does is to follow the instructions on the
rack-debug README and uses a railtie to insert the middleware and the rake
task in Rails.
If you want to know more about rack-debug (or debugging ruby code in general)
you can see my small tutorial.
Edit 2012-07-03: As you can see in the comments, simple_form has build it support for
bootstrap. And built in a much better way than this. Check the
wiki
to see how to make it work.
At Zorros we love Twitter’s bootstrap. It’s easy to use and perfect to start
your new applications with a good result. It provides some basic CSS tools
like a grid system together with some other fancy things.
However it has a small problem. You cannot make it work with a Rails
application out of the box. Don’t worry, it’s not a big problem. You can make
it work with just a bit of hacking here and there.
This are the minimum config options that I’ve found that you need on
simple_form to have it working with bootstrap so that it applies the right
classes. On fields and wrappers.
We’re almost there… there’s one more step needed. Bootstrap uses an extra div
container around each input field. I’m afraid I couldn’t find any way to add
this extra wrapper with simple_form, so I ended up with this patch.
So far it works well, we couldn’t find any caveats yet… but if you do, or if
you find a better way to integrate these pieces of amazing soft, please let me
know!
Update 2011-08-15: We finally covered the vacancy. If you want further info
you can read it at the Zorros’ blog post.
Apparently our clients like our work a lot. They like our work so much that we
are looking for a Zorro to join our team. Zorros is developing web applications
and business software, and is looking for an extra developer. Do you recognize
yourself in the following profile description?
Your passion is IT
You love the web and innovation
You like to solve problems using software
You know how to collaborate in a team
You can work from home
You can manage your own planning and freedom
You have been coding a lot (which language is not important)
You know some basics about UI and design
You are a problem solver
You love coding, but are not too proud to do other web agency related work.
English is not a problem for you.
We’d like to talk with you! Please contact us at hello[at]zorros[dot]be
Note: if you know these words, you definitively have a plus:
Rails & Ruby
DRY & conventions
HAML & Sass & CSS
VIM & TextMate
JSON & XML
JQuery & prototype
Saas & Freemium
Git & Terminal
*nix (Linux, MacOSX, etc…)
You can find more info about us in our website: Zorros.be
Edit 2012-07-05: You can now install rack-debug in a much more easy way by
using my debugger-rails gem.
Without Passenger
In my last post we learned how to debug ruby scripts. It can be done
as well with a rails application, you just have to start it with script/server
-u. The -u means to start on debug mode. Just make sure you have the
ruby-debug gem installed.
With Passenger
That works amazing… unless you use Passenger on development. If you do, then
you’ll need a different way to run the debugger. Don’t panic, it’s quite easy,
thanks to the rack-debug gem.
Rack::Debug is a gem which provides an easy interface to ruby-debug for
applications running on Passenger. And the good thing about it is that… it
just works!
To use it, just add the ruby-debug gem to your Gemfile, make sure to use the
1.4.x version if you’re
running ruby 1.8.x, since the new 2.x version is prepared for ruby 1.9.2.
Now that your gem is installed, add the middleware to Rails.
And the helper rake tasks.
Now you can just add a debugger statement on any line on your application,
restart it and run rake debug so that the Rack::Debug gem connects to the
debugger. Easy, eh?
Debugging rules. Maybe you’re trying to find an elusive bug or how that
spaghetti piece of code works, in any case is a quite powerful tool and you
should add it to your day to day job.
Debugging in ruby is easy… and you don’t even need a GUI for it! Ruby has the
amazing ruby-debug gem. That gem provides an executable to debug your ruby
software. There’re versions for ruby 1.8.x and ruby 1.9.x. I will explain here
how to install and use the version for ruby 1.8, but ruby 1.9 version is quite
similar.
First install the gem with gem install ruby-debug. This will give you an
executable called rdebug which executes the ruby interpreter in debug mode
allowing you to debug your script.
I will use this small script to debug.
Now we can just run the debugger with rdebug small_script.rb. And… voila!
Here you can see the debugger.
Here’s the file in which you’ve stopped:
[-4, 5] in /projects/889122/small_script.rb
This arrow indicates that you’re stopped there:
=> 1 dude = true
And this is the rdebug console where you can give commands to the debugger.:
(rdb:1) _
Let’s give it a quick try to see how the commands work. Type help and press
enter.
As you can see there’re plenty of commands in the debugger, those ones you will
use them a lot.
next (you can use it shortened as n as well) will make the
script go one step forward.
continue (shortened as c) will make the script continue until it
finds a breakpoint.
step (shortened as s) will step into a method.
break (shortened as b) sets a breakpoint with the format b
file_name.rb:XX where XX is the line number.
For everything else… I encourage you to spend 15 minutes reading each command
help 1 by 1. Special mention to irb, where, up and down commands.
After many searches in Google I found many explanations on how to use PayPal,
however any of them did what I exactly needed. So here’s what I did step by
step last time I needed it.
How PayPal works
Take a look at this flow diagram:
The buyer goes to PayPal and performs the needed payment. (This is steps
1 and 2 in the diagram)
PayPal takes the payment and redirects your buyer back to your
application. (steps 3 and 4)
Some time after that, PayPal will send you a callback response with the
details of the received payment. Your application will use those details to
make sure the payment was OK and mark the order as paid. (Steps 5 and 6)
Set up your PayPal sandbox
Sign up here To get a sandbox account. This
account will allow you to handle your development tools:
Seller and buyer mock accounts: That way you can test your payments
without need to use a real account/bank account/credit card.
IPN (Instant Payment Notification) testing: This is the way that
PayPal informs your application when the payment is done.
Create a seller and buyer mock accounts. Preconfigured seller accounts
failed for me, they always failed for me 100% of times in a period of many
months. I’m quite sure it’s my fault, but I just couldn’t find enough info to
know what I did wrong.
Set up your application
Install ActiveMerchant.
Here it came my second big problem. Just couldn’t figure out how to make it
work in the form of a gem. So I finally had to install it as a plugin.
rails plugin install git://github.com/Shopify/active_merchant.git
From the above diagram you application will need to:
Be able to send payment details to PayPal.
Receive a callback from PayPal to acknowledge the received payment.
And here’s where ActiveMerchant enters. It provides a helper to craft a form
with the needed hidden fields which you will send to PayPal and a class with
the needed logic to receive and validate the callback from PayPal.
Configure Active Merchant
Set up an initializer file like config/initializers/active_merchant.rb with
the following content (it explains itself… isn’t it?):
Set up the PayPal button
Use something like that to generate the button that your user has to push so
that he makes the payment.
If you need to send more info to PayPal take a look at the ActiveMerchant’s doc
or even at its code, to see which options are accepted by the
payment_service_for helper. Just FYI, the helper doesn’t support multiple
items. In case you need it you’ll need to craft your own helper… and maybe
send a pull request to ActiveMerchant ^.
Gather and acknowledge the PayPal response
PayPal will send you a notification so you can know when the order is paid.
This following code is a sample of how this can be done.
That’s all folks!
This is the most simple way I found to make PayPal work with ActiveMerchant.
However it’s very limited and lacks support for things like multiple items and
data encryption between your app and PayPal. Unluckly ActiveMerchant lacks
support for these two things on its PayPal integration. However you can achieve
this by using something like that.
If you find some stupid thing or some bug here, just let me know so that I can
fix it :)
Edit 2012-07-04: You won’t have those problems with latest rails and
paperclip. As far as I know this only happens with rails 2.
I love paperclip. To be clear it just
works. It makes what it has been designed for and makes it easy and flexible
enough to do some other things.
However, there’s something with paperclip that always disappoints me, it have
some problems when you want to install it as a gem, and, in my case, when you
want to use it with bundler.
The problem is that it doesn’t load the rake tasks nor the should macros.
Shoulda macros are loaded without problem if the gem is vendored in the project
dir, since shoulda will look for it. But to load the macros when not vendored
you need the following code on your test_helper.rb just before class ActiveSupport::TestCase.
That way the macros are properly load and you can use them on your tests.
To load the rake tasks you need a similar approach. Just include this line in
your Rakefile. If you’re using Rails 3 it will properly load automatically the
rake tasks for you so no need for this trick.
The last week I dropped my job at Nuatt and started a
new professional adventure as a freelance developer together with the excellent
guys from Zorros.
My main role will be developing Ruby on Rails applications to boost others’
business as well as helping with improving Fikket which
is an excellent solution to publish your events online and sell the tickets
without effort.
I’m quite sure that this new adventure will be really interesting and will
bring lots of fun.
BTW, just in case you don’t know “zorro” means “fox” in Spanish.
I’ve just put my dotfiles in a GitHub
repository. It’s just s simple collection of configuration files and a rake
task to install them together with
oh-my-zsh.
It includes as well some nice tools taken from other dotfiles I’ve found
searching on GitHub. If you like it just fork it change it and use it :)
Do you’ve some good tools in your dotfiles? Share it!
Thats how I make to integrate flowplayer inside a
fancybox modal window.
This following code shows a hidden div which will include the video. When
clicking the “Play video” link it will show a fancybox modal window with the
flowplayer video inside.
When closing the fancybox it will unload the video player and hide the div.