Introduction

As many of you know, Ruby on Rails has long included the ability to load up an interactive console environment for your application. Simply running rails c would start this console, which allows you to do things like ActiveRecord queries, troubleshooting, and much more. Since the full Rails stack gets loaded you have access to pretty much anything your application has access to. This is useful to troubleshoot things on the spot.

Starting with Ruby on Rails 4.2, users will have access to a web based version of this console. The good news is that you don't have to wait for Ruby on Rails 4.2 to hit in order to take advantage of this awesome capability. You can quickly and easily use the web console in your current Rails application by performing the steps below.

Installation and Setup

To use the Rails web console, we need to include the web-console gem. Simple add the line below to your Gemfile, then run a bundle install to install it.

Gemfile:

gem 'web-console', '~> 2.0.0.beta4'
Terminal Commands:

bundle install

Next, we need to turn on the web console in our application configuration. To do this, simple add the following line to your development.rb environment file:

config/environments/development.rb:

config.web_console.automount = true

Now, let's create a controller to play with the console. Run the command below to create a controller that we can use to play with the console. For this example, we will create a controller called Homes with a method called show.

Terminal Commands:

rails g controller Homes show

Now, open up your routes file and modify it so that it looks like the code listed below.

config/routes.rb:

Rails.application.routes.draw do
  root to: "homes#show"
end

Now open up the show view for the Homes controller and add in the code listed below.

app/views/homes/show.html.erb:

<%= console %>

If you start a rails server and navigate to your development environment, you'll notice that there is a console at the bottom of the screen. You can execute Ruby and Rails code here. The console is running within your Rails application so pretty much anything goes.

By default, console application is mounted to http://yoursite/console. However, what if we wish to change the path? We can easily do it by adding the line listed below to our development.rb file:

config/environments/development.rb:

config.web_console.default_mount_path = '/your/favorite/path'

Whitelisting IPs

The console by default only works with if you are accessing it locally via 127.0.0.1. If you are on a network or using a VM, you may need to add your ip to a whitelist. You can easily do that by adding the line listed below to your development.rb file. Don't forget to restart your Rails server for changes to take effect.

config/environments/development.rb:

config.web_console.whitelisted_ips =  %w( 127.0.0.1 192.168.0.100 )

You can also add an entire subnet. Remember to make sure you add 127.0.0.1 or the local machine won't have access.

config/environments/development.rb:

config.web_console.whitelisted_ips =  %w( 127.0.0.1 192.168.0.0/16 )

Running Other Commands

By default the console runs the rails console. However, you can have it run another command instead. For instance, we can turn the web console into an terminal running bash by adding the following line to our development.rb file:

config/environments/development.rb:

config.web_console.command = 'sudo /bin/login'

Be aware that passwords sent using this method are sent in plaintext. if that concerns you, you should consider using SSL.

Security Considerations

At this point, it's not advisable to run this on production. Exposing the console endpoint in a production environment means anyone can get access to the Rails console and therefore access to the system itself. Stick to the development/test environments. Hopefully in the future the Rails team will provide additional features that will allow console use in a production environment.

As mentioned earlier, console information is sent plain text. If this concerns you, consider using SSL or not using the web console.

That's it! Thanks for reading!

Resources