A step to a journey of thousands of miles

The initial

I've been always thinking that a tech post should be a thing that is very profound. It should resolve a crucial programming or software engineer issue.

But I am so wrong on that. In reality (by now), most of issues that are just normal, requiring to google and or read documentations to figure out. But I still gained lots of pleasures by resovling them. Therefore I start to record the issues that I meet and how I figure it out.


The first issue

A few weeks ago I was building a dashboard in Rails. To increase the users' happniness I implement the avatar upload function by Paperclips. Parts of the code look like:

has_attached_file :avatar,
    :styles => { medium: "128x128#" },
    :path => ":rails_root/public/:url",
    :url  => "/avatars/:id/:style/:filename"

Clearly I was hoping to organise the path and url better. The issue was that default_url could be not found as the path is redefined. On production the abominated broken image will be shown.

After a few attempts to fix the url and path, but had no luck. The default_url method is overall the places.

My solution

When cannot resolve an issue in a way more than 20~30 mins, I tend to think of it from different ange. The default image is stored in the repository and Rails surely knows where it is. Why don't I just write my own default_url method? So the final codes are

def avatar_url
    current_user.avatar.present? ? current_user.avatar.url(:medium) : "avatar_missing.png"

Problem sovled! :D