简体   繁体   English

使用Rails验证,如何将一个短语列入白名单,例如“ Learn Ruby”

[英]Using Rails validations, how do I whitelist a phrase, e.g., “Learn Ruby”

I have validates_presence_of :subject, inclusion: { in: %w( 'Learn Ruby' 'Learn Ruby on Rails'), message: "Please select a valid Subject" } Learn Ruby and Learn Ruby on Rails are examples of what I'm trying to do. 我有validates_presence_of :subject, inclusion: { in: %w( 'Learn Ruby' 'Learn Ruby on Rails'), message: "Please select a valid Subject" }学习Ruby和Learn Ruby on Rails就是我的例子试图做。

I've tried using single and double quotes without success. 我尝试使用单引号和双引号没有成功。

I've also tried an array: validates_presence_of :subject, inclusion: { in: ['Learn Ruby', 'Learn Ruby on Rails'], message: "Please select a valid Subject" } without success. 我还尝试了一个数组: validates_presence_of :subject, inclusion: { in: ['Learn Ruby', 'Learn Ruby on Rails'], message: "Please select a valid Subject" }但没有成功。

I'm familiar with the Phrasing gem ; 我熟悉短语宝石 ; however, installing it seems overkill. 但是,安装它似乎过于矫kill过正。 (And, not having tried it, I'm not sure it will help here.) (而且,如果没有尝试过,我不确定这里是否会有所帮助。)

It looks like you're mixing the old validates_x_y type methods with the Rails 3.x validates which is much more general purpose. 看起来您正在将旧的validates_x_y类型的方法与Rails 3.x validates混合使用,这更加通用。

validates_presence_of is from the Rails 1.x era and can only deal with the presence of something. validates_presence_of来自Rails 1.x时代, 只能处理某些事物。 There's validates_inclusion_of as a counterpart, but the real answer is to use the validates call which can be configured to test for any number of things at once. validates_inclusion_of作为对应对象,但真正的答案是使用validates调用,该调用可以配置为一次测试任意数量的事物。

The notation for that is: 表示为:

validates :subject,
  inclusion: {
    in: [
      'Learn Ruby',
      'Learn Ruby on Rails'
    ],
    message: "Please select a valid Subject"
  }

I've added some formatting as well to make what's going on more clear. 我还添加了一些格式,以使发生的事情更加清晰。 Those one-liners can get really tangled. 那些单调的人可能真的很纠结。

If your introduction is using those old-style validates_presence_of methods it's out of date and you should look for a more recent version, or a better reference. 如果您的介绍使用的是那些老式的validates_presence_of方法,那么它已经过时了,您应该寻找更新的版本或更好的参考。 As a note the official Rails documentation is usually a great place to start. 注意,正式的Rails文档通常是一个很好的起点。

ActiveModel validates_inclusion_of怎么样?

validates_inclusion_of :subject, in: ['Learn Ruby', 'Learn Ruby on Rails'], message: 'Please select a valid Subject'

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果用户未使用我的Web(导轨)应用程序(例如使用API​​),我该如何对他们进行身份验证 - How do I authenticate a user if they are not using my web (rails) application, e.g. using an API 如何使用Rails中的minitest测试我的控制器中的(例如)@project.save的失败? - How do I test for failure of (e.g.) @project.save in my controller using minitest in Rails? Ruby on Rails问题-如何从请求首次命中应用程序时开始衡量响应时间? (例如,使用“基准”) - Ruby on Rails question - how can I measure response time from when request first hits app?? (e.g. using 'benchmark') 如何获取我的Rails应用程序的基本URL(例如http:// localhost:3000)? - How do I get the base URL (e.g. http://localhost:3000) of my Rails app? 在Ruby on Rails应用程序中使用gem(例如json)的必要步骤? - Necessary steps for using a gem (e.g. json) in Ruby on Rails app? 如何手动创建嵌套的POST参数? (例如,我在.Net中创建请求以联系Rails后端) - how do I manually create POST parameters that are nested? (e.g. I'm creating the request in .Net to contact a Rails backend) 如何为Rails 3应用程序中的用户创建匿名电子邮件地址(例如abcd11245@craigslist.org)? - How do I create anonymous email addresses (e.g. abcd11245@craigslist.org) for users in a Rails 3 app? 在 heroku cli 中,如何在管道应用程序上运行命令(例如 rails c)? - In the heroku cli how to I run commands (e.g. rails c) on pipeline apps? 如果我想做Javascript,AJAX之类的东西,是否需要服务器端知识(例如Django,Rails)? - Do I need server-end knowledge (e.g. Django, Rails), if I want to do Javascript, AJAX stuff? 如何实现同一模型的嵌套实例? 例如一个类别中的类别? - How do I implement nested instances of the same model? E.g. a category within a category?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM