验证基于rails中下拉框的id

时间:2013-01-28 18:02:42

标签: ruby-on-rails ruby rails-activerecord

我在输入过程中发生了验证,如下所示:

validates :solids, :numericality => { :greater_than_or_equal_to => 0 }, :if => lambda { 
  if self.lab_method_id
    lab_method = LabMethod.find(self.lab_method_id)
    lab_method.solids?
  end
}

我希望根据下拉框中选定的ID更改此条件。

我在我的模型中试过这个:

if self.lab_method_id == 1
validates :solids, :numericality => { :greater_than_or_equal_to => 0 }, :if => lambda { 
  if self.lab_method_id
    lab_method = LabMethod.find(self.lab_method_id)
    lab_method.solids?
  end
}
elsif
validates :solids, :numericality => { :greater_than_or_equal_to => 5 }, :if => lambda { 
  if self.lab_method_id
    lab_method = LabMethod.find(self.lab_method_id)
    lab_method.solids?
  end
}
end

但是这会在页面加载时返回错误:

未定义的方法`键?'为零:NilClass

我认为这与模型中的验证在页面加载时加载这一事实有关,而此时检查id的if语句将不起作用。虽然这个错误对我来说没有意义。

所以我想知道如何根据所选择的下拉框项目的ID选择特定的验证方式进行设置(我是否正确地获得了ID)?

额外奖励:有人可以解释与此特定错误的关系吗?

编辑 - 堆栈跟踪:

vendor / bundle / ruby​​ / 1.9.1 / gems / actionpack-3.2.1 / lib / action_controller / metal / hide_actions.rb:36:in visible_action?' vendor/bundle/ruby/1.9.1/gems/actionpack-3.2.1/lib/action_controller/metal/hide_actions.rb:18:in method_for_action' vendor / bundle / ruby​​ / 1.9.1 / gems / actionpack-3.2.1 / lib / action_controller / metal / implicit_render.rb:14:in method_for_action' vendor/bundle/ruby/1.9.1/gems/actionpack-3.2.1/lib/action_controller/metal/compatibility.rb:61:in method_for_action' vendor / bundle / ruby​​ / 1.9.1 / gems / actionpack-3.2.1 / lib / abstract_controller / base.rb:115:in process' vendor/bundle/ruby/1.9.1/gems/actionpack-3.2.1/lib/abstract_controller/rendering.rb:45:in process' vendor / bundle / ruby​​ / 1.9.1 / gems / actionpack-3.2.1 / lib / action_controller / metal.rb:203:in dispatch' vendor/bundle/ruby/1.9.1/gems/actionpack-3.2.1/lib/action_controller/metal/rack_delegation.rb:14:in dispatch' vendor / bundle / ruby​​ / 1.9.1 / gems / actionpack-3.2.1 / lib / action_controller / metal.rb:246:in block in action' vendor/bundle/ruby/1.9.1/gems/actionpack-3.2.1/lib/action_dispatch/routing/route_set.rb:66:in call' vendor / bundle / ruby​​ / 1.9.1 / gems / actionpack-3.2.1 / lib / action_dispatch / routing / route_set.rb:66:in dispatch' vendor/bundle/ruby/1.9.1/gems/actionpack-3.2.1/lib/action_dispatch/routing/route_set.rb:30:in call' vendor / bundle / ruby​​ / 1.9.1 / gems / journey-1.0.4 / lib / journey / router.rb:68:in block in call' vendor/bundle/ruby/1.9.1/gems/journey-1.0.4/lib/journey/router.rb:56:in each' vendor / bundle / ruby​​ / 1.9.1 / gems / journey-1.0.4 / lib / journey / router.rb:56:in call' vendor/bundle/ruby/1.9.1/gems/actionpack-3.2.1/lib/action_dispatch/routing/route_set.rb:589:in call' vendor / bundle / ruby​​ / 1.9.1 / gems / warden-1.2.1 / lib / warden / manager.rb:35:in block in call' vendor/bundle/ruby/1.9.1/gems/warden-1.2.1/lib/warden/manager.rb:34:in catch' vendor / bundle / ruby​​ / 1.9.1 / gems / warden-1.2.1 / lib / warden / manager.rb:34:in call' vendor/bundle/ruby/1.9.1/gems/actionpack-3.2.1/lib/action_dispatch/middleware/best_standards_support.rb:17:in call' vendor / bundle / ruby​​ / 1.9.1 / gems / rack-1.4.1 / lib / rack / etag.rb:23:in call' vendor/bundle/ruby/1.9.1/gems/rack-1.4.1/lib/rack/conditionalget.rb:25:in call' vendor / bundle / ruby​​ / 1.9.1 / gems / actionpack-3.2.1 / lib / action_dispatch / middleware / head.rb:14:in call' vendor/bundle/ruby/1.9.1/gems/actionpack-3.2.1/lib/action_dispatch/middleware/params_parser.rb:21:in call' vendor / bundle / ruby​​ / 1.9.1 / gems / actionpack-3.2.1 / lib / action_dispatch / middleware / flash.rb:242:in call' vendor/bundle/ruby/1.9.1/gems/rack-1.4.1/lib/rack/session/abstract/id.rb:205:in context' vendor / bundle / ruby​​ / 1.9.1 / gems / rack-1.4.1 / lib / rack / session / abstract / id.rb:200:in call' vendor/bundle/ruby/1.9.1/gems/actionpack-3.2.1/lib/action_dispatch/middleware/cookies.rb:338:in call' vendor / bundle / ruby​​ / 1.9.1 / gems / activerecord-3.2.1 / lib / active_record / query_cache.rb:64:in call' vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:443:in call' vendor / bundle / ruby​​ / 1.9.1 / gems / actionpack-3.2.1 / lib / action_dispatch / middleware / callbacks.rb:28:in block in call' vendor/bundle/ruby/1.9.1/gems/activesupport-3.2.1/lib/active_support/callbacks.rb:405:in 运行 _803795940_ call < / EM> _68852270__callbacks' vendor / bundle / ruby​​ / 1.9.1 / gems / activesupport-3.2.1 / lib / active_support / callbacks.rb:405:in __run_callback' vendor/bundle/ruby/1.9.1/gems/activesupport-3.2.1/lib/active_support/callbacks.rb:385:in _ run_call_callbacks' vendor / bundle / ruby​​ / 1.9.1 / gems / activesupport-3.2.1 / lib / active_support / callbacks.rb:81:in run_callbacks' vendor/bundle/ruby/1.9.1/gems/actionpack-3.2.1/lib/action_dispatch/middleware/callbacks.rb:27:in call' vendor / bundle / ruby​​ / 1.9.1 / gems / actionpack-3.2.1 / lib / action_dispatch / middleware / reloader.rb:65:in call' vendor/bundle/ruby/1.9.1/gems/actionpack-3.2.1/lib/action_dispatch/middleware/remote_ip.rb:31:in call' vendor / bundle / ruby​​ / 1.9.1 / gems / actionpack-3.2.1 / lib / action_dispatch / middleware / debug_exceptions.rb:16:in call' vendor/bundle/ruby/1.9.1/gems/actionpack-3.2.1/lib/action_dispatch/middleware/show_exceptions.rb:56:in call' vendor / bundle / ruby​​ / 1.9.1 / gems / railties-3.2.1 / lib / rails / rack / logger.rb:26:in call_app' vendor/bundle/ruby/1.9.1/gems/railties-3.2.1/lib/rails/rack/logger.rb:16:in call' config / initializers / quiet_assets.rb:6:call_with_quiet_assets' vendor/bundle/ruby/1.9.1/gems/actionpack-3.2.1/lib/action_dispatch/middleware/request_id.rb:22:in调用' vendor / bundle / ruby​​ / 1.9.1 / gems / rack-1.4.1 / lib / rack / methodoverride.rb:21:in call' vendor/bundle/ruby/1.9.1/gems/rack-1.4.1/lib/rack/runtime.rb:17:in call' vendor / bundle / ruby​​ / 1.9.1 / gems / activesupport-3.2.1 / lib / active_support / cache / strategy / local_cache.rb:72:in call' vendor/bundle/ruby/1.9.1/gems/rack-1.4.1/lib/rack/lock.rb:15:in call' vendor / bundle / ruby​​ / 1.9.1 / gems / actionpack-3.2.1 / lib / action_dispatch / middleware / static.rb:53:in call' vendor/bundle/ruby/1.9.1/gems/railties-3.2.1/lib/rails/engine.rb:479:in call' vendor / bundle / ruby​​ / 1.9.1 / gems / railties-3.2.1 / lib / rails / application.rb:220:in call' vendor/bundle/ruby/1.9.1/gems/rack-1.4.1/lib/rack/content_length.rb:14:in call' vendor / bundle / ruby​​ / 1.9.1 / gems / railties-3.2.1 / lib / rails / rack / log_tailer.rb:14:in call' vendor/bundle/ruby/1.9.1/gems/rack-1.4.1/lib/rack/handler/webrick.rb:59:in service' /usr/lib/ruby/1.9.1/webrick/httpserver.rb:138:in service' /usr/lib/ruby/1.9.1/webrick/httpserver.rb:94:in run' /usr/lib/ruby/1.9.1/webrick/server.rb:191:in在start_thread中的块'

1 个答案:

答案 0 :(得分:1)

NumericalityValidator(此帮助程序的基础验证程序)不适用于您想要的动态条件。使用自定义验证器:

validate :solids_validation

def solids_validation
  if self.lab_method_id
    lab_method = LabMethod.find(self.lab_method_id)
    if lab_method.solids?
      if (val = Kernel.Float(solids) rescue nil)
        min = (self.lab_method_id == 1 ? 0 : 5)
        errors.add(:solids, "must be greater than or equal to #{min}") if val < min
      else
        errors.add(:solids, :not_a_number)
      end
    end
  end
end