In my articles_controller I have the follow definition
def index
@tags = Article.tag_counts_on(:keywords) || ''
klass = Article
klass = klass.tagged_with(@keyword) if (@keyword = params[:keyword]).present?
@articles = klass.paginate(:page => params[:page])
@articles = Article.where(:state => '4').paginate(:page => params[:page], :per_page => 10)
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @articles }
end
end
and in my views/articles/index.html.erb I have the code:
<% @tags.sort_by(&:count).reverse.each do |k| %>
<% url_opts = {:action => "index", :controller => "articles"}
link_name = "#{k.name} (#{k.count})" %>
<% if @keyword == k.name %>
<%= link_to link_name, url_opts.merge(:keyword => nil), :class => "tag current_tag", :title => "Click again to see all" %>
<% else %>
<%= link_to link_name, url_opts.merge(:keyword => k.name), :class => "tag", :title => "Click to filter by #{k.name}" %>
<% end %>
<% end %>
I use Ruby 1.9.2, Rails 3.0.11 and in Gemfile the gem act-as-taggable-on and the follow code
rails generate acts_as_taggable_on:migration
create tables tags and taggings
In my logs I have this error:
Rendered articles/index.html.erb within layouts/application (57.4ms) Completed 500 Internal Server Error in 189ms
ActionView::Template::Error (You have a nil object when you didn't expect it! You might have expected an instance of Array. The error occurred while evaluating nil.sort_by)
The variable @tags is an Array? and have a nil object?