views:

126

answers:

3

I am tired of asking unanswered questions when using many outdated plugins / gems, and sometimes they don't really work how I really wanted.

So my question is simple:

If I was a PHP programmer, and Rails was my first framework, what do I need to learn next so I can depend on myself when working with troublesome plugins or code snippets or tutorials?

I used to be quite good with an (rather silly now that I know MVC) e-commerce system in PHP, and normally I would just go ahead and read out the plugin's code to find out what it does, but apparently doing so is extremely hard in Ruby on Rails, should I keep doing it, or am I going the right track? (I did learn by the way, but in very slow pace compared when I was still using PHP and that "e-commerce framework")

A: 

You should keep reading plugin code.

If it is extremely hard, you may become extremely good at it.

Thomas Langston
A: 

Go watch the Yehuda Katz video at on the homepage of railsconf.blip.tv. He's now one of the core rails developers and discusses how he got into it.

Kaleb Pederson
+1  A: 

This is like a generic programming question for me. If you have any troubles with some Rails plugin, you can always debug its code with a bunch of hardcore or easy methods, try to understand and fix the error. The question is, which methods should you use in particular situations.

I will give you a little example. When you are debugging your Rails application, always check either logs/production.log or logs/development.log (depending on mode you're working) for errors of any kinds. Every error in Ruby/Rails is represented by a huge stack-trace that you should read from top to bottom. Like this one:

Processing CommentsController#create (for ***.***.***.171 at 2010-08-27 03:31:29) [POST]
  Parameters: {"authenticity_token"=>"[STRIPPED]"}, "last_comment_id"=>"0", "original_controller"=>"projects", "thread"=>"true", "thread_id"=>"Conversation_31", "commit"=>"Save", "_"=>"", "controller"=>"comments", "action"=>"create", "conversation_id"=>"31"}
Sent mail to [email protected]

ArgumentError (invalid byte sequence in UTF-8):
  /usr/lib/ruby/1.9.1/net/protocol.rb:294:in `slice!'
  /usr/lib/ruby/1.9.1/net/protocol.rb:294:in `each_crlf_line'
  /usr/lib/ruby/1.9.1/net/protocol.rb:236:in `write_message_0'
  /usr/lib/ruby/1.9.1/net/protocol.rb:250:in `block (2 levels) in write_message'
  /usr/lib/ruby/1.9.1/net/protocol.rb:280:in `using_each_crlf_line'
  /usr/lib/ruby/1.9.1/net/protocol.rb:250:in `block in write_message'
  /usr/lib/ruby/1.9.1/net/protocol.rb:169:in `writing'
  /usr/lib/ruby/1.9.1/net/protocol.rb:249:in `write_message'
  /usr/lib/ruby/1.9.1/net/smtp.rb:878:in `block in data'
  /usr/lib/ruby/1.9.1/net/smtp.rb:921:in `critical'
  /usr/lib/ruby/1.9.1/net/smtp.rb:875:in `data'
  /usr/lib/ruby/1.9.1/net/smtp.rb:655:in `send_message'
  actionmailer (2.3.8) lib/action_mailer/base.rb:684:in `block in perform_delivery_smtp'
  /usr/lib/ruby/1.9.1/net/smtp.rb:526:in `start'
  actionmailer (2.3.8) lib/action_mailer/base.rb:682:in `perform_delivery_smtp'
  actionmailer (2.3.8) lib/action_mailer/base.rb:523:in `deliver!'
  actionmailer (2.3.8) lib/action_mailer/base.rb:395:in `method_missing'
  app/models/emailer.rb:89:in `send_with_language'
  app/models/conversation.rb:51:in `block in notify_new_comment'
  app/models/conversation.rb:47:in `each'
  app/models/conversation.rb:47:in `notify_new_comment'
  ...

Here we are! The line ArgumentError (invalid byte sequence in UTF-8):, on the very top of this trace, always tells us the error type. It's an argument error!

After that, look at the next line: /usr/lib/ruby/1.9.1/net/protocol.rb:294:in 'slice!'. It tells where the exception (ArgumentError) was raised in the following format:

 /path/to/file/:line:in `method_name'`

Let's open that neglectful file and find the source around that line:

def each_crlf_line(src)
    buffer_filling(@wbuf, src) do
        while line = @wbuf.slice!(/\A.*(?:\n|\r\n|\r(?!\z))/n)
            yield line.chomp("\n") + "\r\n"
        end
    end
end

The error is on the following line:

while line = @wbuf.slice!(/\A.*(?:\n|\r\n|\r(?!\z))/n)

From this point we know where the error is and only need to fix it, but this process is out of scope of my short story. I just wanted to show you how to work with errors in Rails.

floatless
Thank you for your awesome effort in answering this, I learned much from it. However I want to get more more people answering so I could learn even more aspects of RoR programming. So if you allow me, I'd like to hold my answer acceptance. There are other questions regarding debugging that I just posted, I'd be humbled to get other good answers from you: see http://stackoverflow.com/questions/3586386/how-to-debug-a-plugin-or-gem-using-ruby-debug-gem-where-the-part-i-wanted-to-deb and http://stackoverflow.com/questions/3585985/how-to-debug-a-plugin-gem Thanks again!
jaycode