从应用程序布局调用控制器操作

Pet*_*rov 3 tags model-view-controller ruby-on-rails tag-cloud ruby-on-rails-3

我的帖子/索引视图中有这个代码:

 -tag_cloud(@tags, %w(css1 css2 css3 css4)) do |tag, css_class|
    = link_to tag.name, { :action => :tag, :id => tag.name }, :class => css_class
Run Code Online (Sandbox Code Playgroud)

这是我的控制器:

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
Run Code Online (Sandbox Code Playgroud)

我想将标签云从索引视图移动到应用程序布局,但我不知道如何从那里调用控制器操作方法.

另外,我有疑问,这个MVC安全吗?请给我任何建议.

我正在使用 gem 'acts-as-taggable-on'

Naz*_*ain 7

移动tag_cloude的代码

 def tag_cloud
  @tags ||= Post.tag_counts_on(:tags)
 end
Run Code Online (Sandbox Code Playgroud)

ApplicationHelper那时你可以<%= tag_cloud %>在你的应用程序布局中使用它.