我在输入过程中进行了验证,如下所示:
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:inblock in call'
vendor/bundle/ruby/1.9.1/gems/journey-1.0.4/lib/journey/router.rb:56:in
每个' 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:incall'
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
run _803795940_ call _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' 配置/initializers/quiet_assets.rb:6:incall_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 `block in start_thread'