モデルに次のフィールドがある場合:
class Book < ActiveRecord::Base
attr_accessible :author, :title, :year, :month
scope :recent, order('year desc, month desc').limit(3)
end
このデータを年別に表示したい場合、新しいコントローラーを作成して年ごとに表示する方がよいでしょうか? または、ルートを使用してこれを行う必要がありますか?
モデルに次のフィールドがある場合:
class Book < ActiveRecord::Base
attr_accessible :author, :title, :year, :month
scope :recent, order('year desc, month desc').limit(3)
end
このデータを年別に表示したい場合、新しいコントローラーを作成して年ごとに表示する方がよいでしょうか? または、ルートを使用してこれを行う必要がありますか?
いくつかのアクションではなく、ルートまたはクエリ文字列による方が良いと思います。異なるルートまたはパラメーターに従って、1 つのアクションから応答を送信するだけです。
SOは同じことを行います(ルートによる):
いいえ、現在のビューとコントローラーを再利用する必要があります。index
最も簡単な方法は、アクションにパラメーターを追加することです。
class BooksController < ApplicationController
def index
if params[:year]
@books = Book.where(:year => params[:year])
else
@books = Book.scoped
end
end
end
Also, it might be a good idea to move this filter to the Book
model. If you find yourself adding a lot of filters to this action, you can refactor the filtering logic to another class.