rails 3 undefined method `+' for nil:NilClass -
i making sorting table in rails , getting error. index.html.erb
<table> <tr> <th><%= sortable "name" %></th> <th><%= sortable "city" %></th> <th><%= sortable "country" %></th> <th><%= sortable "street_address" %></th> <th><%= sortable "sector" %></th> <th><%= sortable "telephone" %></th> <th><%= sortable "fax" %></th> </tr> <% company in @companies %> <tr> <td><%= company.name %></td> <td><%= company.city %></td> <td><%= company.country %></td> <td><%= company.street_address %></td> <td><%= company.sector %></td> <td><%= company.telephone %></td> <td><%= company.fax %></td> </tr> <% end %> </table>
this companies_controller
def index @companies = company.order(params[:sort] + ' ' + params[:direction]) end
this application_helper
def sortable(column, title = nil) title ||= column.titleize direction = (column == params[:sort] && params[:direction] == "asc") ? "desc" : "asc" link_to title, :sort => column, :direction => direction end
and error is:
nomethoderror in companiescontroller#index undefined method `+' nil:nilclass app/controllers/companies_controller.rb:21:in `index'
what problem , how can fix it?
your params[:sort]
returns nil
.
you can fix example checking params:
@companies = company.scoped if params[:sort].present? && params[:direction].present? @companies = @companies.order(params[:sort] + ' ' + params[:direction]) end
Comments
Post a Comment