3 helpful methods in Rails console

3 helpful methods in Rails console
Photo by Nick Morrison / Unsplash

Recently, I start reading Practicing Rails. I love the main point in chapter 1: Rails console is a good place to experiment with our ideas.

There are 3 tips to explore ideas through the Rails console I learned from Practicing Rails.

1. Use app object.

Who is app?

$ app.class
#=> ActionDispatch::Integration::Session

# That means
app = ActionDispatch::Integration::Session.new(Rails.application)

According to the source code,

An instance of this class represents a set of requests and responses performed sequentially by a test process.

And ActionDispatch::Integration::Session class includes the ActionDispatch::Integration::RequestHelpers. (Read more about this module)

So, we can use app to perform HTTP requests:

$ app.get('http://localhost:3000/articles/1')
# Started GET "/articles/1" for at 2021-07-07 15:14:08 +0800
# Processing by ArticlesController#show as HTML
#  Parameters: {"id"=>"1"}
#  Article Load (0.6ms)  SELECT "articles".* FROM "articles" WHERE "articles"."id" = $1 LIMIT $2  [["id", 1], ["LIMIT", 1]]
#  Rendering articles/show.html.erb within layouts/application
#  Rendered articles/show.html.erb within layouts/application (Duration: 2.7ms | Allocations: 344)
#  Rendered layouts/_header.html.erb (Duration: 0.4ms | Allocations: 145)
#  Rendered layouts/_footer.html.erb (Duration: 0.5ms | Allocations: 91)
# Completed 200 OK in 17ms (Views: 13.3ms | ActiveRecord: 0.6ms | Allocations: 4199)

#=> 200

$ puts app.response.body.first(200)
# <!DOCTYPE html>
# <html>
#   <head>
#    <title>Lynn's Blog</title>
#    <meta name="csrf-param" content="
# => nil

What's more, we can use app to get routes.

$ article = Article.create(title: 'beautiful_article')
$ app.article_path(article)
#=> '/articles/1'

2. Use helper object to preview HTML tags or other return values.

For example,

$ helper.link_to('Hello world!', 'https://www.example.com')
#=> "<a href=\"https://www.example.com\">Hello world!</a>"

If you find out that you get the undefined method error message when trying to call a custom  helper. It might mean the module hasn't been included.

$ helper.your_custom_helper_method
#=> NoMethodError: undefined method `your_custom_helper_method' for #<ActionView::Base:0x00007fe53d1cf140>

It's good timing to check config/application.rb, if the setting below is false.

config.action_controller.include_all_helpers = false

If you just want to check the return value of this custom helper method and don't want to change the setting above, we can include this helper module first:

$ include Admin::ProjectsHelper
#=> Object < BasicObject

$ helper.your_custom_helper_method
#=> 'hi, how are you?'

3. Use a special variable _.

The result of the last line we ran in the console is automatically saved in a variable named _.

For example,

$ Article.new
#=> #<Article id: nil, title: "", content: "", cover: nil, status: "draft", created_at: nil, updated_at: nil>

$ _
#=> #<Article id: nil, title: "", content: "", cover: nil, status: "draft", created_at: nil, updated_at: nil>

# Assume `title` attribute cannot be blank
$ _.valid?
#=> false

$ _
#=> false

$ _.errors.messages
#=> Traceback (most recent call last):
#       2: from (irb):46
#       1: from (irb):47:in `rescue in irb_binding'
# NoMethodError (undefined method `errors' for #<NoMethodError: undefined method `error' for false:FalseClass>)

Next time, if we forget to assign a variable, we can just pick it up to use. :P

Reference Info: