class ArticlesController < ApplicationController
def index
@articles = Article.by_popularity
if params[:category] == 'popular'
@articles = @articles.by_popularity
end
if params[:category] == 'recent'
@articles = @articles.by_recent
end
if params[:category] == 'local'
index_by_local and return
end
if params[:genre]
index_by_genre and return
end
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @articles }
end
end
def index_by_local
# 10 lines of code here
render :template => 'articles/index_by_local'
end
def index_by_genre
# ANOTHER 10 lines of code here
render :template => 'articles/index_by_genre'
end
end
正如你可以從上面看到的。我的控制器不完全是薄。它所做的是取決於傳遞的參數,它與模型交互以過濾記錄。如果params[:local]
或params[:genre]
通過了,然後分別調用它自己的方法(def index_by_local
和def index_by_genre
)做進一步處理。這些方法也加載自己的模板,而不是index.html.erb
。重構此控制器?
這對控制器來說很典型嗎?或者我應該重構這個不知何故?