开发者

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| %>                   (l开发者_如何学运维ine 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 %>


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.


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