gpt4 book ai didi

ruby-on-rails - 在Rails 4.2.4中的 Controller 中使用助手

转载 作者:行者123 更新时间:2023-12-04 03:26:31 24 4
gpt4 key购买 nike

我对正在阅读here的Rails文档感到困惑。特别是这句话:

By default, each controller will include all helpers. These helpers are only accessible on the controller through .helpers



这是什么 .helpers指的是什么?我有一个在 app/helpers/areas_helper.rb中定义的助手:
module AreasHelper
def my_helper
puts "Test from helper"
end
end

我想在 app/controllers/locations_controller.rb中使用此帮助程序:
class LocationsController < ApplicationController
def show
helpers.my_helper
end
end

但是,我得到一个方法未定义的错误。应该如何使用此 .helpers

我知道还有其他方法可以访问 Controller 中的助手,但是我特别想问的是有关此文档及其试图表达的内容。

最佳答案

您应该在 Controller 中包括helper类:

#app/controllers/locations_controller.rb
class LocationsController < ApplicationController
include AreasHelper

def show
my_helper
end
end

关于ruby-on-rails - 在Rails 4.2.4中的 Controller 中使用助手,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32947093/

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