【问题标题】:Determining source location of label method in Rails when using helper method inside Rails console在 Rails 控制台中使用辅助方法时确定 Rails 中标签方法的源位置
【发布时间】:2019-05-21 08:30:40
【问题描述】:

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

$ 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

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

【问题讨论】:

    标签: ruby-on-rails erb rails-console


    【解决方案1】:

    这是第一个。第二个需要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
    

    【讨论】:

    • 是的,我推荐了这些方法并重新加载了控制台。仍然返回"&lt;label class=\"title_label\" for=\"post_title\"&gt;A short title&lt;/label&gt;",我认为这很奇怪。
    • 我认为它会返回类似未定义方法的东西,因为它已被注释掉?
    • 确实应该:NoMethodError: undefined method 'label' for #&lt;ActionView::Base:0x0000000008c0d688&gt;。您的系统上有不同的 gemsets 吗?您使用的是您认为自己正在使用的 Ruby 版本吗?
    • 顺便说一句,在我的系统上,我不得不退出控制台。 reload! 还不够。
    • 我确实尝试退出,但仍然无法在我的系统上运行。感谢您的帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-09-22
    • 2011-01-24
    • 2012-11-16
    • 1970-01-01
    • 2012-02-13
    • 2010-09-14
    相关资源
    最近更新 更多