2014-07-10 4 views
0

센터 목록을 생성하고 있지만 센터는 보로의 자식이므로보기에서 알파벳 순서로 정렬하는 방법을 궁금해하고 있습니다.레일보기에서 사전 순으로 목록을 정렬하는 방법

내 코드는 내가 이렇게 어떻게, 내가 venue.title는 알파벳 순서 싶습니다이

 <% boroughs.each do |borough|%> 
      <% if borough.leisure_centres.any? %> 
      <% borough.leisure_centres.each do |venue| %> 
      <li class="sitemap-accordion"> 
       <%= link_to venue.title, '#', class: "trigger" %> 
       <ul class="sitemap-accordion-child"> 
        <li> 
        <%= link_to "Overview", centre_path(venue) %> 
        </li> 
        <li> 
        <%= link_to 'News', centre_news_index_path(venue) %> 
        </li> 
        <li> 
        <%= link_to 'Facilities', facilities_centre_path(venue) %> 
        </li> 
        <li> 
        <%= link_to 'Contact Us', new_centre_contact_form_path(venue) %> 
        </li> 
        <% unless venue.venue_hire_content.nil? %> 
        <li> 
         <%= link_to 'Hire', venue_hire_centre_path(venue) %> 
        </li> 
        <% end %> 
        <% unless venue.virtual_tour.nil? %> 
        <li> 
         <%= link_to 'Virtual Tour', tour_centre_path(venue) %> 
        </li> 
        <% end %> 
        <% unless venue.custom_pages.nil? %> 
        <% venue.custom_pages.each do |custom_page| %> 
         <li> 
         <%= link_to custom_page.title, centre_custom_page_path(centre_id: venue, id: custom_page.id) %> 
         </li> 
        <% end %> 
        <% end %> 
       </ul> 
       <% end %> 
      </li> 
      <% end %> 
     <% end %> 

처럼 보인다?

감사합니다.

+0

'leisure_centre'는 (는) 자신의 모델입니까? – dax

+0

좋은 외침을 외쳐라. 그래서, 나는보기에서 그것을 어떻게합니까? –

답변

2

나는 4 개

class Borough < ActiveRecord::Base 
    has_many :leisure_centres, -> { order "title asc" } 
end 

레일

class Borough < ActiveRecord::Base 
    has_many :leisure_centres, :order => "title asc" 
end 

3 그래서 당신은 루비하지 SQL을 통해 정렬되어

레일을 할 것입니다.

+0

"구문 오류, 예기치 않은 ->, 기대하는 키워드 _ has_many : leisure_centres -> {order"title asc "}" –

+0

사용중인 루비 버전은 무엇입니까? –

+0

최신, 루비 2.1.2 –

2

다음과 같이 sort_by 메소드에 블록을 전달할 수 있습니다.

<% borough.leisure_centres.sort_by(&:title).each do |venue| %> 
+1

11 초 나를 이길! – dax

+0

예. 그것이 가까웠습니다 : P – Santhosh

+0

네,하지만 자치구별로 레저 센터를 분류 할 수 있습니까? –

관련 문제