我正在使用以下内容使用户能够按日期(ASC),日期(DESC)和投票对评论进行排序:
posts_controller.rb:
def show
@post = Post.find(params[:id])
@comments = @post.comments.paginate(:page => params[:page],
:per_page => 5).order(params[:order_by])
@comment = @post.comments.build
end
视图/帖/ show.html.erb:
<div class="comments-tabs">
<span><%= link_to 'Votes', post_path(@post, :order_by => "total_votes DESC") %></span>
<span><%= link_to 'Date (ASC)', post_path(@post, :order_by => "created_at ASC") %></span>
<span><%= link_to 'Date (DESC)', post_path(@post, :order_by => "created_at DESC") %></span>
</div>
网址最终看起来像这样:
http://localhost:3000/posts/59?order_by=total_votes+DESC
http://localhost:3000/posts/59?order_by=created_at+ASC
http://localhost:3000/posts/59?order_by=created_at+DESC
我想创建一个if语句来向当前标签添加一个类。例如:
http://localhost:3000/posts/59?order_by=created_at+ASC
<span class="comment-tab"><%= link_to 'Votes', post_path(@post, :order_by => "total_votes DESC") %></span>
<span class="comment-tab current"><%= link_to 'Date (ASC)', post_path(@post, :order_by => "created_at ASC") %></span>
<span class="comment-tab><%= link_to 'Date (DESC)', post_path(@post, :order_by => "created_at DESC") %></span>
答案 0 :(得分:1)
你可以使用current_page吗?确定用户是否正在查看某个页面并根据该页面应用html类的方法。
以下是一个例子:
%ul
%li{:class => "#{current_page?(root_path) ? "selected" : ""}"}= link_to content_tag(:span, "Home"), root_path
%li{:class => "#{current_page?(birth_certificate_path) ? "selected" : ""}"}= link_to content_tag(:span, "Birth Certificates"), birth_certificate_path
%li{:class => "#{current_page?(marriage_certificate_path) ? "selected" : ""}"}= link_to content_tag(:span, "Marriage Certificates"), marriage_certificate_path
%li{:class => "#{current_page?(death_certificate_path) ? "selected" : ""}"}= link_to content_tag(:span, "Death Certificates"), death_certificate_path
答案 1 :(得分:0)
link_to_unless_current
方法
<%=
link_to_unless_current("Comment", { :controller => "comments", :action => "new" }) do
link_to("Go back", { :controller => "posts", :action => "index" })
end
%>
答案 2 :(得分:0)
这是一个很好的问题。你真让我思考。我想在我自己的项目中完成类似的事情,重构一些臭的if语句,当它的包含链接等于当前页面时用于设置div或li的样式。
我用咖啡味的jQuery写了这个。试一试。
应用程序/资产/ Javascript角/ current_tab.js.coffee
$(document).ready ->
$('.comment-tab').each ->
if $(this).children('a').first().attr('href') is window.location.href
$(this).addClass('current')
在视图中将post_path更改为post_url,它应该可以正常工作。
应用程序/视图/帖/ show.html.haml
.comments-tabs
%span= link_to 'Votes', post_url(@post, order_by: 'total_votes DESC')
%span= link_to 'Date (ASC)', post_url(@post, order_by: 'created_at ASC')
%span= link_to 'Date (DESC)', post_url(@post, order_by: 'created_at DESC')
答案 3 :(得分:0)
<div class="comments-tabs">
<span><%= link_to 'Votes', post_path(@post, :order_by => "total_votes DESC"), {}, {:class => "#{params[:order_by] == 'total_votes+DESC' ? 'comment_tab current' : 'comment_tab'}"} %></span>
<span><%= link_to 'Date (ASC)', post_path(@post, :order_by => "created_at ASC"), {}, {:class => "#{params[:order_by] == 'created_at+ASC' ? 'comment_tab current' : 'comment_tab'}"} %></span>
<span><%= link_to 'Date (DESC)', post_path(@post, :order_by => "created_at DESC"), {}, {:class => "#{params[:order_by] == 'created_at+DESC' ? 'comment_tab current' : 'comment_tab'}"} %></span>
</div>
未测试。