flyerhzm
Login flyerhzm
Website http://www.huangzhimin.com/
13 votes
4 comments
4026 views
Use asset_host for production server
Use asset host for cookie-free domains for components, that make your components load faster.
4 votes
2 comments
6212 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.
implemented8 votes
2 comments
4566 views
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.
10 votes
32 comments
12377 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.
0 votes
18 comments
16170 views
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.
8 votes
1 comments
4845 views
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.
14 votes
3 comments
17959 views
There are a few posts told you how to integrate bundler into capistrano, but they are out of date now. After bundler 1.0 released, you can add only one line in capistrano to use bundler.
implemented8 votes
7 comments
4132 views
Use batched finder for large data query
If you want to do a large data query such as finding all the 10,000,000 users to send email to them, you should use batched finder to avoid eating too much memory.
9 votes
4 comments
5148 views
If you want to generate different urls according to different objects, you should use the polymorphic_path/polymorphic_url to simplify the url generation.
19 votes
10 comments
7520 views
Do you always check if ActiveRecord's attributes exist or not by nil?, blank? or present? ? Don't do that again, rails provides a cleaner way by query attribute
implemented4 votes
14 comments
5103 views
Select specific fields for performance
In a system like forum, the title and body is displayed on show page, but only title is on index page. You should use select in query to speed up the query and save memory.
26 votes
12 comments
11183 views
Memoization is an optimization technique used primarily to speed up computer programs by having function calls avoid repeating the calculation of results for previously-processed inputs. In rails, you can easily use memoize which is inherited from ActiveSupport::Memoizable.
14 votes
9 comments
9105 views
Trailing whitespace always makes noises in version control system, it is meaningless. We should remove trailing whitespace to avoid annoying other team members.
implemented10 votes
18 comments
9743 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.
implemented7 votes
6 comments
6042 views
Simplify render in controllers
Like the simplify render in views, from rails 2.3, we can also simplify render in controllers.
implemented2 votes
9 comments
5599 views
Ruby/Rails provides a lot of magic codes, especially for metaprogramming, they are powerful, less codes to implement more functions, but they are not intuition, you should write good comment for your magic codes.
13 votes
4 comments
3510 views
If you want to process something expensive as part of a web request, it will delay the response. If the job is not critical, it's wiser to move the expensive to a background queue and returns the response immediately.
20 votes
21 comments
9646 views
Are you tired of going to schema.rb to find your table structures information? It would be better to list all the attributes of the model in the model itself.
13 votes
6 comments
5302 views
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.
implemented20 votes
6 comments
7820 views
split route namespaces into different files
the routes will become complicated with the growth of your application, contain different namespaces, each with a lot of resources and custom routes, it would be better to split routes into different files according to the namespaces, which makes it easy to maintain the complicated routes.
2 votes
5 comments
3562 views
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.
implemented3 votes
3 comments
4200 views
rails migration provides a convenient way to alter database structure, you can easily add, change and drop column to a existing table, but when the data in existing table are huge, it will take a long time to alter existing table, you should try to merge/optimize the db alter sql statements.
5 votes
7 comments
6636 views
Use cells to abstract view widgets
Rails developers always pay more attentions on models and controllers refactoring, they don't take care about views modularization, that makes view codes most difficult to maintain. Here I recommend you to use cells gem to write more reuseable, testable and cacheable view codes.
