2012-11-30 64 views
0

我有一項任務來排列學生名單。我有三個屬性的姓名,年齡實體學生,學生的city.My控制器頁面如下如何在rails上對ruby中的值列表進行排序?

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> 

我如何可以排序列表學生在軌道上的紅寶石升序?請幫助我。

+0

好的。我接受。謝謝 – Niths

+0

29%仍然很低... –

回答

2

您可以用這種方式使用Active Record Query Interface

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: find in conjunction with :order and :group

+0

當我導入這個我會得到一個箭頭按鈕列出名稱升序和降序>>>排序:真,/ /啓用排序 defaultSorting:'name ASC 」, – Niths