Ruby on Rails表未正确排序 [英] Ruby on Rails table not sorting properly

查看:100
本文介绍了Ruby on Rails表未正确排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Ruby on Rails页面,我试图在4列上设置可排序标题.我使用了 http://railscasts.com/episodes/228-sortable-table-columns作为指导.我确定我缺少一些小东西,但是排序完全在成绩和输入字段上进行,因为它们不在关联表中.但是,当我尝试对user和dept字段进行排序时,它只会按升序排序.当我在url中手动键入"desc"并发送该参数时,排序有效,但条件语句似乎不适用于employee和department字段.另外,当我单击user或dept标题时,它在"created_at"旁边显示了排序箭头,因此似乎在辅助函数中默认了一些东西,但我不知道为什么这样做.有人在代码中看到任何错误吗?

I have a Ruby on Rails page that I am trying to set up with sortable headers on 4 columns. I used http://railscasts.com/episodes/228-sortable-table-columns as a guide. I'm sure I'm missing something small, but the sorting works completely on the grade and entered fields as they are not in associated tables. However, when I try to sort on the user and dept fields, it only sorts ascending. The sorting works when I manually type in "desc" in the url and send that parameter, but the conditional statements don't seem to work for the employee and department fields. Also, when I click on the user or dept header, it shows the sorting arrow next to "created_at", so it seems to be defaulting some things in the helper function but I can't find out why it's doing that. Anyone see anything wrong in the code?

Index.html.erb

    <table class="table table-striped table-hover">
      <thead>
        <tr>
          <th><%= sortable "user", "Employee" %></th>
          <th><%= sortable "dept", "Department" %></th>
          <th><%= sortable "grade", "Grade" %></th>
          <th><%= sortable "created_at", "Entered" %></th>
        </tr>
      </thead>
      <tbody>
      <% @transactions.each do |transaction| %>    
          <tr>
           <td><%= link_to transaction.user.fl_name, transaction, :target => '_blank' %></td>
           <td><%= transaction.user.department.name %></td>
           <td><span class="<%= 'text-success bold' if transaction.grade >=
             90 %>"><%= transaction.grade %></span></td>
           <td><%= transaction.created_at.strftime("%b %d, %Y %l:%M %P")  %></td>
          </tr>

      <% end %>
     </tbody>
      </table>

控制器

helper_method :sort_column, :sort_direction

def index
if (has_option("edit_option") == true) || (current_user.is_admin?)
  if params[:sort] == "user"
    @transactions = sort_by_user
  elsif params[:sort] == "dept"
    @transactions = sort_by_dept
  else
    @transactions = sort
  end
  respond_to do |format|
    format.html # index.html.erb
    format.json { render json: @transactions }
  end
else
  redirect_to root_path
end
end

def sort
  transaction.order(sort_column + " " + sort_direction).page(params[:page])
end

def sort_by_user
  transaction.paginate(
      :page => params[:page],
      :include => :user,
      :order => "users.first_name #{sort_direction}")
end

def sort_by_dept
  transaction.paginate(
      :page => params[:page],
      :include => :user,
      :order => "users.department_name #{sort_direction}")
 end

def sort_column
  transaction.column_names.include?(params[:sort]) ? params[:sort] : "created_at"
end

def sort_direction
  %w[asc desc].include?(params[:direction]) ? params[:direction] : "desc"
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

推荐答案

direction = column == sort_column && sort_direction == "asc" ? "desc" : "asc"

由于第一部分column == sort_column,此条件始终为假(css_class也是如此). sort_column检查dept/user是否为transaction.column_names之一,并且由于不是,它返回默认值created_at,从而导致条件始终被评估为false并返回asc.您要做的就是更改sort_column,因此它也适用于transaction.column_namesuser/dept.或者只是

This condition is always false because of first part column == sort_column (the same happens for css_class as well). sort_column checks if dept/user is one of the transaction.column_names and since it isn't, it returns default created_at, causing your conditions to always be evaluated to false and return asc. All you have to do is change sort_column, so it works for transaction.column_names and user/dept as well. Or just

def sort_column
  ['user', 'dept', 'created_at', 'grade'].include?(params[:sort]) ? params[:sort] : "created_at
end

这篇关于Ruby on Rails表未正确排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