Javid Jamae
Javid Jamae

Reputation: 8999

Embedding a live Rails console in a view

Lately, I've been using the better_errors gem and I find the live shell capabilities (basically a Rails console embedded in your view) to be extraordinarily useful. I'd love to be able to have access a live shell of this sort which I could use for debugging / diagnostic purposes, even when I don't have an error. This would be something great to embed in my application controller and restrict to admin access so that I could use it in a staging or prod server environment.

Is there any other similar tool, or perhaps a way to strip out the live shell from better_errors and embed it in my views?

Upvotes: 10

Views: 1681

Answers (1)

Ryan
Ryan

Reputation: 9430

Have you taken a look at pry? It's not technically like better_errors where it opens up a console in the browser, but it functions the same way. Basically you just add binding.pry anywhere in your code, even in your views, to create a break point in the code letting you run whatever you want at that point in time.

There's also a railscast on how to use it http://railscasts.com/episodes/280-pry-with-rails

As an alternative, you should just be able to throw an error anywhere in your code which will bring up the better_errors page at that point in the code.

I'm not sure of any way to do this on Staging/Production, apart from just opening up the console. You would probably never want to give that level of access to your code on Production anyways though. At that point anyone who can access that page has complete access to the database. Even if it is limited to developers it seems like a potential security risk.

Upvotes: 2

Related Questions