Fork me on GitHub

controller

    Don't modify the params hash

    18 Sep 2013

    David Davis (ddavis1@gmail.com)

    The params hash contains all the data that was submitted from a request. If you modify it, later code won't have access to it. Instead, copy the params hash and modify the copy. Read More

    Tags 


    Create base controller

    02 Jun 2011

    Guo Lei (guolei9@gmail.com)

    Have base controllers for DRY Read More

    Tags 


    Simplify render in controllers

    12 Dec 2010

    Richard Huang (flyerhzm@gmail.com)

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

    Tags 


    Substituting before_filter :load_object

    12 Oct 2010

    (mail@dennisbloete.de)

    Instead of loading an object with a before_filter you can use a more intelligent helper_method to get the main object for the controller context. Read More

    Tags 


    Use before_filter

    24 Jul 2010

    Wen-Tien Chang (ihower@gmail.com)

    Don't repeat yourself in controller, use before_filter to avoid duplicated codes. Read More

    Tags 


    Move code into controller

    24 Jul 2010

    Wen-Tien Chang (ihower@gmail.com)

    According to MVC architecture, there should not be logic codes in view, in this practice, I will introduce you to move codes into controller. Read More

    Tags 


    DRY Controller (and debate)

    24 Jul 2010

    Wen-Tien Chang (ihower@gmail.com)

    For CRUD resources, we always write the same 7 actions with duplicated codes. To avoid this, you can use inherited_resources plugin. But be careful, there is DRY controller debate!! (http://www.binarylogic.com/2009/10/06/discontinuing-resourcelogic/) 1. You lose intent and readability 2. Deviating from standards makes it harder to work with other programmers 3. Upgrading rails trouble Read More

    Tags 


    Love named_scope

    23 Jul 2010

    Wen-Tien Chang (ihower@gmail.com)

    named_scope is awesome, it makes your codes much more readable, you can also combine named_scope finders to do complex finders. Read More

    Tags 


    Nested Model Forms

    22 Jul 2010

    Wen-Tien Chang (ihower@gmail.com)

    Use accepts_nested_attributes_for to make nested model forms much easier, this feature is provided by rails 2.3 Read More

    Tags 


    model.collection_model_ids (many-to-many)

    22 Jul 2010

    Wen-Tien Chang (ihower@gmail.com)

    When you want to associate a model to many association models by checkbox on view, you should take advantage of model.collection_model_ids to reduce the code in controller. Read More

    Tags 


    Use model callback

    21 Jul 2010

    Wen-Tien Chang (ihower@gmail.com)

    Use model callback can avoid writing some logic codes in controller before or after creating, updating and destroying a model. Read More

    Tags 


    Replace Complex Creation with Factory Method

    21 Jul 2010

    Wen-Tien Chang (ihower@gmail.com)

    Sometimes you will build a complex model with params, current_user and other logics in controller, but it makes your controller too big, you should move them into model with a factory method Read More

    Tags 


    Move Model Logic into the Model

    21 Jul 2010

    Wen-Tien Chang (ihower@gmail.com)

    In MVC model, controller should be simple, the business logic is model's responsibility. So we should move logic from controller into the model. Read More

    Tags 


    Add model virtual attribute

    21 Jul 2010

    Wen-Tien Chang (ihower@gmail.com)

    Do not assign the model's attributes directly in controller. Add model virtual attribute to move the assignment to model. Read More

    Tags 


    Use scope access

    20 Jul 2010

    Wen-Tien Chang (ihower@gmail.com)

    You can use scope access to avoid checking the permission by comparing the owner of object with current_user in controller. Read More

    Tags 


    Use model association

    19 Jul 2010

    Wen-Tien Chang (ihower@gmail.com)

    Use model association to avoid assigning reference in controller. Read More

    Tags 


    Move finder to named_scope

    14 Jul 2010

    Wen-Tien Chang (ihower@gmail.com)

    Complex finders in controller make application hard to maintain. Move them into the model as named_scope can make the controller simple and the complex find logics are all in models. Read More

    Tags