acts_as_taggable, undefined method 'empty?'

I've tried out 'acts_as_taggable', 'acts_as_taggable_on' and
'acts_as_taggable_on_steroids' and all of them output "undefined
method 'empty?'" with the tag_cloud action.

I am following the guides precisely. Yet I can find no references to
this error anywhere, so I must be doing something wrong...

I am on rails 2.3.5, and my current attempt is with
acts_as_taggable_on

I've generated the migration and ran it, I can add tags through my
edit view, and I can view them either from the console or as a simple
list in my index.

"include ActsAsTaggableOn::TagsHelper" is inside my application_helper

  def tag_cloud
    @tags = Blog.tag_counts_on(:tags)
  end

is in my controller, and

<% tag_cloud(@tags, %w(css1 css2 css3 css4)) do |tag, css_class| %>
    <%= link_to tag.name, { :action => :tag, :id => tag.name }, :class
=> css_class %>
<% end %>

is in my view

the error is:
"ActionView::TemplateError (undefined method `empty?' for
nil:NilClass)"

and stack trace:
    app/views/blog/index.haml:21:in
`_run_haml_app47views47blog47index46haml'
    haml (3.0.23) rails/./lib/haml/helpers/action_view_mods.rb:13:in
`render'
    haml (3.0.23) rails/./lib/haml/helpers/action_view_mods.rb:13:in
`render'
    app/controllers/blog_controller.rb:10:in `index'
    haml (3.0.23) rails/./lib/sass/plugin/rack.rb:41:in `call'
    /dh/passenger/lib/phusion_passenger/rack/request_handler.rb:92:in
`process_request'
    /dh/passenger/lib/phusion_passenger/abstract_request_handler.rb:
207:in `main_loop'
    /dh/passenger/lib/phusion_passenger/railz/application_spawner.rb:
400:in `start_request_handler'
    /dh/passenger/lib/phusion_passenger/railz/application_spawner.rb:
351:in `handle_spawn_application'
    /dh/passenger/lib/phusion_passenger/utils.rb:184:in `safe_fork'
    /dh/passenger/lib/phusion_passenger/railz/application_spawner.rb:
349:in `handle_spawn_application'
    /dh/passenger/lib/phusion_passenger/abstract_server.rb:352:in
`__send__'
    /dh/passenger/lib/phusion_passenger/abstract_server.rb:352:in
`main_loop'
    /dh/passenger/lib/phusion_passenger/abstract_server.rb:196:in
`start_synchronously'
    /dh/passenger/lib/phusion_passenger/abstract_server.rb:163:in
`start'
    /dh/passenger/lib/phusion_passenger/railz/application_spawner.rb:
209:in `start'
    /dh/passenger/lib/phusion_passenger/spawn_manager.rb:262:in
`spawn_rails_application'
    /dh/passenger/lib/phusion_passenger/abstract_server_collection.rb:
126:in `lookup_or_add'
    /dh/passenger/lib/phusion_passenger/spawn_manager.rb:256:in
`spawn_rails_application'
    /dh/passenger/lib/phusion_passenger/abstract_server_collection.rb:
80:in `synchronize'
    /dh/passenger/lib/phusion_passenger/abstract_server_collection.rb:
79:in `synchronize'
    /dh/passenger/lib/phusion_passenger/spawn_manager.rb:255:in
`spawn_rails_application'
    /dh/passenger/lib/phusion_passenger/spawn_manager.rb:154:in
`spawn_application'
    /dh/passenger/lib/phusion_passenger/spawn_manager.rb:287:in
`handle_spawn_application'
    /dh/passenger/lib/phusion_passenger/abstract_server.rb:352:in
`__send__'
    /dh/passenger/lib/phusion_passenger/abstract_server.rb:352:in
`main_loop'
    /dh/passenger/lib/phusion_passenger/abstract_server.rb:196:in
