从模型中访问Rails Helper文件

时间:2012-11-14 14:40:59

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

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

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

1 个答案:

答案 0 :(得分:0)

您对include的来电必须在课堂内:

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