fly*_*123 10 ruby-on-rails ruby-on-rails-4
我对这里读到的rails文档感到困惑.特别是这句话:
默认情况下,每个控制器都包含所有帮助程序.这些助手只能通过控制器访问
.helpers
这是什么.helpers,它是指什么?我有一个帮助定义在app/helpers/areas_helper.rb:
module AreasHelper
def my_helper
puts "Test from helper"
end
end
Run Code Online (Sandbox Code Playgroud)
我想在这个帮手中使用app/controllers/locations_controller.rb:
class LocationsController < ApplicationController
def show
helpers.my_helper
end
end
Run Code Online (Sandbox Code Playgroud)
但是,我得到一个方法未定义错误.这.helpers应该如何使用?
我知道还有其他方法可以访问控制器中的帮助程序,但我特别询问这篇文档以及它想说的内容.
您打算在控制器中包含帮助程序类:
#app/controllers/locations_controller.rb
class LocationsController < ApplicationController
include AreasHelper
def show
my_helper
end
end
Run Code Online (Sandbox Code Playgroud)
Rails 5在以下PR中引入了此功能 https://github.com/rails/rails/pull/24866
因此,我们可以在Rails 5及更高版本中使用此功能,而不能在Rails 4.x中使用。
| 归档时间: |
|
| 查看次数: |
7240 次 |
| 最近记录: |