Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/62.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 如果文本在rails模型中具有特定的单词或字符串,则显示错误_Ruby On Rails_Ruby - Fatal编程技术网

Ruby on rails 如果文本在rails模型中具有特定的单词或字符串,则显示错误

Ruby on rails 如果文本在rails模型中具有特定的单词或字符串,则显示错误,ruby-on-rails,ruby,Ruby On Rails,Ruby,我有一个模型问题,它有以下字段: 问题:文本 答案1:文本 答案2:文本 答案3:文本 答案4:文本 我不允许提出以下问题: 在问题中,除了“”之外,还有“not”或“code>” 在答案字段中输入单词“以上所有内容均不”或“以上所有内容” 在回答字段中以“真”、“假”、“是”或“否”开头,或 Have“答案在答案本身中是” 如果不适合您,您可以尝试使用regex: 在控制器中,类似于: @question = Question.new(question_params) if params

我有一个模型
问题
,它有以下字段:

  • 问题:文本
  • 答案1:文本
  • 答案2:文本
  • 答案3:文本
  • 答案4:文本
我不允许提出以下问题:

  • 在问题中,除了“”之外,还有“not”或“code>”
  • 在答案字段中输入单词
    “以上所有内容均不”
    “以上所有内容”
  • 在回答字段中以“真”、“假”、“是”或“否”开头,或
  • Have
    “答案在答案本身中是”
    • 如果不适合您,您可以尝试使用regex:

      在控制器中,类似于:

      @question = Question.new(question_params)
      if params[:question] =~  /not/ || params[:answer] =~ /all of the above/ || what-so-ever-conditions
         raise "not accepted"
      else
         @question.save
      end
      

      但是,这不是一个好的解决方案,相反,您应该尝试@pavan在其评论中提到的方法。

      您可以在模型中创建自定义验证器:

      validate :check_words, :on => :create
      NOT_ALLOWED_WORDS = ["not", "except"]
      
       def check_words 
        errors.add :question, "not allowed" unless (NOT_ALLOWED_WORDS - only_words_from_question) == NOT_ALLOWED_WORDS
      end
      
      
      def only_words_from_question
        self.question.split(/\W+/)
      end
      
      如果创建后有任何“不允许”字,则obj.errors.full_消息中的错误应为“不允许”


      我想这就是你想要的。

      以下是我解决问题的方法

      validate :check_question, :on => :create
      DISALLOW_QUESTION = ["not", "except"]
      
      def check_question 
        DISALLOW_QUESTION.each do |w|
            errors.add :question, "shouldn't have '#{w}'", if question.include? w
        end
      end
      
      validate :check_answer, :on => :create
      DISALLOW_ANSWER = ["none of the above", "all of the above", "answer is"]
      START_WITH = ['true', 'false', 'yes', 'no']
      
      def check_answer 
        DISALLOW_ANSWER.each do |w|
          errors.add :answer_1, "shouldn't have '#{w}'" if answer_1.include? w
          errors.add :answer_2, "shouldn't have '#{w}'" if answer_2.include? w
          errors.add :answer_3, "shouldn't have '#{w}'" if answer_3.include? w
          errors.add :answer_4, "shouldn't have '#{w}'" if answer_4.include? w
        end
        START_WITH.each do |w|
          errors.add :answer_1, "can't start with '#{w}'" if answer_1.start_with? w
          errors.add :answer_2, "can't start with '#{w}'" if answer_2.start_with? w
          errors.add :answer_3, "can't start with '#{w}'" if answer_3.start_with? w
          errors.add :answer_4, "can't start with '#{w}'" if answer_4.start_with? w
        end
      end
      

      您最好查看
      验证:问题,exlusion:{in:[“not”,“except”],消息:“not allowed”}
      如果字段中有空格,则没有帮助。我投票支持这个答案,所以有人能告诉我这里出了什么问题吗?任何人都可以。