Write to Rails Console

Write to rails console

As other have said, you want to use either puts or p. Why? Is that magic?

Actually not. A rails console is, under the hood, an IRB, so all you can do in IRB you will be able to do in a rails console. Since for printing in an IRB we use puts, we use the same command for printing in a rails console.

You can actually take a look at the console code in the rails source code. See the require of irb? :)

How do I write a function in rails console?

IRB, the ruby console in which rails console relies supports this out of the box.

Just type your function declaration, press the enter key, then input the body line by line, and finally type end.

You'll see text like this:

2.4.1 :001 > def say_hi(person)
2.4.1 :002?> puts "Hi #{person}"
2.4.1 :003?> end
=> :say_hi
2.4.1 :004 > say_hi("Nina")
Hi Nina
=> nil
2.4.1 :005 >

Notice how the ? indicates that IRB is waiting for more input before evaluating the expression.

Can I print debug messages to the browser console in Ruby on Rails?

Simple. Just call puts 'your debug message' and it will be printed to where the server is logging. For instance, if you are running the rails server only by running rails s on a terminal, the output of puts will be on this same terminal. If you need more 'power' to debug, you should consider using IDE's like RubyMine to debug your code. Thus, you can place breakpoints and see all the application state.

How do I send my output to the Rails console?

You want to use Rails.logger:

rescue => e
Rails.logger.error "#{e.message}\n"
e.backtrace.each { |line| Rails.logger.error "#{msg}\n#{line}" }
raise e
end

How to print a controller as text in the rails console?

Pry which is a replacement console for IRB has built in source browsing.

Install the pry-rails gem and start the console with rails c and it will start up Pry instead of IRB.

You can then use show-source to view the source code of the gem right from the console:

max@pop-os ~/p/sandbox> rails c
Running via Spring preloader in process 29286
Loading development environment (Rails 6.0.2.1)
[1] pry(main)> show-source Devise::RegistrationsController

From: /home/linuxbrew/.linuxbrew/lib/ruby/gems/2.7.0/gems/devise-4.7.2/app/controllers/devise/registrations_controller.rb @ line 3:
Class name: Devise::RegistrationsController
Number of monkeypatches: 4. Use the `-a` option to display all available monkeypatches
Number of lines: 166

class Devise::RegistrationsController < DeviseController
prepend_before_action :require_no_authentication, only: [:new, :create, :cancel]
prepend_before_action :authenticate_scope!, only: [:edit, :update, :destroy]
prepend_before_action :set_minimum_password_length, only: [:new, :edit]

# GET /resource/sign_up
def new
build_resource
yield resource if block_given?
respond_with resource
end

# POST /resource
def create
build_resource(sign_up_params)

resource.save
yield resource if block_given?
if resource.persisted?
if resource.active_for_authentication?
set_flash_message! :notice, :signed_up
sign_up(resource_name, resource)
respond_with resource, location: after_sign_up_path_for(resource)
else
set_flash_message! :notice, :"signed_up_but_#{resource.inactive_message}"
expire_data_after_sign_in!
respond_with resource, location: after_inactive_sign_up_path_for(resource)
end
else
clean_up_passwords resource
set_minimum_password_length
respond_with resource
end
end
:

If you don't want to depend on Pry and stick with IRB you can cobble something together with Method#source_location and IO.readlines. But IMHO it seems like a waste of time

rails - Redirecting console output to a file

You can use override $stdout to redirect the console output:

$stdout = File.new('console.out', 'w')

You may also need to call this once:

$stdout.sync = true

To restore:

$stdout = STDOUT

ROR console output to text file

Use the private key in accessing the ROR server and insert it into filezilla.
By then login thru filezilla, hostname should be ROR host and user type should be interactive.

How do I give a Ruby script executed via Rails console write permissions on Digital Ocean?

You need to give the necessary folder permissions to create/write a file. You can do that by:

chmod -R g+w app_path/config

This gives write permission to app config folder.

If you want to give permissions to a certain user then:

=> w || who; #list all of the currently logged in users
=> chgrp -R user_name app_path/config
=> chmod -R g+w app_path/config

Edited by OP

What also works is: sudo chmod 777 oxr.json

How to get nice formatting in the Rails console

The y method is a handy way to get some pretty YAML output.

y ProductColor.all

Assuming you are in script/console

As jordanpg commented, this answer is outdated. For Rails 3.2+ you need to execute the following code before you can get the y method to work:

YAML::ENGINE.yamler = 'syck'

From ruby-docs

In older Ruby versions, ie. <= 1.9, Syck is still provided, however it
was completely removed with the release of Ruby 2.0.0.

For rails 4/ruby 2 you could use just

puts object.to_yaml


Related Topics



Leave a reply



Submit