我有基于Foundation5框架的以下索引, 我想使用F5 subnav对索引进行排序:
class PostsController < ApplicationController
def index
if params[:tag].present?
@posts = Post.includes(:tags, :user).tagged_with(params[:tag]).page(params[:page])
elsif params[:sort] == 'votes'
@posts = Post.order("cached_votes_up DESC").includes(:tags, :user).page(params[:page])
elsif params[:query] == 'unauthored'
@posts = Post.includes(:tags, :user).where("author_id IS ?", nil).order("created_at ASC").page(params[:page])
elsif params[:query] == 'hot'
@posts = Post.includes(:tags, :user).tagged_with(:hot).order("created_at ASC").page(params[:page])
else
@posts = Post.order("created_at DESC").includes(:tags, :user).page(params[:page])
end
end
查看
<dl class="sub-nav right">
<dd class="active"><%= link_to 'newest', questions_path %></dd>
<dd><a href="#"><%= link_to 'votes', questions_path(:sort => "votes") %></a></dd>
<dd><a href="#"><%= link_to 'advice', questions_path(query: 'hot') %></a></dd>
<dd><a href="#"><%= link_to 'unanswered', questions_path(query: 'unauthored') %></a></dd>
</dl>
简而言之,单击subnav链接会将我带到索引页面,其中传递的查询字符串如下:
/posts/?query=hot
/posts/?query=unauthored
/posts/?sort=votes
问题是,如何动态分配“有效”&#39;基于查询字符串? dd 元素的类?
我使用以下助手为基于路径的另一个subnav:
def subnav_link(link_text, link_path)
class_name = current_page?(link_path) ? 'active' : ''
content_tag(:dd, :class => class_name) do
link_to link_text, link_path
end
end
但我无法弄清楚如何修改查询字符串。
答案 0 :(得分:0)
我基本上没有意识到它就回答了我自己的问题!以下是它的工作原理:
查看:
<%= subnav_link 'newest', questions_path(:sort => 'newest') %>
<%= subnav_link 'votes', questions_path(:sort => 'votes') %>
<%= subnav_link 'unauthored', questions_path(query: 'unauthored') %>
<%= subnav_link 'unanswered', questions_path(query: 'hot') %>