Search code examples
ruby-on-railssorting

Why am I getting this wrong number or argument column sort error?


I followed this sortable column tutorial http://railscasts.com/episodes/228-sortable-table-columns?view=asciicast The column sorting worked fine until I added in the helper section. I now get this error:

index.html.erbwhere line #20 raised:

wrong number of arguments (given 1, expected 3..4)
Extracted source (around line #2):
              
module SortHelper
  def sortable(relation, column, title, options = {})
    matching_column = column.to_s == params[:sort]
    direction = if matching_column
      (params[:direction] == "asc") ? "desc" : "asc"
    else # Ascending by default if switching columns

This is the index.html:

              <tr>
    line 20     <th><%= sortable "name" %></th>
                <th><%= sortable "street" %></th>
                <th><%= sortable "city" %></th>
                <th><%= sortable "state" %></th>
                <th><%= sortable "zip" %></th>
                <th><%= sortable "list" %></th>
                <th><%= sortable "website" %></th>
              </tr>

This is the helper:

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

Code added to controller:

  helper_method :sort_column, :sort_direction (at top)

  def index
    @clubs = Club.order(params[:sort])
  end


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

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

The error is saying it should have 3-4 arguments but the helper is only for 2 at most. So what gives? What am I missing?


Solution

  • This is because your

    def sortable(relation, column, title, options = {})

    expects 3 required params: relation, column and title, and 1 optional options

    But you call this method with just 1 argument sortable "name"