在 Rails 5 控制台中运行助手

mbi*_*ras 2 ruby console ruby-on-rails-5

我正在尝试测试 rails 控制台中的一些帮助程序。

我读了另一个描述一些技术的答案

在我的第一次尝试中,我开始在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)>
Run Code Online (Sandbox Code Playgroud)

然后我尝试了一种在创建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)>
Run Code Online (Sandbox Code Playgroud)

我希望能够在某个地方的 config/env 文件中放置一些东西,或者在控制台上运行一个 oneliner,“包括所有东西,所以我的助手'只是工作'”。这可能吗?

小智 6

我不确定是否将辅助方法自动加载到控制台中,但您可以通过ApplicationController.helpers.your_method在控制台中调用来访问各个辅助方法。