Value whitelist using strong parameters in Rails 4

冷暖自知 提交于 2019-12-11 01:55:22

问题


Is it possible to use strong parameters to ensure that an attribute will be filtered with a whitelist of possible values?

For example, I have a parameter age that I want to ensure that can only have this values [10,20,30,40,50]. Is it possible to use the strong parameters logic to ensure that?

Thanks


回答1:


Quick answer

No! Strong parameters only let you to filter keys from a hash regardless of the value they have.

Long answer

No! But as it was pointed out in comments, your best solution is to use validations:

class MyModel < ActiveRecord::Base
  validates :value, inclusion: { in: [1,2,3] }
end


来源:https://stackoverflow.com/questions/28541432/value-whitelist-using-strong-parameters-in-rails-4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!