首页 > 解决方案 > 在 Rails 控制台中使用辅助方法时确定 Rails 中标签方法的源位置

问题描述

我知道 Rails 中有两种label方法 (ActionView::Helpers::FormHelper#labelActionView::Helpers::FormBuilder#label) 可用。在以下内容中键入以下内容时将调用哪个#label方法rails console

$ rails c
Loading development environment (Rails 5.0.7.2)
2.6.0 :001 > helper.label(:post, :title, "A short title", class: "title_label")
 => "<label class=\"title_label\" for=\"post_title\">A short title</label>"

我已经进入gems/actionview-5.0.7.2/lib/action_view/helpers/form_helper.rb并注释掉了这两种label方法:

#      def label(object_name, method, content_or_options = nil, options = nil, &block)
#        Tags::Label.new(object_name, method, self, content_or_options, options).render(&block)
#      end
.
.
.
#      def label(method, text = nil, options = {}, &block)
#        @template.label(@object_name, method, text, objectify_options(options), &block)
#      end

但它仍在labelRails 控制台中执行该方法,我如何确定该方法调用的源位置?

标签: ruby-on-railserbrails-console

解决方案


这是第一个。第二个需要一个FormBuilder对象(f.部分)才能工作。

注释掉这些方法后,您是否重新加载了控制台?

我您正在使用Pry,您可以使用show-method

pry(main)> show-method helper.label

From: /home/user/.rvm/gems/ruby-2.5.3@gemset/gems/actionview-5.2.3/lib/action_view/helpers/form_helper.rb @ line 1114:
Owner: ActionView::Helpers::FormHelper
Visibility: public
Number of lines: 3

def label(object_name, method, content_or_options = nil, options = nil, &block)
  Tags::Label.new(object_name, method, self, content_or_options, options).render(&block)
end

推荐阅读