0

我的任务是对学生名单进行排序。我有一个实体学生,具有三个属性名称、年龄、城市。我的学生控制器页面如下所示

class StudentsController < ApplicationController
   def list
    @students = Student.all
    @jtable = {'Result' => 'OK','Records' => @students.map(&:attributes)}

    respond_to do |format|
      format.html # index.html.erb
      format.json { render :json => @jtable}
    end
  end
  # GET /students/new
  # GET /students/new.json
  def new
    @student = Student.new
    respond_to do |format|
      format.html # new.html.erb
      format.json { render :json => @student }
    end
  end
  # GET /students/1/edit
  def edit
    @student = Student.find(params[:id])
  end

  def editstudent
    @student = Student.find(params[:id])
      puts @student.inspect
    respond_to do |format|
      if @student.update_attributes(params[:student])
        puts '+++++++'

        @jtable = {'Result' => 'OK'}
        format.html { redirect_to @student, :notice => 'Student was successfully updated.' }
        format.json { render :json => @jtable }
        else
         @jtable = {'Result' => 'ERROR','Message'=>'Empty'}
          format.html { render :action => "edit" }
          format.json { render :json => @jtable.errors, :status => :unprocessable_entity }
      end
    end
  end
  # DELETE /students/1
  # DELETE /students/1.json
  def deletestudent
    @student = Student.find(params[:id])
    @student.destroy
    @jtable = {'Result' => 'OK'}

    respond_to do |format|

      format.json { render :json => @jtable }
    end
  end
end

index.html.erb 文件是

<html>
    <head>
        <%=stylesheet_link_tag "jtable_blue","http://code.jquery.com/ui/1.9.1/themes/base/jquery-ui.css","validationEngine.jquery",:media => "all" %>
        <%= javascript_include_tag "jquery-1.8.3.min","http://code.jquery.com/ui/1.9.1/jquery-ui.js","jquery.jtable.min","jquery.validationEngine","jquery.validationEngine-en"%>
<script type="text/javascript">

        var _gaq = _gaq || [];
        _gaq.push(['_setAccount', 'UA-22169554-3']);
        _gaq.push(['_trackPageview']);

        (function () {
            var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
            ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
            var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
        })();

    </script>

<style>
        #QuickImageLinks
        {
            text-align: center;
        }

        #QuickImageLinks img
        {
            border: none;
            margin: 0px;
            padding: 0px;
        }

        .child-opener-image
        {
            cursor: pointer;
        }

        .child-opener-image-column
        {
            text-align: center;
        }

        .jtable-dialog-form
        {
            min-width: 220px;
        }

        .jtable-dialog-form input[type="text"]
        {
            min-width: 200px;
        }

    </style>
    </head>
    <body>
     <div class="site-container">

    <div class="content-container">

    <div id="StudentTableContainer">
    </div>

<script type="text/javascript">
    jQuery.noConflict();
    jQuery(document).ready(function () {
        jQuery('#StudentTableContainer').jtable({
            title:'StudentList',
            sorting: true, //Enable sorting
            defaultSorting: 'name ASC', //Set default sorting
            actions: {
                listAction: '/students/list',
                createAction: '/students/newstudent',
                updateAction: '/students/editstudent',
                deleteAction: '/students/deletestudent'
            },
            fields: {
                id: {
                    key: true,
                    create: false,
                    edit: false,
                    list: false
                },
                 addresses: {
                    title: '',
                    width: '5%',
                    sorting: false,
                    edit: false,
                    create: false,
                    listClass: 'child-opener-image-column',
                    display: function (studentData) {
                        //Create an image that will be used to open child table
                        var $img = $('<img class="child-opener-image" src="/assets/phone.png" title="Edit Address" />');
                        //Open child table when user clicks the image
                        $img.click(function () {
                             jQuery('#StudentTableContainer').jtable('openChildTable',
                                    $img.closest('tr'),
                                    {
                                        title: studentData.record.name + ' - Addresses',
                                        actions: {
                                            listAction: '/addresses/AddressList?student_id=' + studentData.record.id,
                                            deleteAction: 'addresses/DeleteAddress',
                                            updateAction: 'addresses/UpdateAddress',
                                            createAction: 'addresses/CreateAddress'
                                        },
                                        fields: {
                                           student_id: {
                                                type: 'hidden',
                                                defaultValue: studentData.record.id
                                            },
                                            id: {
                                                key: true,
                                                create: false,
                                                edit: false,
                                                list: false
                                            },
                                            hname: {
                                                title: 'HouseName',
                                                width: '30%',
                                            },
                                            place: {
                                                title: 'Place',
                                                width: '30%'
                                            },
                                            state: {
                                                title: 'State',
                                                width: '30%'
                                            }
                                            },
                                             formCreated: function (event, data) {
                                             data.form.validationEngine();
                                             },
                                            formSubmitting: function (event, data) {
                                             return data.form.validationEngine('validate');
                                            },
                                            formClosed: function (event, data) {
                                             data.form.validationEngine('hide');
                                             data.form.validationEngine('detach');
                                    }
                                    }, 
                                    function (data) { //opened handler
                                        data.childTable.jtable('load');
                                    });
                        });
                        //Return image to show on the person row
                        return $img;
                    }
                },
                name: {
                    title: 'Name',
                    width: '40%'
                },
                age: {
                    title: 'Age',
                    width: '20%'
                },
                city: {
                    title: 'City',
                    width: '30%'
                }

            }
        });

       jQuery('#StudentTableContainer').jtable('load');
    });
</script>
</div></div></body></html>

如何在 ruby​​ on rails 中按升序对学生列表进行排序?请帮助我。

4

1 回答 1

2

您可以通过这种方式使用Active Record 查询接口

Student.order("name ASC") # Returns all students sorted in ascending order by name

Student.find(:all, :order => "age DESC") # Returns all students sorted by age in descending order

查看此内容以查找和排序嵌套模型。Rails Active Record:与 :order 和 :group 一起查找

于 2012-11-30T10:23:32.427 回答