`start_synchronously'

i am not sure what the problem is, any advice would be appreciated

aperture science wrote in post #961901:

I've tried out 'acts_as_taggable', 'acts_as_taggable_on' and
'acts_as_taggable_on_steroids' and all of them output "undefined
method 'empty?'" with the tag_cloud action.

Then the error is probably not in their code...

[...]

the error is:
"ActionView::TemplateError (undefined method `empty?' for
nil:NilClass)"

and stack trace:
    app/views/blog/index.haml:21:in

It pays to read stack traces: they tell you right where the error is.
What's there?

Best,

- tag_cloud @tags, %w(css_class1 css_class2 css_class3 css_class4)
do |tag, css_class|
    = link_to tag.name, { :action => :tag, :id => tag.name }, :class
=> css_class

Is the offending line(s)

That is taken directly from the github page (translated into HAML)

I assume it is something I am doing wrong, but I can't see what it is,
since I appear to be doing everything the instructions say to do.

the documentation is here: https://github.com/mbleigh/acts-as-taggable-on

and i do get the same error when I use html.erb syntax instead of haml

Please quote when replying.

aperture science wrote in post #961912:

- tag_cloud @tags, %w(css_class1 css_class2 css_class3 css_class4)
do |tag, css_class|
    = link_to tag.name, { :action => :tag, :id => tag.name }, :class
=> css_class

Is the offending line(s)

Which?

That is taken directly from the github page (translated into HAML)

I assume it is something I am doing wrong, but I can't see what it is,
since I appear to be doing everything the instructions say to do.

What is the value of @tags ?

Best,

Which?

The first line of the stack trace points to a line in my index which
contains the following:
- tag_cloud @tags, %w(css_class1 css_class2 css_class3 css_class4) do |
tag, css_class|

The above line of code is taken from the plugin's documentation and
translated into HAML

if I directly use the documented coding verbatim, e.g.
<% tag_cloud(@tags, %w(css1 css2 css3 css4)) do |tag, css_class| %>

I get the same error

The documentation is here:
https://github.com/mbleigh/acts-as-taggable-on

What is the value of @tags ?

As per the documentation, @tags is defined in my controller as:
@tags = Blog.tag_counts_on(:tags)

from the console, the above line outputs all my tags. Something is
going awry when I use the tag_cloud action to calculate the frequency
of individual tags. As per my introductory post, I have followed
exactly as to what the documentation explains on how to properly use
the plugin. There is seemingly something wrong, however, which is why
I am asking here.

Amendment:

- tag_cloud(@tags, %w(css1 css2 css3 css4)) do |tag, css_class|

is the exact line in my view that the stack trace is pointing at

aperture science wrote in post #961920:
[...]

What is the value of @tags ?

As per the documentation, @tags is defined in my controller as:
@tags = Blog.tag_counts_on(:tags)

I didn't ask that. I asked *what the actual value* was. It seems like
it's nil.

from the console, the above line outputs all my tags. Something is
going awry when I use the tag_cloud action to calculate the frequency
of individual tags.

Are you running in the same environment?

As per my introductory post, I have followed
exactly as to what the documentation explains on how to properly use
the plugin. There is seemingly something wrong, however, which is why
I am asking here.

Right.

Best,

I didn't ask that. I asked *what the actual value* was. It seems like
it's nil.

