ruby-on-rails - 从应用程序布局调用 Controller 操作

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

我的帖子/索引 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

这是我的 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

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

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

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

最佳答案

移动tag_cloude的代码

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

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

关于ruby-on-rails - 从应用程序布局调用 Controller 操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5418692/

相关文章:

javascript - Mapbox.js 在 Ruby on Rails 中呈现空白

ruby-on-rails - rails 4 回形针 : undefined method `brand_logo_image_content_type' for #<Batch:0x431a408>

html - 枚举和 View 集成的 Rails 替代品

ruby-on-rails - 带有默认值和新对象的 Rails 序列化

ruby-on-rails - 如何重构这段 Rails 代码?

ruby-on-rails - 如何使用两个搜索文本参数进行 postgres 搜索,Search FILter text 1 && search filter text 2

java - MVC-听起来是 View 还是模型?

model-view-controller - EmberJS : Good separation of concerns for Models, 在相当复杂的应用程序中存储、 Controller 、 View ?

java - Spring 3 <mvc :interceptors> flawed?

ruby-on-rails - 如何在一些数据之后进行迭代?