Tuesday, September 8, 2015

It wasn't off the rails... I was!

Remember last post? It wasn't Ruby running off the rails. I was just me?

It seems I forgot a basic rule: go through assets pipeline!

When you're dealing with non-public assets, always use the assets pipeline, for it is going to solve assets location disregarding the web context.

Consider the case of the last post again, so we can understand exactly what happened.

In my bootstrap carousel images I was saying something like

<img src="carousel/image001.jpg" />

When I was visiting the page by the root route "/" my src element was pointing correctly to the image resource and the image was displaying correctly. In my routes.rb the root route was

root 'welcome#index'

Rails maps this to the URI "/", of course. Then we should have "/" and "/welcome/index" routing to the same point, and so we have. but  just inside Rails, and this is the point here.

When I do not use assets pipeline, bu try to point to a certain asset myself, as I did, you bypass Rails and then your are depending on another thing: the we context! 

And, as it happens, the web contexts for "/" and "/welcome/index" are different, so my images disappeared in the second case, even when displaying correctly in the first one.

The solution?

Easy as pie! Do as I said and go through the assets pipeline. In other word, go through Rails framework and say something like

<%= image_tag('carousel/img001.jpg') %>

With this you'll be saying to Rails something like "Hey! Find my asset your way. It's your business, not mine!"

Hope this may help other in the future. And please, send me your doubts when you think Ruby is running off the rails. I can't tell you I'll have a solution for all cases, but I may tell you I'll research a lot before giving up.

Friday, September 4, 2015

When things go off the rails

... and then I was dealing with my website http://borntoraiseheaven.org, which I intend to make available to public in a few days. Everything is ok and I am now joining the pieces together. This is the image of the homepage.



As you may see, this this homepage is the view Welcome#index. And, of course, my config/routes.rb says

get 'welcome/index'
root 'welcome#index'

as it must be. But that single button saying "Homepage" is defined by

<a class="btn btn-block" href="/welcome/index">Homepage</a>

and then points to the same root route, right?

Then, why when I click the button I got this?


Yeah! My beautiful bootstrap carousel now displays no images!

I saw the trace of the server, I asked some expert friends... nothing! Nobody may point me a reason for this to happen!

Then I suddenly realized that sometimes things go off the rails. And I mean no pun when I say this! It's is not just Ruby on Rails, but every programming language sometimes goes off the rails and does exactly what we don't want it to do.

Why does this happens? Who knows! Maybe only God, in His infinite wisdom, knows the reason why programming languages sometimes tend to make us crazy.

This blog is focused on Ruby on Rails, but sometimes I'll talk about other programming languages I use to deal with, like PHP and Javascript...

Oh, please, don't get me started about Javascript! According to AndrĂ© Guanabara, a friend of mine from Rio de Janeiro, Javascript is a language that tends to make you go from heaven to hell, and all the way back to heaven, in just a few moments. A very powerful language, indeed, but also one of the hardest to deal with, according to his opinion and mine. Even when you use things like jQuery, a life-saving library. jQuery surely saved the sanity of many  programmers, but it also have its tricks.

These things happen for us to understand our mothers when they told us to choose simple and lucrative professions, such as brain surgery ... That's what I sometimes think.

By the way... may someone help me with my bootstrap carousel?