Ruby 在Rails 5控制台中运行帮助程序
我正在尝试在rails控制台中测试一些助手 我读过一篇描述一些技巧的文章 在我的第一次尝试中,我开始在Ruby 在Rails 5控制台中运行帮助程序,ruby,console,ruby-on-rails-5,Ruby,Console,Ruby On Rails 5,我正在尝试在rails控制台中测试一些助手 我读过一篇描述一些技巧的文章 在我的第一次尝试中,我开始在helpers对象上调用我的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".* FR
helpers
对象上调用我的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\u helper
对象。老实说,我不确定helper
对象与views\u 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)>
我希望能够将一些东西放在config/env文件中的某个地方,或者在控制台上运行一个“包含所有内容以便我的助手‘正常工作’”的oneliner。这可能吗?我不确定是否会自动将助手方法加载到控制台中,但是 您可以通过在控制台中调用ApplicationController.helpers来访问各个帮助程序方法。
ArgumentError: arguments passed to url_for can't be handled. Please require routes or provide your own implementation
当我尝试使用我的Rails 5.1应用程序中的帮助程序时出错。请链接到
将route.url\u helpers
包含到helper
对象中,这样可以解决我的问题:
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>"
main>helper.singleton\u class.include Rails.application.routes.url\u helpers
=>#简单的方法是使用模块名称空间:
UserHelper.some_method
但是,您应该使用self来定义方法:
module UserHelper
def self.some_method
puts 'hello world!'
end
end