2016-11-25 79 views
0

這是在軌應用範圍,我的第一次。我試圖在我的章節模型中通過名爲:priority(一個整數)的字段對章節列表進行排序。我已經查看了範圍文檔,但我似乎無法弄清楚如何讓功能工作。軌道4範圍不正確排序

模型

class Chapter < ActiveRecord::Base 
belongs_to :book 
scope :priority_sort, -> { order(priority: :asc) } 
end 

控制器

@chapters = Chapter.all.priority_sort 

和視圖

<% @book.chapters.each do |chapter| %> 
    <%= link_to chapter.title, [@book, chapter] %> 
<% end %> 

的觀點看什麼看法目前看起來像 優先/ CHAPTER_TITLE

-15 
    About the authors 

    3 
    Chapter 18 Equal pay 

    -13 
    Chapter 2 Overview 

    -4 
    Chapter 11 Non-exempt employees: determining work time 

    -11 
    Chapter 4 Workers not covered by the FLSA 

是什麼樣的一個default_scope { order("priority ASC") }

-15 
    About the authors 

    -14 
    Chapter 1 Snapshot 

    -13 
    Chapter 2 Overview 

    -12 
    Chapter 3 Covered employers 

    -11 
    Chapter 4 Workers not covered by the FLSA 

缺少什麼我在這裏?

回答

2
<% @book.chapters.each do |chapter| %> 
<%= link_to chapter.title, [@book, chapter] %> 
<% end %> 

這是一個錯誤?如果不是這樣,那是因爲你在控制器中的某些東西上沒有使用。即

@chapters = Chapter.priority_sort.all

如果沒有,那麼你可以把它改成

<% @book.chapters.priority_sort.each do |chapter| %> 
    <%= link_to chapter.title, [@book, chapter] %> 
<% end %>