gpt4 book ai didi

ruby-on-rails - 应用程序 Controller 中的全局数据? -- rails 3.1

转载 作者:行者123 更新时间:2023-12-03 15:35:36 25 4
gpt4 key购买 nike

我在每个 View 中都需要几个元素,并尝试在应用程序 Controller 中设置它们(这不起作用)...

class ApplicationController < ActionController::Base
protect_from_forgery

@top_categories = Category.top.limit(10)

end

我怎样才能在所有 View 中提供全局数据?

最佳答案

您可以将其包装在一个函数中并指定为帮助器,例如:

class ApplicationController < ActionController::Base    
helper_method :top_categories

def top_categories
Category.top.limit(10)
end
end

这将在所有 View 和 Controller 中可用

关于ruby-on-rails - 应用程序 Controller 中的全局数据? -- rails 3.1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8234730/

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