3
在rails console
中,有兩個對象可用:app
和helper
。 Severalblogposts涵蓋了這些有用的東西。不幸的是,它們在普通的Rails應用程序的源代碼中不可用。Rails控制檯:幫助程序和應用程序定義在哪裏?
這些方法在哪裏定義,或者我如何在Rails應用程序中使用它們?
在rails console
中,有兩個對象可用:app
和helper
。 Severalblogposts涵蓋了這些有用的東西。不幸的是,它們在普通的Rails應用程序的源代碼中不可用。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/[email protected]/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
的別名,我可以確認在控制器可用
是否使用這些應用程序中好的做法呢?可能不會。
這是一種快速又髒的訪問Rails視圖助手和渲染器的方法嗎?我想是這樣。