0
votes

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?

2

2 Answers

1
votes

This error is a common Rails (probably Ruby, actually) confuser that just means @tags was nil when you called the sort_by method ... and I guess the error is trying to be helpful since sort_by is a method of Array.

So, why is @tags nil? Fire up rails console (in your project directory) and execute Article.tag_counts_on('something') -- where 'something' is a keyword.

Perhaps you meant in the first line to get keywords from the params array?

@tags = Article.tag_counts_on(params[:keywords])

Also, you need to handle the case where no tags are found, right?

0
votes

Finaly the solution for this error for me was: NOT DRY. When add the code from def index to def all on articles_controller my problem disapear.

my new articles_controller

        def index
    @tags = Article.tag_counts_on(:keywords)
    klass = Article
    klass = klass.tagged_with(@keyword) if (@keyword = params[:keyword]).present?
    @articles = klass.where(:state => '4').paginate(:page => params[:page])
    respond_to do |format|
        format.html  # index.html.erb
        format.xml  { render :xml => @articles }
    end
    end                

        def all            
    @tags = Article.tag_counts_on(:keywords)
    klass = Article
    klass = klass.tagged_with(@keyword) if (@keyword = params[:keyword]).present?
    @articles = klass.where(:state => ['3', '4']).search(params[:search]).order('accepted desc').paginate(:page => params[:page], :per_page => 10)
            respond_to do |format|
        format.html { render 'index' }
        format.xml  { render :xml => @articles }
    end
    end