gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 18:25:39 25 4
gpt4 key购买 nike

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com