2016-12-03 41 views
1

我試圖在rails控制檯中測試一些幫助程序。在Rails 5控制檯中運行幫助程序

我讀another answer描述了一些技術。

在我第一次嘗試,我開始調用helpers對象上我的助手:

[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)> 

然後我嘗試了不同的技術,我在創建views_helper對象的答案看到的。老實說,我不知道helper對象與views_helper對象有什麼不同,但是無論如何我仍然無法運行我的命令。

[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)> 

我希望能夠把一些在配置/ env的文件的某處,或運行oneliner在那個「包括一切,讓我的助手‘只是工作’」控制檯。這可能嗎?

回答

0

我不確定自動加載助手方法到控制檯中,但您可以通過在控制檯中調用ApplicationController.helpers.your_method來訪問各個助手方法。

0

我遇到同樣的

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

錯誤,當我試圖在我的Rails應用程序5.1使用helper.link_to

包括route.url_helpershelper對象這樣固定的問題對我來說:

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

main > helper.link_to 'user', User.find(1) 
=> "<a href=\"https://stackoverflow.com/users/1\">user</a>" 
0

簡單的方法就是使用模塊命名空間:

UserHelper.some_method 

,但是,你應該使用自己的定義方法:

module UserHelper 
    def self.some_method 
    puts 'hello world!' 
    end 
end