Ruby on rails 如何根据asc/desc排序显示箭头(RoR 4)
正如标题所说,我试图显示向上/向下箭头。目前正在Ruby 2.0/Rails 4.0上构建 我跟随railscasts对表列进行排序(),但他当然使用表,所以他的箭头显示得很好 我想使用div/span/其他任何东西。(这不是很多数据,也不是需要放在表中的那种数据。) 也就是说,我为我的箭头创建了一个应用程序助手方法,它们可以工作,但是如果我为asc单击一个,两个箭头都指向上。如果我为desc单击一个,两个都指向下。显然,因为Ruby on rails 如何根据asc/desc排序显示箭头(RoR 4),ruby-on-rails,ruby,sorting,Ruby On Rails,Ruby,Sorting,正如标题所说,我试图显示向上/向下箭头。目前正在Ruby 2.0/Rails 4.0上构建 我跟随railscasts对表列进行排序(),但他当然使用表,所以他的箭头显示得很好 我想使用div/span/其他任何东西。(这不是很多数据,也不是需要放在表中的那种数据。) 也就是说,我为我的箭头创建了一个应用程序助手方法,它们可以工作,但是如果我为asc单击一个,两个箭头都指向上。如果我为desc单击一个,两个都指向下。显然,因为params[:direction]被设置为asc或desc,所以两个
params[:direction]
被设置为asc或desc,所以两个箭头都被设置。我怎样把它们分开?在一些伪代码中:
if title && asc
sort by title and asc && show up arrow (for title only)
if title && desc
sort by title and desc && show down arrow (for title only)
if date posted && asc
sort by created_by and asc && show up arrow (for date posted only)
etc.
我真的不想有一个巨大的if/then条件语句,但我想要一些更简单的语句
(如果有必要的话,我会用一张桌子作为分类依据:部分,但这看起来真的非常愚蠢,这是最后的手段……)
以下是我得到的代码:
show.html.erb
<h3>Images</h3>
<% if @user.images.any? %>
<div class="sortable"><%= sortable "img_name", "Title" %><%= arrow %> |
<%= sortable "created_at", "Date posted" %><%= arrow %>
</div>
<%= render @images %>
<%= will_paginate @images %>
<% end %>
def sortable(column, title = nil)
title ||= column.titleize
css_class = column == sort_column ? "current #{sort_direction}" : nil
direction = column == sort_column && sort_direction == "asc" ? "desc" : "asc"
link_to title, {sort: column, direction: direction}, {class: css_class}
end
def arrow
if params[:direction] == 'asc'
image_tag("arrow_up.png", alt: "up arrow", size: "15x15")
elsif params[:direction] == 'desc'
image_tag("arrow_down.png", alt: "down arrow", size: "15x15")
else
# either a blank image to show or just force no-display of image
end
end
def show
@user = User.find(params[:id])
@images = @user.images.paginate(page: params[:page]).order(sort_column + " " + sort_direction)
end
private
def sort_column
Image.column_names.include?(params[:sort]) ? params[:sort] : "created_at"
end
def sort_direction
%w[asc desc].include?(params[:direction]) ? params[:direction] : "asc"
end
def sortable(column, title = nil)
title ||= column.titleize
css_class = column == sort_column ? "current #{sort_direction}" : nil
direction = column == sort_column && sort_direction == 'asc' ? 'desc' : 'asc'
capture do
link_to({sort: column, direction: direction}, {class: css_class}) do
concat title
concat " "
concat arrow(column)
end
end
end
def arrow(column)
return '' if params[:sort] != column
arrow = params[:direction] == 'asc' ? 'down' : 'up'
"<i class='fa fa-caret-#{arrow}'></i>".html_safe
end
<%= sortable "img_name", "Title" %>
用户\u控制器.rb
<h3>Images</h3>
<% if @user.images.any? %>
<div class="sortable"><%= sortable "img_name", "Title" %><%= arrow %> |
<%= sortable "created_at", "Date posted" %><%= arrow %>
</div>
<%= render @images %>
<%= will_paginate @images %>
<% end %>
def sortable(column, title = nil)
title ||= column.titleize
css_class = column == sort_column ? "current #{sort_direction}" : nil
direction = column == sort_column && sort_direction == "asc" ? "desc" : "asc"
link_to title, {sort: column, direction: direction}, {class: css_class}
end
def arrow
if params[:direction] == 'asc'
image_tag("arrow_up.png", alt: "up arrow", size: "15x15")
elsif params[:direction] == 'desc'
image_tag("arrow_down.png", alt: "down arrow", size: "15x15")
else
# either a blank image to show or just force no-display of image
end
end
def show
@user = User.find(params[:id])
@images = @user.images.paginate(page: params[:page]).order(sort_column + " " + sort_direction)
end
private
def sort_column
Image.column_names.include?(params[:sort]) ? params[:sort] : "created_at"
end
def sort_direction
%w[asc desc].include?(params[:direction]) ? params[:direction] : "asc"
end
def sortable(column, title = nil)
title ||= column.titleize
css_class = column == sort_column ? "current #{sort_direction}" : nil
direction = column == sort_column && sort_direction == 'asc' ? 'desc' : 'asc'
capture do
link_to({sort: column, direction: direction}, {class: css_class}) do
concat title
concat " "
concat arrow(column)
end
end
end
def arrow(column)
return '' if params[:sort] != column
arrow = params[:direction] == 'asc' ? 'down' : 'up'
"<i class='fa fa-caret-#{arrow}'></i>".html_safe
end
<%= sortable "img_name", "Title" %>
我不理解你不愿意在这里用桌子。即使是一个小表,这仍然是表格数据,包含行、列和标题。为什么要用类似于表的div结构替换语义表结构 也就是说,我将扩展arrow帮助程序以发送箭头用于哪个列:
def arrow(column)
# as you're not sorting on the column, you don't want to see the arrow at
# all of the header is not for the sorted column
return '' if params[:sort] != column
# ...
end
导致模板看起来像:
<%= sortable "img_name", "Title" %><%= arrow "img_name" %>
产生更简单的模板:
<%= sortable "img_name", "Title" %>
如果其他人想使用glyphicon而不是像我这样的图像,这个解决方案可能会很有用。在这个例子中,我使用 应用程序\u helper.rb
<h3>Images</h3>
<% if @user.images.any? %>
<div class="sortable"><%= sortable "img_name", "Title" %><%= arrow %> |
<%= sortable "created_at", "Date posted" %><%= arrow %>
</div>
<%= render @images %>
<%= will_paginate @images %>
<% end %>
def sortable(column, title = nil)
title ||= column.titleize
css_class = column == sort_column ? "current #{sort_direction}" : nil
direction = column == sort_column && sort_direction == "asc" ? "desc" : "asc"
link_to title, {sort: column, direction: direction}, {class: css_class}
end
def arrow
if params[:direction] == 'asc'
image_tag("arrow_up.png", alt: "up arrow", size: "15x15")
elsif params[:direction] == 'desc'
image_tag("arrow_down.png", alt: "down arrow", size: "15x15")
else
# either a blank image to show or just force no-display of image
end
end
def show
@user = User.find(params[:id])
@images = @user.images.paginate(page: params[:page]).order(sort_column + " " + sort_direction)
end
private
def sort_column
Image.column_names.include?(params[:sort]) ? params[:sort] : "created_at"
end
def sort_direction
%w[asc desc].include?(params[:direction]) ? params[:direction] : "asc"
end
def sortable(column, title = nil)
title ||= column.titleize
css_class = column == sort_column ? "current #{sort_direction}" : nil
direction = column == sort_column && sort_direction == 'asc' ? 'desc' : 'asc'
capture do
link_to({sort: column, direction: direction}, {class: css_class}) do
concat title
concat " "
concat arrow(column)
end
end
end
def arrow(column)
return '' if params[:sort] != column
arrow = params[:direction] == 'asc' ? 'down' : 'up'
"<i class='fa fa-caret-#{arrow}'></i>".html_safe
end
<%= sortable "img_name", "Title" %>
def可排序(列,标题=nil)
title | |=column.titleize
css\u class=column==sort\u column?“当前#{sort_direction}”:无
方向=列==排序\列和排序\方向=='asc'?'描述:'asc'
俘获
链接到({sort:column,direction:direction},{class:css\u class})do
康卡特头衔
海螺“
concat箭头(列)
结束
结束
结束
def箭头(列)
如果参数[:排序]!=柱
箭头=参数[:方向]=“asc”?”向下“:“向上”
“”.html\u安全
结束
show.html.erb
<h3>Images</h3>
<% if @user.images.any? %>
<div class="sortable"><%= sortable "img_name", "Title" %><%= arrow %> |
<%= sortable "created_at", "Date posted" %><%= arrow %>
</div>
<%= render @images %>
<%= will_paginate @images %>
<% end %>
def sortable(column, title = nil)
title ||= column.titleize
css_class = column == sort_column ? "current #{sort_direction}" : nil
direction = column == sort_column && sort_direction == "asc" ? "desc" : "asc"
link_to title, {sort: column, direction: direction}, {class: css_class}
end
def arrow
if params[:direction] == 'asc'
image_tag("arrow_up.png", alt: "up arrow", size: "15x15")
elsif params[:direction] == 'desc'
image_tag("arrow_down.png", alt: "down arrow", size: "15x15")
else
# either a blank image to show or just force no-display of image
end
end
def show
@user = User.find(params[:id])
@images = @user.images.paginate(page: params[:page]).order(sort_column + " " + sort_direction)
end
private
def sort_column
Image.column_names.include?(params[:sort]) ? params[:sort] : "created_at"
end
def sort_direction
%w[asc desc].include?(params[:direction]) ? params[:direction] : "asc"
end
def sortable(column, title = nil)
title ||= column.titleize
css_class = column == sort_column ? "current #{sort_direction}" : nil
direction = column == sort_column && sort_direction == 'asc' ? 'desc' : 'asc'
capture do
link_to({sort: column, direction: direction}, {class: css_class}) do
concat title
concat " "
concat arrow(column)
end
end
end
def arrow(column)
return '' if params[:sort] != column
arrow = params[:direction] == 'asc' ? 'down' : 'up'
"<i class='fa fa-caret-#{arrow}'></i>".html_safe
end
<%= sortable "img_name", "Title" %>
Hm,我试过这个,但没有成功。两支箭仍然同时指向上或下。我决定只为排序链接创建一个表,并尝试以这种方式工作:一旦我让它完全工作,我会发布我的解决方案。我想你一定错过了什么(或者我错过了什么)。请注意箭头辅助对象中的第一行。应该只有一个箭头,即当前正在排序的列的箭头。如果列不是要排序的列,则返回空字符串而不是图像标记。