当前位置: 动力学知识库 > 问答 > 编程问答 >

ruby on rails 3 - acts-as-taggable-on tag cloud problem

问题描述:

i'm new to rails. There is one question about this plugin. I've read the official readme file, and few other topics, but I still can't make it work.

I have installed it, it works perfectly for creating tags, but I can't make tagcloud

I've done

 module SitesHelper

include ActsAsTaggableOn::TagsHelper

end

class SiteController < ApplicationController

def tag_cloud

@tags = Site.tag_counts_on(:tags)

end

end

And the view throws me exeption on

 <% 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 %>

wrong number of arguments (at least 1)

24: <b>Tags:</b>

25: <%= @site.type_list %>

26:

27: <% tag_cloud @site.type_list, %w(css1 css2 css3 css4) do |tag, css_class| %>

28: <%= link_to tag.name, { :action => :tag, :id => tag.name }, :class => css_class %>

29: <% end %>

30:

What am I doing wrong?

网友答案:

I think it adds methods to the classes, so you can directly access them.

You could try this:

        <h2>Tag Cloud</h2>
    <% if Site.tag_counts.length > 0 -%>
        <% tag_cloud Site.tag_counts, %w(css1 css2 css3 css4) do |tag, css_class| -%>
            <%= link_to tag.name, home_tag_path(tag), :class => css_class -%>
        <% end -%>
    <% else -%>
        <%= content_tag :p, 'No tags' -%>
    <% end -%>

That is what I have used and it works.

N.B. when you call tag_cloud from within the sites view, it is calling the corresponding definition in the controller. You want to call the tag_cloud in tags_helper (in the plugin), so you'll wantt to remove your tag_cloud definition in your controller as well.

Some good topics to cover that might help understand this are:

  • Method lookup (one step to the right, then up) and
  • Self (good to understand fully if starting on ruby/rails)
  • Metaprogramming (if you aren't familiar with the term)

All are good to get your head around if you are new to ruby/rails.

Hope that helps.

网友答案:

I ran into the same problem with this example myself. And a few other problems as well. I fixed what I ran into to work for my needs even though they may stray slightly from the original intention.

I fixed it by moving @tags = Site.tag_counts_on(:tags) into the index method. I then removed the tag_cloud method. (It wasn't restful anyway.)

I also changed the link_to to redirect to the controller for the items that were tagged. That involved:

  • setting the text to tag.name
  • changing the path
  • removing :action => :tag and :id => :tag.name
  • adding a key value identifier to be passed in through params

I also changed the tag classes to something less generic.

My finished controller method:

def index
  @list = List.new
  @tags = List.tag_counts_on(:tags)
end

My finished view snippet:

<% tag_cloud(@tags, %w(tag1 tag2 tag3 tag4)) do |tag, css_class| %>
  <%= link_to tag.name, lists_path(:id => tag.name), :class => css_class
分享给朋友:
您可能感兴趣的文章:
随机阅读: