【问题标题】:Accessing Rails Helper files from models从模型访问 Rails Helper 文件
【发布时间】:2012-11-14 14:40:58
【问题描述】:

我正在尝试使用以下方法验证状态字段的输入:

include ActionView::Helpers
class Credentials < ActiveRecord::Base
    attr_accessible :license_number, ...:address_zip_code,

    ...

    validates :license_number,  presence: true, length: { minimum: 4 }
    ...
    validates_inclusion_of :current_practice_address_state, :in => state_list

end

变量 state_list 是 helpers/credentials_helper.rb 中描述的数组

测试我遇到未定义局部变量错误的模型

$ bundle exec rspec spec/models/credentials_spec.rb
in `method_missing': undefined local variable or method `state_list' for #<Class:0x007f86a1844250> (NameError)

帮助类看起来像:

 module CredentialsHelper
        state_list = %w(AL AK...WY)
 end

【问题讨论】:

    标签: ruby-on-rails-3 models helpers


    【解决方案1】:

    您对include 的调用必须在课堂内:

    class Credentials < ActiveRecord::Base
      include ActionView::Helpers
      ...
    end
    

    【讨论】:

    • 好的,谢谢(doh!),但错误仍然存​​在。 method_missing': undefined local variable or method state_list' for #<0x007fb71d960448>
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多