I must have really screwed something up big time. I was trying to
modify SortHelper to work on multiple columns and now I can’t get
anything to work even though I reverted all my changes.
I keep getting the error:
can’t convert Symbol into Integer
C:/Users/Eric
Powell/Documents/ruby/my1fnc/app/helpers/sort_helper.rb:108:in []' C:/Users/Eric Powell/Documents/ruby/my1fnc/app/helpers/sort_helper.rb:108:in
sort_clause’
C:/Users/Eric
Powell/Documents/ruby/my1fnc/app/controllers/posts_controller.rb:29:in
`index’
The offending line in sort_helper.rb is:
result = session[@sort_name][:key] + ’ ’ +
session[@sort_name][:order]
Here’s my entire SortHelper module:
module SortHelper
require ‘active_support/inflector’
Initializes the default sort column (default_key) with the following
options:
- :default_order – the default sort order ‘asc’ or ‘desc’. Defaults
to
‘asc’.
- :name – the name of the session hash entry that stores the sort
state.
Defaults to ‘<controller_name>_sort’.
- :icons_dir – directory with sort direction icons. Defaults to
/images
def sort_init(default_key, options={})
options = { :default_order => ‘asc’,
:name => params[:controller] + ‘_sort’,
:icons_dir => ‘/images’,
}.merge(options)
@sort_name = options[:name]
@sort_default = {:key => default_key, :order =>
options[:default_order]}
@icons_dir = options[:icons_dir]
end
Updates the sort state. Call this in the controller prior to calling
sort_clause.
def sort_update()
if params[:sort_key]
sort = {:key => params[:sort_key], :order => params[:sort_order]}
elsif session[@sort_name]
sort = session[@sort_name] # Previous sort.
else
sort = @sort_default
end
session[@sort_name] = sort
end
Returns an SQL sort clause corresponding to the current sort state.
Use this to sort the controller’s table items collection.
def sort_clause()
result = session[@sort_name][:key] + ’ ’ +
session[@sort_name][:order]
result if result =~ /^[\w_]+ (asc|desc)$/i # Validate sort.
end
Returns a link which sorts by the named column.
- column is the name of an attribute in the sorted record
collection.
- The optional text explicitly specifies the displayed link text.
- A sort icon image is positioned to the right of the sort link.
def sort_link(column, text=nil, options=nil)
key, order = session[@sort_name][:key], session[@sort_name][:order]
if key == column
if order.downcase == ‘asc’
icon, order = ‘sort_asc.png’, ‘desc’
else
icon, order = ‘sort_desc.png’, ‘asc’
end
else
icon, order = nil, ‘asc’
end
text = ActiveSupport::Inflector::titleize(column) unless text
params = {:params => {:sort_key => column, :sort_order => order } }
params = params.merge(options[:params]) if options[:params]
link_to(text, params) +
(icon ? nbsp(2) + image_tag(File.join(@icons_dir,icon)) : ‘’)
end
def sort_header_tag(column, options = {})
text = options.delete(:text) ||
ActiveSupport::Inflector::titleize(column.humanize)
options[:title]= “Sort by #{text}” unless options[:title]
text = options[:title] || options.delete(:text) ||
ActiveSupport::Inflector::titleize(column.humanize)
content_tag(‘th’, sort_link(column, text, options), options)
end
private
# Return n non-breaking spaces.
def nbsp(n)
' ' * n
end
end
and here’s the relevent code from my controller:
def index
sort_init ‘name’
sort_update
if not params[:q].blank?
options = { :conditions => params[:q] }
end
options = { :order => sort_clause }
@clause = options[:order]
#@posts = Post.all
@posts = Post.find(:all, :order => sort_clause)
@posts = Post.find(:all, options)
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @posts }
end
end