Sunday 15 June 2014

ruby - Implementing sort ASC or DESC in Rails -



ruby - Implementing sort ASC or DESC in Rails -

i working open source school management software, fedena , trying sort list of users surnames. software shows users first names. i've found these 2 files responsible showing info intend change.

student_controller.rb

def list_students_by_course @students = student.find_all_by_batch_id(params[:batch_id], :order => 'last_name asc') render(:update) { |page| page.replace_html 'students', :partial => 'students_by_course' } end

when delete above section of file, users names don't show believe section responsible populating table usernames.

_students_by_course.erb

<div class="students-table"> <table align="center" width="100%" cellpadding="1" cellspacing="1"> <tr class="tr-head"> <td><%= t('sl_no') %></td> <td><%= t('name') %></td> <td><%= t('adm_no') %></td> <td></td> </tr> <tr> <td></td> <td></td> <td></td> <td></td> </tr> <% @students.each_with_index |r, i| %> <tr class="tr-<%= cycle('odd', 'even') %>"> <td class="col-1"> <%= i+1 %> </td> <td class="col-2"> <%= link_to r.full_name,:controller => "student", :action => "profile", :id => r.id %> </td> <td class="col-1"> <%= r.admission_no %> </td> <td class="col-7"> <%= link_to "#{t('view_profile')}", :controller => "student", :action => "profile", :id => r.id %> </td> </tr> <% end %> </table> </div>

i have tried changing 'last_name asc' 'last_name desc' nil changed. help appreciated.

you've clarified in comments want not sort order (i take you've figured out?) format of full name displayed each instance of model student.

it's not hard if traced source. let's start view, since that's see.

@students.each_with_index |r, i|

we start loop, each iteration of processes entry r index i. since we're looping on collection of students (seems valid assumption), r instance of student. problematic line is:

<%= link_to r.full_name,:controller => "student", :action => "profile", :id => r.id %>

actually, should @ r.full_name since that's link label. it's student's method. here is.

def full_name "#{first_name} #{middle_name} #{last_name}" end

you might wondering sec space, since such implementation implies if middle_name absent, we'd have two. @ source of page , you'll see there two! in order alter how total name looks, you'll have modify method.

ruby-on-rails ruby

No comments:

Post a Comment