/ rails

Getting Pry into rails console

Getting Pry into rails console.

First you should have Pry installed.
Add gem 'pry' and gem 'pry-doc' to your Gemfile.
pry-doc allows you to do show-doc File for example to reference documentation right in your console.

Gemfile

group :development, :test do
  # Call 'byebug' anywhere in the code to stop execution and get a debugger console
  gem 'byebug', platform: :mri
  gem 'pry'
  gem 'pry-doc'
end

Then you need to add the following setup to development.rb.

config/environments/development.rb

  #https://coderwall.com/p/htjmwa/using-pry-with-rails-4-with-console-helpers
    console do
      require "pry"
      config.console = Pry
      unless defined? Pry::ExtendCommandBundle
        Pry::ExtendCommandBundle = Module.new
      end
      require "rails/console/app"
      require "rails/console/helpers"
      TOPLEVEL_BINDING.eval('self').extend ::Rails::ConsoleMethods

      #http://guides.rubyonrails.org/configuring.html
      config.action_controller.allow_forgery_protection = false
    end
    config.action_controller.permit_all_parameters = true

The allow_forgery_protection and permit_all_parameters allows you to sign in a Devise user like this:

app.post('/users/sign_in', user: {email: 'abc@example.com', password: '123456'})

You can then access current_user like this:

app.controller.current_user
Getting Pry into rails console
Share this