Sorting selects within Ruby On Rails
ROR newby question:
I've got a controller with the following action:
# 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
and a select within the organisations/new view for organisationtype like so:
<p>
<%= f.label :organisationtype %><br />开发者_开发知识库;
<%= select "organisation", "organisationtype_id", Organisationtype.find(:all).collect {|p| [ p.organisationtype, p.id ] }, {:include_blank => true } %>
</p>
I'm struggling to order the select by organisationtype. How would I do this?
Thanks for your time
Sniffer
Try it with:
<%= 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 } %>
Try it with:
Organisationtype.find(:all).sort{ |a,b| a.title <=> b.title }.collect...
精彩评论