If I understand you correctly, the actual value of @tags as defined by
Blog.tag_counts_on(:tags) is:
[#<ActsAsTaggableOn::Tag id: 2, name: "post test">]

Are you running in the same environment?

Yes

I didn't ask that. I asked *what the actual value* was. It seems like
it's nil.

If I understand you correctly, the actual value of @tags as defined by
Blog.tag_counts_on(:tags) is:
[#<ActsAsTaggableOn::Tag id: 2, name: "post test">]

I think Marnen is asking what is the actual value when the problematic
line of code is executed, rather than what you think it aught to be.
If you do not know how to find this out have a look at the Rails
Guide on Debugging.

Colin

Colin Law wrote in post #961937:

I didn't ask that. I asked *what the actual value* was. It seems like
it's nil.

If I understand you correctly, the actual value of @tags as defined by
Blog.tag_counts_on(:tags) is:
[#<ActsAsTaggableOn::Tag id: 2, name: "post test">]

I think Marnen is asking what is the actual value when the problematic
line of code is executed, rather than what you think it aught to be.
If you do not know how to find this out have a look at the Rails
Guide on Debugging.

Exactly.

Colin

Best,

I see, thanks.

If I prepend "<%= debug @tags>" prior to the tag cloud block, I
receive this on my page:

aperture science wrote in post #961957:

I see, thanks.

If I prepend "<%= debug @tags>" prior to the tag cloud block, I
receive this on my page:

---
- !ruby/object:ActsAsTaggableOn::Tag
  attributes:
    name: post test
    id: "2"
    count: "1"
  attributes_cache: {}

  readonly: true

post test

That looks reasonable.

but it was necessary to move the @tags definition into my index action
in order for it to display

Where was it before (when it wasn't working)?

however, it only works on the index of the controller in which it is
defined, i suppose i can define it in all of my controller's indexes,
but I don't imagine it is supposed to behave this way

It probably is. If you need to set it in many places, you may need a
before_filter.

Best,

It had been defined within the tag_cloud definition, which I had
assumed would be processing the loop based on "tag_cloud(@tags,
%w(css1 css2 css3 css4))"

I have since found it necessary to define @tags =... in every action
for each controller in which the tag cloud appears

Furthermore, in order for the tags to actually function when clicked,
it was necessary to add:
  def tag
    @tags = Blog.tag_counts_on(:tags)
    @blogs = Blog.tagged_with(params[:id])
  end

within each controller, along with the associated tag.html.erb in the
corresponding views folder

It all seems to be working now, although I am pretty sure I am doing
it all wrong since nothing of this nature is mentioned in the
documentation anywhere. Plus I have no idea how I am going to get
tags to work with another model at the same time, now...

But thanks for the help

aperture science wrote in post #961979:

Where was it before (when it wasn't working)?

It had been defined within the tag_cloud definition, which I had
assumed would be processing the loop based on "tag_cloud(@tags,
%w(css1 css2 css3 css4))"

No, I mean where did you *set* @tags, not where did you read it?

I have since found it necessary to define @tags =... in every action
for each controller in which the tag cloud appears

That's pretty standard Rails. You can use a before_filter to remove the
duplication.

Furthermore, in order for the tags to actually function when clicked,
it was necessary to add:
  def tag
    @tags = Blog.tag_counts_on(:tags)
    @blogs = Blog.tagged_with(params[:id])
  end

within each controller, along with the associated tag.html.erb in the
corresponding views folder

It all seems to be working now, although I am pretty sure I am doing
it all wrong

Doesn't look wrong to me.

since nothing of this nature is mentioned in the
documentation anywhere.

Presumably the documentation expects that you know how variables are
passed around between controllers and views in Rails. There is nothing
out of the ordinary here.

Plus I have no idea how I am going to get
tags to work with another model at the same time, now...

Probably the same way you did with this one.

But thanks for the help

It looks to me like you need a review of basic Rails practices.

Best,

No, I mean where did you *set* @tags, not where did you read it?

within the tag_cloud action defined within the controller from which i
was calling tag_cloud

That's pretty standard Rails. You can use a before_filter to remove the
duplication.

will do, thanks

Doesn't look wrong to me.

i suppose i was expecting tag_cloud to output the tags wherever it was
called, and link back to the tag_cloud definition despite it being
inserted into the view of an index action
sort of like a 'create' action doesn't need a corresponding
create.html.erb file in order to work, but i am understanding why it
wasnt working that way now

Probably the same way you did with this one.

yes, i have it all more or less working correctly across models now

It looks to me like you need a review of basic Rails practices.

probably a wise idea...ive been teaching myself rails when i have the
time by diving in and trying to do things with it. but it would be
smart to study the API a bit further

thanks again