我将过滤功能放入应用程序(Rails 4.1beta) - 我通过在Item模型上创建作用域,通过请求参数传递作用域并在索引行动。这一切都有效,但是我试图摆脱其中一个控制器的索引操作中的代码味道;减少轨道控制器中的作用域过滤器
def index
case params[:scope]
when "recent"
@items = Item.recent
when "active"
@items = Item.active
when "inactive"
@items = Item.inactive
else
@items = Item.all
end
end
这一切都觉得有点过于僵硬/冗长。我真的很想做这样的事情;
def index
@items = Item.send(params[:scope])
end
但是随后我将应用程序全部公开给调用Item类的方法的人。在那里的打击条件有点击败了我想要实现的目标。
是否有一些轨道魔法我错过了,可以帮助我吗?