Skip to content

Running on virtual machines

Robin Daugherty edited this page Oct 28, 2020 · 8 revisions

Editor links

If your Rails app is running in a virtual machine (e.g. Vagrant, Docker, or Windows Subsystem for Linux), the path to your source files from Rails' perspective will be different to the path seen by your editor.

You can adjust the path used to generate open-in-editor links using the vagrant-host-path gem.

You then add something like the following to your project's config/environments/development.rb, depending on your editor:

if defined?(BetterErrors)
  BetterErrors.editor = proc { |full_path, line|
    full_path = full_path.sub(Rails.root.to_s, ENV["VAGRANT_HOST_PATH"])
    "subl://open?url=file://#{full_path}&line=#{line}"
  }
end

For VS Code:

if defined?(BetterErrors)
  BetterErrors.editor = proc { |full_path, line|
    full_path = full_path.sub(Rails.root.to_s, ENV["VAGRANT_HOST_PATH"])
    "vscode://file#{full_path}:#{line}"
  }
end

Allowing access to your IP address

The following can be added to your config/environments/development.rb. When you connect to the virtual machine to run your project, it will allow the IP address that you are connecting from.

if defined?(BetterErrors) && ENV["SSH_CLIENT"]
  host = ENV["SSH_CLIENT"].match(/\A([^\s]*)/)[1]
  BetterErrors::Middleware.allow_ip! host if host
end
Clone this wiki locally