Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/65.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_Methods_Textfield - Fatal编程技术网

Ruby on rails Rails-将方法添加到文本字段

Ruby on rails Rails-将方法添加到文本字段,ruby-on-rails,ruby,methods,textfield,Ruby On Rails,Ruby,Methods,Textfield,我正在尝试让CheckSware方法在每个文本字段上运行,然后再提交 我基本上是这样的:(脱光衣服) 路线: match '/check' => 'profiles#checkSwear' 非常感谢任何帮助 (CheckSwarm是一个单独的gem;也就是说,一个单独的问题!这里的what Do表示从表单接收到什么类型的变量,要通过CheckSwarm gem进行输入) 更新: 很抱歉,我是一名研究Rails等的Java开发人员,旧习惯很难改掉。这是一个项目。我应该写一个小的gem来

我正在尝试让CheckSware方法在每个文本字段上运行,然后再提交

我基本上是这样的:(脱光衣服)

路线:

  match '/check' => 'profiles#checkSwear'
非常感谢任何帮助

(CheckSwarm是一个单独的gem;也就是说,一个单独的问题!这里的what Do表示从表单接收到什么类型的变量,要通过CheckSwarm gem进行输入)

更新:

很抱歉,我是一名研究Rails等的Java开发人员,旧习惯很难改掉。这是一个项目。我应该写一个小的gem来做一些ruby逻辑并将其应用到一些事情上。创业板的内容包括:

module antiSwear

  @swearwords = ["f**k", "f***ing", "shit", "shitting", "lecturer"]
  @replacements = ["fornicate", "copulating", "poop", "pooping", "Jonathan"]

  def self.checkText(text)

    @swearwords.each do |swearword|
      if text.include?(swearword)
        index = @swearwords.index(swearword)
        replacement = @replacements[index]
        text.gsub(swearword, replacement)
      end
    end
    return text   
  end
end 

:/

这确实应该在模型验证中完成

class Profile < ActiveRecord::Base
  validate :deny_swearing

  private
  def deny_swearing
    if AntiSwear.check_swear(love_to) || AntiSwear.check_swear(hate_to)
      errors.add_to_base('Swearing is not allowed.')
    end
  end
end
类配置文件
也就是说,如果您坚持要在控制器中执行此操作,您可以检查
params[:profile][:love_to]
params[:profile][:hate_to]
以查看提交的内容


另外,在这个例子中,我使用了正确的ruby命名约定,因为我们不使用“camelCasing”。

您这样做是作为验证的一部分吗?你可以用几种方法中的一种。您可以通过自定义验证方法在保存前运行检查,或直接重写setter。我在这里向您展示了自定义验证方法:

class Profile < ActiveRecord::Base
  validate :clean_loveTo

  protected
  def clean_loveTo
    errors.add(:loveTo, "can't contain swears") if antiSwear.checkSwear(loveTo)
  end
end
类配置文件

我假设checksware在这里返回一个布尔值。

我会在数组上使用一个交集,其中一个交集是将源文本拆分为单词,然后将替换项放在其中。你必须确保单词和它们的替换之间有1:1的关系,在这种情况下,我建议在你的字典中使用散列(恰巧在其他语言中有时称为散列)


如果替换内容在
\n
&
\r
内容上挂起,您可能需要使用
文本.split
参数进行进一步修改。

在提交之前?您需要在提交前进行Ajax调用。有多种方法可以执行此操作,或者使用@hakunin的建议并将其作为正常模型验证的一部分。此外,您的字段名不符合Rails约定。loveTo应该改名为love_to,hateTo应该改名为hate_to
class Profile < ActiveRecord::Base
  validate :deny_swearing

  private
  def deny_swearing
    if AntiSwear.check_swear(love_to) || AntiSwear.check_swear(hate_to)
      errors.add_to_base('Swearing is not allowed.')
    end
  end
end
class Profile < ActiveRecord::Base
  validate :clean_loveTo

  protected
  def clean_loveTo
    errors.add(:loveTo, "can't contain swears") if antiSwear.checkSwear(loveTo)
  end
end
module antiSwear

  # var names changed for formatting
  @swears = ["f**k", "f***ing", "shit", "shitting", "lecturer"]
  @cleans = ["fornicate", "copulating", "poop", "pooping", "Jonathan"]

  def self.checkText(text)
    # array intersection. "which elements do they have in common?"
    bad = @swears & text.split # text.split = Array
    # replace swear[n] with clean[n]
    bad.each { |badword| text.gsub(/#{badword}/,@cleans[@swears.index(badword)] }
  end

end