Rails Best Practices

9 votes

13435 views

Put scripts at the bottom

Do you experience that your website renders slow due to loading a lot of javascripts, especially loading some third-party javascripts? Move script tags to the bottom of body can speed up the render of your website.

Comments

flyerhzm

11 votes

6423 views

use OpenStruct when advance search

use OpenStruct when advance search

Comments

alvin2ye

0 votes

55325 views

Fetch current user in models

I don't remember how many times I need to fetch current user in models, such as audit log. Here is a flexible way to set the current user in and fetch the current user from User model.

Comments

flyerhzm

16 votes

10913 views

Use say and say_with_time in migrations to make a useful migration log

Use say_with_time and say in migrations will produce a more readable output in migrations. And if use correctly it could be a helpful friend when something goes wrong because normally it is stored in the deploy log

implemented

Comments

Guillermo

13 votes

39298 views

Use STI and polymorphic model for multiple uploads

This is a flexible and reusable solution for multiple uploads, using STI model to save all the uploaded assets in one "assets" table and using polymorphic model to reuse "Asset" model in different uploadable models.

Comments

flyerhzm

2 votes

6523 views

to_s/to_s(:short)

override the to_s to make the method sensible instead of "display_name", "format_name"..

Comments

by yincan
yincan

10 votes

8676 views

Use css sprite automatically

Using css sprite can reduce a large number of http requests, so it makes the web page loaded much faster. It it painful to composite a lot of images manually, do it automatically.

Comments

flyerhzm

8 votes

23534 views

Use I18n.localize for date/time formating

For reliable formatting of a date/time string in the desired language, use I18n.localise, Time#strftime can cause u unnecessary headache.

Comments

by ngty
ngty

4 votes

18587 views

Use multipart/alternative as content_type of email

Rails uses plain/text as the default content_type for sending email, you should change it to multipart/alternative that email clients can display html formatted email if they support and display plain text email if they don't support html format.

implemented

Comments

flyerhzm

17 votes

11378 views

Use asset_host for production server

Use asset host for cookie-free domains for components, that make your components load faster.

Comments

flyerhzm