Rails Best Practices

8 votes

21420 views

speed up assets precompile with turbo-sprockets-rails3

Rails is integrated with sprockets from 3.1, which gives you the power to pre-process, compress and minify your assets. It's awesome, but it slows down deployment a lot.

implemented

Comments

flyerhzm

1 votes

20886 views

Check the return value of "save", otherwise use "save!"

The "save" method on ActiveRecord returns "false" and does nothing if the record is invalid. You should always check the return value, otherwise you may inadvertently not save the record. If you think the record can never be invalid, or don't want to check the return value, use "save!"

implemented

Comments

flyerhzm

10 votes

41038 views

Don't rescue Exception, rescue StandardError

In C# or Java, you can `catch (Exception)` to rescue all exception types. However, in Ruby you should almost never catch `Exception`, but only catch `StandardError`.

implemented

Comments

by ricbrad
ricbrad

6 votes

24849 views

Protect mass assignment

Rails mass assignment feature is really useful, but it may be a security issue, it allows an attacker to set any models' attributes you may not expect. To avoid this, we should add attr_accessbile or attr_protected to all models.

implemented

Comments

flyerhzm

27 votes

29314 views

Not use time_ago_in_words

It's very common for a rails developer to use time_ago_in_words to display time like "5 minutes ago", but it's too expensive to calculate the time in server side, you should utilize client cpu to calculate the time ago.

implemented

Comments

flyerhzm

14 votes

21997 views

Restrict auto-generated routes.

By default, Rails generates seven RESTful routes(new,edit,create,destroy,index,show, update) for a resource, sometime the resource only needs one or two routes, so just user :only or :except while defining routes to speedup the routing.

implemented

Comments

by yorzi
yorzi

-1 votes

10009 views

Remove tab

Using tabs can mess up the spacing since some IDE's use 4 spaces for a tab, while others use 2, and some people don't use tabs at all, a mix of tabs and spaces causes things to not line up in most cases.

implemented

Comments

flyerhzm

13 votes

13746 views

Remove empty helpers

If you use rails generator to create scaffolds or controllers, it will also create some helpers, most of the helpers are useless, just remove them.

implemented

Comments

flyerhzm

9 votes

17524 views

Simplify render in controllers

Like the simplify render in views, from rails 2.3, we can also simplify render in controllers.

implemented

Comments

flyerhzm

15 votes

35332 views

Simplify render in views

render is one of the often used view helpers, we can pass object, collection or local variables. From rails 2.3, more simplified syntax for render are provided.

implemented

Comments

flyerhzm