Dodjs
Dodjs

Reputation: 819

can't convert Symbol into Integer


I created a simple tables sorter:

ProductsController:   
 helper_method :sort_column, :sort_direction
  # GET /products
  # GET /products.xml
  def index
    @products = Product.order(sort_column+ " "+sort_direction)

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @products }
    end
  end

Application_helper

module ApplicationHelper

  def sortable(column, title = nil )
    title ||= column.titleize
    direction = column == sort_column && sort_direction[:direction] == "asc" ? "desc" : "asc"
    link_to title, {:sort => column, :direction => direction}
  end
end

And view:

 <tr>
    <th><%= sortable "name"%></th>
    <th><%= sortable "price"%></th>
    <th><%= sortable "released" %></th>
  </tr>

After I tried open /products page but I got this error (Rails v 3.0.3):

   can't convert Symbol into Integer
Extracted source (around line #5):

2: 
3: <table>
4:   <tr>
5:     <th><%= sortable "name"%></th>
6:     <th><%= sortable "price"%></th>
7:     <th><%= sortable "released" %></th>
8:   </tr>

And sort methods:

 def sort_column
    Product.column_names.include?(params[:sort]) ? params[:sort] : "name"
  end

  def sort_direction
    %w[asc desc].include?(params[:direction]) ? params[:direction] : "asc"
  end

Upvotes: 2

Views: 12985

Answers (1)

Dylan Markow
Dylan Markow

Reputation: 124419

direction = column == sort_column && sort_direction[:direction] == "asc" ? "desc" : "asc"

Should probably be changed to this. sort_direction looks like it's a string, but you're treating it like a hash.

direction = column == sort_column && sort_direction == "asc" ? "desc" : "asc"

Upvotes: 4

Related Questions