【问题标题】:Call controller action from application layout从应用程序布局调用控制器操作
【发布时间】:2011-07-22 01:55:37
【问题描述】:

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

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

这是我的控制器:

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

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

另外,我怀疑这个 MVC 安全吗?请有任何建议。

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

【问题讨论】:

    标签: ruby-on-rails model-view-controller ruby-on-rails-3 tags tag-cloud


    【解决方案1】:

    移动tag_cloude的代码

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

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

    【讨论】:

      猜你喜欢
      • 2015-01-28
      • 1970-01-01
      • 1970-01-01
      • 2012-12-10
      • 1970-01-01
      • 2014-08-14
      • 1970-01-01
      • 2014-11-10
      • 1970-01-01
      相关资源
      最近更新 更多