cbrulak
cbrulak

Reputation: 15629

Rails: Helper function not found in view

I'm following the instructions at: http://agilewebdevelopment.com/plugins/acts_as_taggable_on_steroids to add the tag cloud to my view:

in the controller:

class PostController < ApplicationController
   def tag_cloud
      @tags = Post.tag_counts
   end
end

I also added the tag_cloud method as a helper method in the controller

and in the view:

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

However:

1) if I don't add the helper_method :tag_cloud in the controller I get a undefined method error for tag_cloud

2) if I do add the helper method I get: wrong number of arguments (2 for 0) on the same line 1 of my sample code above.

Suggestions?

SOLUTION I ended up not doing what I had as example code in the view.

Instead I did this:

<% @post.tags.each do |tag| %>
    <%= link_to( tag.name,tag,:class => "tag_cloud_item_link") %>
<% end %>

Upvotes: 4

Views: 3897

Answers (3)

stevec
stevec

Reputation: 52218

I arrived here because I wanted to define a method to use in the view. The simple answer was that it should have been defined in the model (e.g. conversation.rb file), and not in the helper.

Upvotes: 1

thoughtpunch
thoughtpunch

Reputation: 1937

I'm having the same issue. Just like the OP, Moving the "tag_cloud" helper method to the TagHelper seemed to get rid of some issue, but creates the "wrong number of arguments" error in the process.

cbrulak said he found a work arround. Can you update us and possibly send a PM to the "Acts-as-taggable-on" authors at https://github.com/mbleigh/acts-as-taggable-on

Upvotes: 1

theIV
theIV

Reputation: 25774

1.

Methods defined in the controller are not accessible to views unless you add (as you mention) the helper_method call.

2.

Your method tag_cloud that you've defined as a helper in your controller doesn't take any parameters, but you are trying to call tag_cloud with @tags, %w(css1...), & a block.

Your tag_cloud method will return an @tags instance variable and that's it.

From the post you've provided that you are working off of, did you include TagsHelper in your ApplicationHelper? I'm guessing that this defines a tag_cloud helper method that will accept the params that you are trying to pass in.

Upvotes: 2

Related Questions