Wyświetlanie postów z grudzień, 2011

Validation error objects (and hashes)

TL;DR. You can add Hash or custom class instances as error. It does not have to be a String.

Let's start with a simple Rails code that uses standard validation errors and see what kind of problems we have with such approach.

This is how it looks like when validation error occurs:

Let's just say that for a better ui experience we want to change the formatting of such
validation message (ex. numbers can be bold so that user pays attention to them). However, current solution does not allow us to do it easily. That is because of the fact that error is simply a String. Also the whole message is generated on the model side and just displayed on the view. This is a very quick and simple approach but does not give as a wide spectrum for improvement. Fortunately our errors does not to be of such simple type. We can put there something more complex like a Hash.

Looks perfect but sadly this implementation leads to I18n::InvalidPluralizationData exception so we must get a little dirty t…

Communication between controllers and views via symbol messages

Sometimes a new, simple idea allows us to see the world better and understand the limitations that previously we did not even know we have. We are all accustomed to code like this:

The problem is that sometimes we want to format the flash message. Maybe part of the text needs to be in bold. Maybe we want to display it entirely different. In such case we just need to remind ourselves that there is no need to generate and put the message into flash object in the controller. Maybe we should not generate such informational messages in controllers at all. If that action was performed by mobile device you probably would not generate the message that phone is supposed to display to the user. That does not feel right, does it? Instead, you would provide enough information so that the device can provide a meaningful message. Also, it would be the job of the phone to display it in a proper language. So, why don't we try the same approach with browsers.

And here is the final effect:

Despite the…

convert: Unrecognized option (+repage)

If you are getting "convert: Unrecognized option (+repage)" error for example from paperclip gem it probably means that you need to install imagemagick package instead of relaying on graphicsmagick-imagemagick-compat package that is in Ubuntu and provides binary named same way.