次のように、入力プロセス中に検証が行われます。
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
ただし、これによりページの読み込み時にエラーが返されました。
未定義のメソッド`key? ' nilの場合:NilClass
これは、モデルの検証がページの読み込み時に読み込まれるという事実と関係があり、その時点でIDをチェックするifステートメントが機能しないことを理解しました。このエラーは私にはまったく意味がありませんが。
それで、選択したドロップダウンボックスアイテムのIDに基づいて特定の検証が選択されるように設定する方法を考えています(IDを正しく取得していますか?)
ボーナス:誰かがこの特定のエラーとの関係を説明できますか?
編集-スタックトレース:
visible_action?'
vendor/bundle/ruby/1.9.1/gems/actionpack-3.2.1/lib/action_controller/metal/hide_actions.rb:18:in
ベンダー/バンドル/ruby/1.9.1/gems/actionpack-3.2.1/lib/action_controller/metal/hide_actions.rb:36:inmethod_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:indispatch'vendor/bundle/ruby/1.9.1/gems/actionpack-3.2.1/lib/action_controller/ dispatch'
vendor/bundle/ruby/1.9.1/gems/actionpack-3.2.1/lib/action_controller/metal/rack_delegation.rb:14:in
_ metal.rb:246:in call'vendor block in action'
vendor/bundle/ruby/1.9.1/gems/actionpack-3.2.1/lib/action_dispatch/routing/route_set.rb:66:in
/ bundle / ruby /1.9.1/gems/actionpack-3.2.1/lib/action_dispatch/routing/route_set.rb:66:in call'vendor / dispatch'
vendor/bundle/ruby/1.9.1/gems/actionpack-3.2.1/lib/action_dispatch/routing/route_set.rb:30:in
bundle / ruby/1.9。 1 / gems / journal-1.0.4 / lib / journal / 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 / journal-1.0.4 / lib / journal / 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 catch'vendor block in call'
vendor/bundle/ruby/1.9.1/gems/warden-1.2.1/lib/warden/manager.rb:34:in
/ bundle / ruby /1.9.1/gems/warden-1.2.1/lib/warden/manager.rb:34:in call'vendor call'
vendor/bundle/ruby/1.9.1/gems/actionpack-3.2.1/lib/action_dispatch/middleware/best_standards_support.rb:17:in
/ bundle / ruby /1.9.1/gems/rack-1.4.1/lib/rack/etag.rb:23:in call'vendor call'
vendor/bundle/ruby/1.9.1/gems/rack-1.4.1/lib/rack/conditionalget.rb:25:in
/ bundle / ruby /1.9.1/gems/actionpack-3.2.1/lib/action_dispatch/middleware/ head.rb:14:in call'vendor call'
vendor/bundle/ruby/1.9.1/gems/actionpack-3.2.1/lib/action_dispatch/middleware/params_parser.rb:21:in
/ 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 call'
vendor/bundle/ruby/1.9.1/gems/actionpack-3.2.1/lib/action_dispatch/middleware/cookies.rb:338:in
/ 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'ベンダーcall'
vendor/bundle/ruby/1.9.1/gems/actionpack-3.2.1/lib/action_dispatch/middleware/remote_ip.rb:31:in
/バンドル/ruby/1.9.1/gems/actionpack-3.2.1/lib/action_dispatch/middleware/reloader.rb:65:incall'vendor/bundle/ruby/1.9.1/gems/actionpack-3.2.1 /lib/action_dispatch/middleware/debug_exceptions.rb:16:in call'vendor call'
vendor/bundle/ruby/1.9.1/gems/actionpack-3.2.1/lib/action_dispatch/middleware/show_exceptions.rb:56:in
/ 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: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:incall call'
vendor/bundle/ruby/1.9.1/gems/rack-1.4.1/lib/rack/runtime.rb:17:in
」vendor/ bundle / ruby / 1.9.1 / gems / activesupport-3.2.1 /lib/active_support/cache/strategy/local_cache.rb:72:in call'vendor call'
vendor/bundle/ruby/1.9.1/gems/rack-1.4.1/lib/rack/lock.rb:15:in
/ 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 '