跳到内容 跳到搜索
方法
A
C
H
N
R

实例公共方法

app(create = false)

引用全局“app”实例,按需创建。要重新创建实例,请将非 false 值作为参数传递。

# File railties/lib/rails/console/app.rb, line 10
def app(create = false)
  @app_integration_instance = nil if create
  @app_integration_instance ||= new_session
end

controller()

获取控制器对象的新实例。

此方法假定存在一个 ApplicationController,并且它扩展了 ActionController::Base

# File railties/lib/rails/console/helpers.rb, line 15
def controller
  @controller ||= ApplicationController.new
end

helper()

获取控制器可用的帮助器方法。

此方法假定存在一个 ApplicationController,并且它扩展了 ActionController::Base

# File railties/lib/rails/console/helpers.rb, line 8
def helper
  ApplicationController.helpers
end

new_session()

创建一个新会话。如果给出了一个块,则会在返回新会话之前将其让渡给该块。

# File railties/lib/rails/console/app.rb, line 17
def new_session
  app = Rails.application
  session = ActionDispatch::Integration::Session.new(app)

  # This makes app.url_for and app.foo_path available in the console
  session.extend(app.routes.url_helpers)
  session.extend(app.routes.mounted_helpers)

  session
end

reload!(print = true)

重新加载环境

# File railties/lib/rails/console/app.rb, line 29
def reload!(print = true)
  puts "Reloading..." if print
  Rails.application.reloader.reload!
  true
end