Skip to content

Move code into helper

Richard Huang edited this page Aug 15, 2010 · 4 revisions

Please go to http://rails-bestpractices.com/posts/26-move-code-into-helper

Before:


<%= select_tag :state, options_for_select( [[t(:draft), "draft"],
                                            [t(:published), "published"]],
                                           params[:default_state] ) %>

After:


<%= select_tag :state, options_for_post_state(params[:default_state]) %>

# app/helpers/posts_helper.rb
def options_for_post_state(default_state)
  options_for_select( [[t(:draft), "draft"], [t(:published), "published"]],
                      default_state )
end