开发者

Call controller action from application layout

开发者 https://www.devze.com 2023-02-19 16:38 出处:网络
I have this code in my posts/index view: 开发者_运维知识库 -tag_cloud(@tags, %w(css1 css2 css3 css4)) do |tag, css_class|

I have this code in my posts/index view:

开发者_运维知识库
 -tag_cloud(@tags, %w(css1 css2 css3 css4)) do |tag, css_class|
    = link_to tag.name, { :action => :tag, :id => tag.name }, :class => css_class

This is my controller:

def index
    @posts = Post.page(params[:page]).per(5)
    tag_cloud
    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @posts }
    end
end

def tag
  @posts = Post.tagged_with(params[:id]).page(params[:page]).per(5)
  @tags = Post.tag_counts_on(:tags)
  render :template => 'posts/index'
end

def tag_cloud
  @tags ||= Post.tag_counts_on(:tags)
end

I want to move tag cloud from index view to the application layout, but I don't know how to call controller action method from there.

Also, I'm in doubt, is this MVC safe? Any advices please.

I'm using gem 'acts-as-taggable-on'


Move the code of tag_cloude

 def tag_cloud
  @tags ||= Post.tag_counts_on(:tags)
 end

to the ApplicationHelper then you can use it <%= tag_cloud %> in your layout of the application.

0

精彩评论

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