gpt4 book ai didi

ruby-on-rails - Rails 控制台 : where are helper and app defined?

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

rails console 中,有两个对象可用:apphelperSeveral blog posts涵盖这些有什么用。不幸的是,它们在普通 Rails 应用程序的源代码中不可用。

这些方法在哪里定义,或者我如何在 Rails 应用程序中使用它们?

最佳答案

长篇小说

.../gems/railties-3.2.8/lib/rails/console/app.rb
.../gems/railties-3.2.8/lib/rails/console/helpers.rb

长版

我将介绍我是如何发现这些信息的,以及声明是什么样子的。

方法:

# in rails console
1.9.3p194 :019 > a = self
=> main
1.9.3p194 :020 > a.method :helper
=> #<Method: Object(Rails::ConsoleMethods)#helper>
1.9.3p194 :021 > _.source_location
=> ["/Users/erichu/.rvm/gems/ruby-1.9.3-p194@global/gems/railties-3.2.8/lib/rails/console/helpers.rb", 3]

该文件的相关来源:

def helper
@helper ||= ApplicationController.helpers
end

所以 helpers 只是 ApplicationController.helpers 的别名>

寻找app 源的过程是类似的。这是相关的来源:

def app(create=false)
@app_integration_instance = nil if create
@app_integration_instance ||= new_session do |sess|
sess.host! "www.example.com"
end
end

所以控制台中的 app@app_integration_instance 的别名,我可以验证它在 Controller 中是否可用

在应用程序中使用这些是好的做法吗?可能不是。

这是一种快速访问 Rails View 助手和渲染器的方法吗?我想是的。

关于ruby-on-rails - Rails 控制台 : where are helper and app defined?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13061264/

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