2

I'm trying to test out some helpers in the rails console.

I read another answer that describes some techniques.

In my first attempt I start calling my helpers on the helpers object:

[10] pry(main)> helper.class
=> ActionView::Base
[1] pry(main)> helper.link_to gravatar_for(User.first, size: 50), User.first
  User Load (0.2ms)  SELECT  "users".* FROM "users" ORDER BY "users"."id" ASC LIMIT ?  [["LIMIT", 1]]
NoMethodError: undefined method `gravatar_for' for main:Object
from (pry):1:in `__pry__'
[2] pry(main)> include UsersHelper
=> Object
[3] pry(main)> helper.link_to gravatar_for(User.first, size: 50), User.first
  User Load (0.2ms)  SELECT  "users".* FROM "users" ORDER BY "users"."id" ASC LIMIT ?  [["LIMIT", 1]]
NoMethodError: undefined method `image_tag' for main:Object
from /Users/max/Dropbox/work/src/github.com/mbigras/micro-twitter/app/helpers/users_helper.rb:7:in `gravatar_for'
[4] pry(main)> include ActionView::Helpers::AssetTagHelper
=> Object
[5] pry(main)> helper.link_to gravatar_for(User.first, size: 50), User.first
  User Load (0.1ms)  SELECT  "users".* FROM "users" ORDER BY "users"."id" ASC LIMIT ?  [["LIMIT", 1]]
  User Load (0.1ms)  SELECT  "users".* FROM "users" ORDER BY "users"."id" ASC LIMIT ?  [["LIMIT", 1]]
ArgumentError: arguments passed to url_for can't be handled. Please require routes or provide your own implementation
from /Users/max/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/actionview-5.0.0.1/lib/action_view/helpers/url_helper.rb:38:in `url_for'
[6] pry(main)> include ActionView::Helpers::UrlHelper
=> Object
[7] pry(main)> helper.link_to gravatar_for(User.first, size: 50), User.first
  User Load (0.3ms)  SELECT  "users".* FROM "users" ORDER BY "users"."id" ASC LIMIT ?  [["LIMIT", 1]]
  User Load (0.2ms)  SELECT  "users".* FROM "users" ORDER BY "users"."id" ASC LIMIT ?  [["LIMIT", 1]]
ArgumentError: arguments passed to url_for can't be handled. Please require routes or provide your own implementation
from /Users/max/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/actionview-5.0.0.1/lib/action_view/helpers/url_helper.rb:38:in `url_for'
[8] pry(main)> include ActionController::Metal
TypeError: wrong argument type Class (expected Module)
from (pry):8:in `include'
[9] pry(main)>

Then I tried a different technique that I saw in the answer that creates a views_helper object. Honestly I'm not sure how the helper object is different from the views_helper object, but either way I still wasn't able to run my command.

[15] pry(main)> views_helper.class
=> ActionView::Base
[11] pry(main)> views = Rails::Application::Configuration.new(Rails.root).paths["app/views"]; nil
=> nil
[12] pry(main)> views_helper = ActionView::Base.new views; nil
=> nil
[13] pry(main)> views_helper.link_to gravatar_for(User.first, size: 50), User.first
  User Load (0.1ms)  SELECT  "users".* FROM "users" ORDER BY "users"."id" ASC LIMIT ?  [["LIMIT", 1]]
  User Load (0.1ms)  SELECT  "users".* FROM "users" ORDER BY "users"."id" ASC LIMIT ?  [["LIMIT", 1]]
ArgumentError: arguments passed to url_for can't be handled. Please require routes or provide your own implementation
from /Users/max/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/actionview-5.0.0.1/lib/action_view/helpers/url_helper.rb:38:in `url_for'
[14] pry(main)>

I'd like to be able to put something in a config/env file somewhere, or run a oneliner at the console that "includes everything so my helpers 'just work'". Is this possible?

Community
  • 1
  • 1
mbigras
  • 7,664
  • 11
  • 50
  • 111

3 Answers3

8

I'm not sure about auto loading helper methods into the console but you can access individual helper methods by calling ApplicationController.helpers.your_method in the console.

petecss
  • 389
  • 2
  • 5
4

I ran into the same

ArgumentError: arguments passed to url_for can't be handled. Please require routes or provide your own implementation

error when I tried to use helper.link_to in my Rails 5.1 app.

Including the route.url_helpers into the helper object like this fixed the problem for me:

main > helper.singleton_class.include Rails.application.routes.url_helpers
=> #<Class:#<ActionView::Base:0x00007fb02c2f7478>>

main > helper.link_to 'user', User.find(1)
=> "<a href=\"/users/1\">user</a>"
Tyler Rick
  • 9,191
  • 6
  • 60
  • 60
0

simple way is use module namespace:

UserHelper.some_method

but, you should use self for define methods:

module UserHelper
  def self.some_method
    puts 'hello world!'
  end 
end
Sergio Belevskij
  • 2,478
  • 25
  • 24