排序选择Ruby On Rails

时间:2010-10-21 19:07:41

标签: ruby-on-rails

ROR newby问题:

我有一个控制器,其行动如下:

# GET /organisations/new
# GET /organisations/new.xml
def new
  @organisation = Organisation.new
  @organisationtypes = Organisationtype.find(:all) 

  respond_to do |format|
    format.html # new.html.erb
    format.xml  { render :xml => @organisation }
  end
end

并在组织/新视图中选择organisationtype,如:

<p>
<%= f.label :organisationtype %><br />
<%= select "organisation", "organisationtype_id", Organisationtype.find(:all).collect {|p| [ p.organisationtype, p.id ] }, {:include_blank => true } %>
</p>

我正在努力按照组织类型来命令选择。我该怎么做?

感谢您的时间

嗅探

2 个答案:

答案 0 :(得分:2)

尝试使用:

<%= select "organisation", "organisationtype_id", Organisationtype.find(:all,:order => "organisationtype DESC").collect {|p| [ p.organisationtype, p.id ] }, {:include_blank => true } %>

OR

<%= select "organisation", "organisationtype_id", Organisationtype.find(:all,:order => "organisationtype ASC").collect {|p| [ p.organisationtype, p.id ] }, {:include_blank => true } %>

答案 1 :(得分:0)

尝试使用:

Organisationtype.find(:all).sort{ |a,b| a.title <=> b.title }.collect...