我想根据rails中的特定key = value参数触发不同的排序查询。我的语法需要帮助。我希望你能看到我想要实现的目标吗?
控制器中的:
def index
if params[:sort = originality?].present?
#perform your logic with sort
end
#Other logic
end
def index
if params[:sort = dynamics?].present?
#perform your logic with sort
end
#Other logic
end
在视图中:
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Rankingz <b class="caret"></b></a>
<ul class="dropdown-menu">
<!-- We are passing the sort parameter here -->
<li><a href="?sort=originality">Originality</a></li>
<li><a href="?sort=dynamics">Dynamics</a></li>
<li><a href="?sort=Execution">Execution</a></li>
<li><a href="?sort=Battle">Battle</a></li>
<li><a href="?sort=Votes">Votes</a></li>
</ul>
</li>
答案 0 :(得分:1)
您不应再次定义索引操作,将索引操作更改为:
def index
if params[:sort].present?
sort_fields = ['originality', 'dynamics', 'execution', 'battle', 'votes']
@instance_var = ModelName.order(params[:sort]) if sort_field.include?(params[:sort])
end
@instance_var ||= ModelName.all #ensure if the instace variable is not nil.
end