пятница, января 09, 2009

Back to Rails

Lately I wasn't doing any Rails development because I was busy re-learning C/C++ "the right way". Turns out it's not that painful as they say.

Then I had to do some Rails coding using latest and greatest Rails 2.2 and I find that that the simple things are broken (or somewhat obscure).

Case 1.

I was developing some general resource controller and wanted to spec out a redirect after #create action. My controller should not be bound to any specific resource url (as it is planned to bind many different resources with that controller), so I decided to make use of Rails' url rewriting. Spec was like this:

it "should redirect to #index after item creation" do
  @model.stubs(:create!)
  post :create
  response.should redirect_to(:action => :index)
end

And this spec failed because of MethodNotAllowed: Only get and post requests are allowed.

WTF? It's the very basic functionality. Why does it fail to work ?

After half a day of investigation I found out that this is a bug in rspec-rails package and redirect_to matcher fails to handle hash-like url specifier.

Case 2.

I wanted to handle AR::RecordInvalid exceptions with one handler to DRY up controller code. I wrote corresponding "rescue_from" call in controller and wrote specs (in fact, the reverse order: specs then code). And those specs failed too.

The other several hours of investigation revealed hidden option: RSpec-rails overrides default Rails' error handling (which is based on ActiveSupport's #rescue_from feature) with pass-through exception handler, and you have to write following code to make YOUR exception handlers work:
before do
  controller.use_rails_error_handling!
end

Yeah, Rails is nice, but upgrading to a new version is a pain.

2 комментария:

Анонимный комментирует...

Может ето только у меня,
но в Opera в теле rss полно тагов,
читать сложно.

Unknown комментирует...

Похоже, это проблема Opera. Я попробовал в Safari - он понимает, что там на самом деле HTML. Про Opera - не знаю.

Я, на самом деле, не в восторге от этого блоггера, ибо нету нормальных инструментов делать вставки кода, подсветки кода и т.п. Мне этот блоггер взял и обернул каждую строчку в DIV, чем нарушил порядок вложенности тэгов (т.к. я сам руками указываю начало и конец PRE областей. Пришлось потом руками чистить нагенеренный HTML =(