所以我只是弄清楚如何验证我的选择的包含。但是我有一个问题已经上升。如果其中一个值包含两个或更多单词,我无法弄清楚如何验证它。
以例如:
validates_inclusion_of :possibility, :in => %w(yes maybe no)
它会完美运作。然而,如果我有两个,三个字说:
validates_inclusion_of :job_possibility, :in => %w(yes please maybe not no thank you)
它不会起作用。我已经尝试将它们添加到括号等中。但是能够弄明白。有谁知道如何使这项工作?
答案 0 :(得分:4)
您可以使用%W
代替%w
来实现所需的结果。由于%W
支持字符串插值,因此您可以轻松实现两个单词,三个单词等。
使用%W
:
validates_inclusion_of :job_possibility, :in => %W(yes please #{"maybe not"} no #{"thank you"})
-Or -
你可以直接指定数组:
validates_inclusion_of :job_possibility, :in => ["yes", "please", "maybe not", "no", "thank you"]
例如:
在IRB,
> job_possibility = %W(yes please #{"maybe not"} no #{"thank you"})
## => ["yes", "please", "maybe not", "no", "thank you"]
> job_possibility = ["yes", "please", "maybe not", "no", "thank you"]
## => ["yes", "please", "maybe not", "no", "thank you"]