in my list action, I setup the sort parameters as folows :
..
sort = case @params[''sort'']
when "login" then "login DESC"
when "first_name" then "first_name DESC"
when "last_name" then "last_name DESC"
end
...
to be used in a ''sort_link_helper''
def sort_link_helper(text, param)
key = param
key += "_reverse" if @params[:sort] == param
options = {
:url => {:action => ''list'', :params =>
@params.merge({:sort =>
key, :page => nil})},
:update => ''zList'',
:before => "Element.show(''spinnerT'')",
:success => "Element.hide(''spinnerT'')"
}
html_options = {
:title => "Sort this column",
:href => url_for(:action => ''list'', :params
=>
@params.merge({:sort => key, :page => nil}))
}
link_to_remote(text, options, html_options)
end
In the list view I display :
HEADERS
<td align="center" <%= sort_td_class_helper
"login" %>>
<%= sort_link_helper "Login", "login" %>
</td>
<td align="center" <%= sort_td_class_helper "role"
%>>
<%= sort_link_helper "Role", "role" %>
</td>
<td align="center" <%= sort_td_class_helper
"first_name" %>>
<%= sort_link_helper "First Name", "first_name"
%>
</td>
<td align="center" <%= sort_td_class_helper
"last_name" %>>
<%= sort_link_helper "Last Name", "last_name" %>
</td>
<td align="center" <%= sort_td_class_helper
"trustee" %>>
<%= sort_link_helper "Trustee", "trustee" %>
</td>
BODY
<td width="20%" align="center"><%= h(user.login)
%></td>
<td width="20%" align="center"><%=
h(user.roles[0].name) %></td> #1st
role is main role
<td width="30%" align="center"><%=
h(user.first_name) %></td>
<td width="20%" align="center"><%=
h(user.last_name) %></td>
user and roles are joined by a many-to-many relationship (roles_users
table)
Its running very well... but I would like to be able to sort on the role
column...
when "role" then "role DESC" doesn''t work
obviously
I got a MYSQL error
Mysql::Error: Unknown column ''role'' in ''order
clause'': SELECT * FROM
users roles ORDER BY role DESC LIMIT 0, 5
thanks for your lights..
kad
--
Posted via http://www.ruby-forum.com/.