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
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?
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:
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.
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.
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 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.
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.
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.
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...
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.
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