IT Blog, Web Development,

Ride Those Rails With Style!

2 m read
QArea Expert
QArea Expert Marketing Manager
September 14, 2015
Share on
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 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[…].

Example:
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!

Share on
Privacy Preference Center
Your Privacy

When you visit any web site, it may store or retrieve information on your browser, mostly in the form of cookies. This information might be about you, your preferences or your device and is mostly used to make the site work as you expect it to. The information does not usually directly identify you, but it can give you a more personalized web experience. Because we respect your right to privacy, you can choose not to allow some types of cookies. Click on the different category headings to find out more and change our default settings. However, blocking some types of cookies may impact your experience of the site and the services we are able to offer.

Strictly Necessary Cookies Always Active

These cookies are necessary for the website to function and cannot be switched off in our systems. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.

You can set your browser to block or alert you about these cookies, but some parts of the site will not then work. These cookies do not store any personally identifiable information.

Performance Cookies

These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us to know which pages are the most and least popular and see how visitors move around the site.

All information these cookies collect is aggregated and therefore anonymous. If you do not allow these cookies we will not know when you have visited our site, and will not be able to monitor its performance.

Cookies used

Google Analytics
Functional Cookies

When you visit any web site, it may store or retrieve information on your browser, mostly in the form of cookies. This information might be about you, your preferences or your device and is mostly used to make the site work as you expect it to. The information does not usually directly identify you, but it can give you a more personalized web experience. Because we respect your right to privacy, you can choose not to allow some types of cookies. Click on the different category headings to find out more and change our default settings. However, blocking some types of cookies may impact your experience of the site and the services we are able to offer.

Cookies used

Test Second Test
Targeting Cookies

When you visit any web site, it may store or retrieve information on your browser, mostly in the form of cookies. This information might be about you, your preferences or your device and is mostly used to make the site work as you expect it to. The information does not usually directly identify you, but it can give you a more personalized web experience. Because we respect your right to privacy, you can choose not to allow some types of cookies. Click on the different category headings to find out more and change our default settings. However, blocking some types of cookies may impact your experience of the site and the services we are able to offer.

More Information

When you visit any web site, it may store or retrieve information on your browser, mostly in the form of cookies. This information might be about you, your preferences or your device and is mostly used to make the site work as you expect it to. The information does not usually directly identify you, but it can give you a more personalized web experience. Because we respect your right to privacy, you can choose not to allow some types of cookies. Click on the different category headings to find out more and change our default settings. However, blocking some types of cookies may impact your experience of the site and the services we are able to offer.