Web Console

repository·main·Indexed 23 days ago

https://github.com/rails/web-console

A debugging tool for Ruby on Rails applications that provides an interactive Ruby session directly in the web browser. It allows developers to trigger a console from views or controllers using the `console` method, configure access permissions via IP networks, and customize the mount point and template paths. The tool includes a middleware system and browser extensions for Chrome.

Tokens
1.6K
Snippets
8
Records
15
Agent score
78%

What's inside web-console

  1. Quickstart for Web Console Browser Extensions development

    main

    To develop the Web Console browser extensions, clone the repository, install dependencies, and run the Chrome extension task using Rake.

    Note: This process requires having git, bundle, and rake installed on your system.

    $ git clone https://github.com/rails/web-console.git
    $ cd web-console
    $ bundle install
    $ bundle exec rake ext:chrome:run
  2. Inspect local and instance variables in the console

    main

    Since the interactive console executes arbitrary Ruby code, you can inspect the current scope by calling standard Ruby methods:

    • Use local_variables to see local variables.
    • Use instance_variables to see instance variables.
  3. Use the `console` method in views or controllers

    main

    The console method is defined in Kernel and can be invoked anywhere in your application code to launch an interactive Ruby session in your browser.

    • In a view: Calling <% console %> displays the console within the current page in the context of the view binding.
    • In a controller: Calling console inside an action launches the console in the context of that controller action.

    Note: Only one console invocation per request is allowed. Attempting multiple invocations will raise a WebConsole::DoubleRenderError.

    <% console %>
  4. Configure `config.web_console.permissions`

    main

    By default, only IPv4 and IPv6 localhosts are allowed access. Use config.web_console.permissions to grant access to specific IP addresses or entire networks.

    Note that IPv4 and IPv6 localhosts are always allowed and cannot be revoked via this setting.

    # Allow a single IP
    class Application < Rails::Application
      config.web_console.permissions = '192.168.0.100'
    end
    
    # Allow a whole private network
    Rails.application.configure do
      config.web_console.permissions = '192.168.0.0/16'
    end
  5. Configure Web Console access permissions

    main

    Web Console uses a permissions system to restrict access to the console interface to specific IP networks. By default, 127.0.0.0/8, ::1, and ::ffff:127.0.0.0/104 (localhost) are always permitted.

    You can customize the allowed networks by providing a list of network strings or IPAddr objects to the permissions configuration. The system automatically normalizes these inputs and ensures localhost remains accessible.

  6. Configure `config.web_console.whiny_requests`

    main

    When a console cannot be rendered (due to IP restrictions or content type), Web Console prints a message to the server logs. To suppress these messages, set config.web_console.whiny_requests to false.

    Rails.application.configure do
      config.web_console.whiny_requests = false
    end
  7. Configure `config.web_console.template_paths`

    main

    To customize the appearance of the console, you can override its templates. Set config.web_console.template_paths to a directory containing your custom style.css and other template files.

    Rails.application.configure do
      config.web_console.template_paths = 'app/views/web_console'
    end
  8. Configure `config.web_console.mount_point`

    main

    By default, the Web Console middleware is mounted at /__web_console. You can change this path using config.web_console.mount_point.

    Rails.application.configure do
      config.web_console.mount_point = '/path/to/web_console'
    end
  9. Troubleshoot console not appearing when called manually

    main

    If the console appears on error pages but not when you explicitly call console, it is likely due to Rack::Deflater. Ensure WebConsole::Middleware is used after Rack::Deflater.

    The easiest fix is to insert Rack::Deflater as early as possible in your middleware stack:

    Rails.application.configure do
      config.middleware.insert(0, Rack::Deflater)
    end
  10. Troubleshoot unavailable session errors

    main

    Web Console sessions are stored in memory. If you are using a multi-process server (like Unicorn), you may see 'unavailable session' errors because a subsequent request might hit a different worker process that does not have the session in its memory.

    Solution: Configure your server to serve requests from a single process during development. For example, if using Passenger, enable sticky sessions.