English 中文(简体)
使用包括班级方法中的助手的铁路
原标题:Rails using included helpers inside class methods

是否有任何人知道,所包括的方法为什么不以班级方法工作?

class MyClass
  include ActionView::Helpers::NumberHelper

  def test
    puts "Uploading #{number_to_human_size 123}"
  end

  def self.test
    puts "Uploading #{number_to_human_size 123}"
  end
end


ree-1.8.7-2011.03 :004 > MyClass.new.test
Uploading 123 Bytes
 => nil 
ree-1.8.7-2011.03 :005 > MyClass.test
NoMethodError: undefined method `number_to_human_size  for MyClass:Class
    from /path/to/my/code.rb:9:in `test 
    from (irb):5
ree-1.8.7-2011.03 :006 >
问题回答

对于那些想要使用某些习俗帮助的人来说,有时不值得包括所有帮助者。 而是直接称之为:

class MyClass
  def test
    ::ApplicationController.helpers.number_to_human_size(42)
  end
end

(从http://makandracards.com/makandra/1307-how-to-use-helper-methods-inside-a-model)

如果看不见你的助手代码,则很难说出,但include将把该模块中的所有方法插入instances。 <代码>extend用于将方法带入 >>>>>。 因此,如果你刚刚掌握了编号Helper的方法,这些方法就会被引入到MyClas的所有情况,而不是一类。

The way that lots of Rails extensions work is using techniques that have been consolidated into ActiveSupport::Concern. Here is a good overview.

基本上,扩大积极的支持:在你的模块中,你可以确定,在称为“级治疗和症状”的分模块中,你想要在包括你的模块的课堂和例中增加什么功能。 例如:

module Foo
    extend ActiveSupport::Concern
    module ClassMethods
        def bar
            puts "I m a Bar!"
        end
    end
    module InstanceMethods
        def baz
            puts "I m a Baz!"
        end
    end
end

class Quox
    include Foo
end

Quox.bar
=> "I m a Bar"
Quox.new.baz
=> "I m a Baz"

I ve used this before to do things like define the bar function in ClassMethods, then also make it available to instances by defining a bar method of the same name that just calls this.class.bar, making it callable from both. There are lots of other helpful things that ActiveSupport::Concern does, like allowing you to define blocks that are called back when the module is included.

Now, this is happening here specifically because you re includeing your helper, which might indicate that this functionality is more general-purpose than a helper - helpers are only automatically included in views, since they are only intended to help with views. If you want to use your helper in a view, you could use the helper_method macro in your class to make that method visible to your views, or, even better, make a module as above and not think about it as a helper, but use include to mix it in to the classes you want to use it in. I think I would go that route - there s nothing that says you can t make a HumanReadableNumber module and include it in NumberHelper to make it easily available across your views.

I faced the same issue. Here s how I solved it,

helper = Object.new.extend(ActionView::Helpers::NumberHelper)
helper.number_to_human_size(1000000)

http://archive.railsforum.com/viewtopic.php?id=25150"rel=“nofollow”

我也面临同样的问题。 在我的情况下,将<代码>include改为extend 使之发挥作用。

class MyClass
  extend ActionView::Helpers::NumberHelper
  ...
end




相关问题
rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

SSL slowness in EC2

We ve deployed our rails app to EC2. In our setup, we have two proxies on small instances behind round-robin DNS. These run nginx load balancers for a dynamically growing and shrinking farm of web ...

Auth-code with A-Za-z0-9 to use in an URL parameter

As part of a web application I need an auth-code to pass as a URL parameter. I am currently using (in Rails) : Digest::SHA1.hexdigest((object_id + rand(255)).to_s) Which provides long strings like : ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

activerecord has_many :through find with one sql call

I have a these 3 models: class User < ActiveRecord::Base has_many :permissions, :dependent => :destroy has_many :roles, :through => :permissions end class Permission < ActiveRecord::...

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...