How do I call controller / view methods from the console in Ruby on Rails?

Posted by asuamol on Thu, 30 Jan 2020 11:59:15 +0100

When I load script/console, sometimes I want to play with the controller's output or view assistant methods.

There are ways to:

  • Simulation request?
  • Call the method from the controller instance on the request?
  • Test assistant method by controller instance or other means?

#1 building

To call the helper, use the helper object:

$ ./script/console
>> helper.number_to_currency('123.45')
=> "R$ 123,45"

If you want to use a helper that is not included by default (for example, because you removed helper: all from ApplicationController), just include the helper.

>> include BogusHelper
>> helper.bogus
=> "bogus output"

As for the processing controller, I quote Nick Answer:

> app.get '/posts/1' > response = app.response # you now have a rails response object much like the integration tests > response.body # get you the HTML > response.cookies # hash of the cookies # etc, etc

#2 building

The previous answer was to call the helper, but the following will help you call the controller method. I used it on Ruby on Rails 2.3.2.

First add the following code to the. irbrc file (available in your home directory)

class Object
   def request(options = {})
     url=app.url_for(options)
     app.get(url)
     puts app.html_document.root.to_s
  end
end

Then on the Ruby on Rails console, you can type something like

request(:controller => :show, :action => :show_frontpage)

... and HTML will be dumped to the console.

#3 building

A simple way to call a controller action from a script / console and view / manipulate the response object is to:

> app.get '/posts/1'
> response = app.response
# You now have a Ruby on Rails response object much like the integration tests

> response.body            # Get you the HTML
> response.cookies         # Hash of the cookies

# etc., etc.

The app object is Of actioncontroller:: Integration:: session An example

This applies to my use of Ruby on Rails 2.1 and 2.3, which I didn't try earlier.

#4 building

This is a way to do this through the console:

>> foo = ActionView::Base.new
=> #<ActionView::Base:0x2aaab0ac2af8 @assigns_added=nil, @assigns={}, @helpers=#<ActionView::Base::ProxyModule:0x2aaab0ac2a58>, @controller=nil, @view_paths=[]>

>> foo.extend YourHelperModule
=> #<ActionView::Base:0x2aaab0ac2af8 @assigns_added=nil, @assigns={}, @helpers=#<ActionView::Base::ProxyModule:0x2aaab0ac2a58>, @controller=nil, @view_paths=[]>

>> foo.your_helper_method(args)
=> "<html>created by your helper</html>"

By creating a new instance of ActionView::Base, you can access the normal view methods that the assistant might use. Then extend your helpermodule to mix its methods into your objects so you can see their return values.

#5 building

In Ruby on Rails 3, try this:

session = ActionDispatch::Integration::Session.new(Rails.application)
session.get(url)
body = session.response.body

The body will contain the HTML for the URL.

How to route and render (schedule) from the model in Ruby on Rails 3

Topics: Ruby Session