Ride Those Rails With Style!

QArea Expert by QArea Expert on September 14, 2015

Ride Those Rails With Style!
Reading Time: 2 minutes

Imagine a world where you can easily deflect any challenge your RoR project throws at you. That perfect world can indeed become a reality once you put your mind to it. Ruby on rails development includes numerous simple tricks and hacks that can dramatically assist developers. The only trouble is that useful things are often too tiny to be spotted at the right moment. That is exactly why you require to know all of them from the start. And, preferably, even before that.

I couldn’t be more honest if I told you that I wish I’ve known the following tips at my time. They would have saved me a lot of time, effort and headache. And to make the long story short let’s dive directly into some RoR greatness:

  • Custom apps that are rails-based often generate content on their own. And they tend to insert HTML as any CMS like WordPress, etc. All of that sounds lovely and magical until a certain point when you will require to truncate some content to get a clear display on the search results page or anything of the same sort. Hear those bells? They say you’re in trouble! Sure you could use a simple .html_safe but all you will get as a result is a divthat will never be closed. You truncated it, did you not? And now you sit there and look at a nightmare that’s going on live and the worst part is – you’re the one who’s to fix the mess. BTW the fix is quite simple, just consider the following: <%=truncate((sanitize blog.body,:tags=>%w(img)),:length=>600)%> and you will be able to include and exclude precise HTML tags of your own will and choice.
  • URL structure changes can be done the hard way or our way despite all possible reasons you have to make the change in the first place. As you probably know changing URL names is a hell of a deal and involves more pain than your body placed in a tub full of fire ants. So let’s imagine you have an URL like /gallery/title-slugand you must change it into /portfolio/title-slug. You can change the model name in any way you were going to do it, or you can use the following in your routes: resources :portfolio, :controller => ’gallery’ and march onward to victory. One more friendly advice – use the link_to URL method in your project. Why? because that’s how you fix all from just one place. In other cases you will probably be forced to manually find and change each link which is time-consuming, yet still less troublesome than the alternatives. As a pleasant addition – if you forget one URL somewhere nothing will get broken.
  • Here’s one more nice tip that can assist you with creation of a real-deal hash from a particular list of values via Hash[…].

Hash[‘key1’, ‘value1’, ‘key2’, ‘value2’]
# => {“key1”=>”value1”, “key2”=>”value2”}

  • Watch out for latest versions of Rails. They often come with bugs and glitches version 1.9.3 had an issue with taps/heroku/ruby which lead to numerous Data Base-related trouble and the quickest fix lied in degrading to version 1.9.2. New stuff may seem cool and all but you should really take your time before diving heads-in into new glory, otherwise you might get yourselves a serious bump on your head.

Do you have any other cool RoR tricks up your sleeves? Please share them through the comments!