Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 674
Javid Jamae Avatar asked Mar 02 '13 17:03

Javid Jamae


People also ask

How do I start rails console?

Go to your browser and open http://localhost:3000, you will see a basic Rails app running. You can also use the alias "s" to start the server: bin/rails s . The server can be run on a different port using the -p option. The default development environment can be changed using -e .

Can you console log in rails?

One of the best tools in a rails developers arsenal is the rails console, being extremely useful for brainstorming, debugging, and testing. Having it log active record queries directly to the console can improve readability and convenience over looking through the development logs to see what SQL queries have been run.

What is rails web console?

Web Console is a debugging tool for your Ruby on Rails applications.

What is debugger in rails?

Let's start with the most straightforward option: Rails' own debug method. It can be used for displaying data for any object in the front end. This is helpful when something isn't showing properly on the website and you want to find out if it's missing in the database or it's an issue with the view code.


1 Answers

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.

like image 114
Ryan Avatar answered Nov 07 '22 05:11

Ryan