简体   繁体   中英

Sorting a Select_tag with haml and Ruby On Rails

I am trying to sort the output of a select_tag by the name field

%p= select_tag :template, template_options

With the following helper code

def template_options
  options_from_collection_for_select ExportTemplate.all, :id, :name
end

but I can't work out how to do this or whether I should do it in the helper or the view.

Use order

def template_options
  options_from_collection_for_select ExportTemplate.order('name ASC'), :id, :name
end

It really depends. I tend to use helpers when I'm going to be using the same code in multiple places to DRY (Don't repeat yourself) code up. If you're only using it the once, I'd recommend maybe just placing it inline rather than moving it off to a helper.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM