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

发布于 2024-12-21 22:25:12 字数 1372 浏览 3 评论 0原文

我试图让 checkSwear 方法在提交之前在每个文本字段上运行。

我基本上是这样的:(精简)

<%= form_for(@profile) do |f| %>

  <div class="field">
    <%= f.label 'I love to ' %>&nbsp;
    <%= f.text_field :loveTo %>
  </div>
  <div class="field">
    <%= f.label 'I hate to ' %>&nbsp;
    <%= f.text_field :hateTo %>
  </div>

  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

在我的控制器中我有:

  def checkSwear
    antiSwear.checkSwear(What goes here?)
  end

在路线中:

  match '/check' => 'profiles#checkSwear'

非常感谢任何帮助!

(checkSwear 是一个单独的 gem;即一个单独的问题!这里的“what”表示从表单接收到哪种类型的变量,并通过 checkSwear gem 进行处理)

更新:

抱歉,我是一个学习 Rails 的 Java 开发人员等等,旧习难改。这是为了一个项目。我应该编写一个小 gem 来执行一些 ruby​​ 逻辑并将其应用到某些东西上。 gem 的内容是::

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 

/

I'm trying to get the checkSwear method to run on each textfield before it's submitted..

I have basically this: (stripped down)

<%= form_for(@profile) do |f| %>

  <div class="field">
    <%= f.label 'I love to ' %> 
    <%= f.text_field :loveTo %>
  </div>
  <div class="field">
    <%= f.label 'I hate to ' %> 
    <%= f.text_field :hateTo %>
  </div>

  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

In my controller I have:

  def checkSwear
    antiSwear.checkSwear(What goes here?)
  end

In routes:

  match '/check' => 'profiles#checkSwear'

Any help much appreciated!

(checkSwear is a separate gem; i.e. a separate problem! The what does here means what kind of variable is received from the form, to be put through the checkswear gem)

UPDATE:

Sorry for the camelcasing, I'm a Java developer studying Rails etc., old habits die hard. This is for a project. I'm supposed to be writing a small gem to do some ruby logic and apply it to something. The contents of the gem are:

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 

:/

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

岁月苍老的讽刺 2024-12-28 22:25:12

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

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] 来查看已提交的内容。

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

This should really be done in model validations.

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

That said, if you insist on this being in controller, you can check params[:profile][:love_to] and params[:profile][:hate_to] to see what's been submitted.

P.S. In this example I used proper ruby naming conventions, since we don't use "camelCasing".

無心 2024-12-28 22:25:12

您这样做是作为验证的一部分吗?您可以通过以下几种方法之一来完成此操作。您可以在保存之前通过自定义验证方法运行检查或直接覆盖设置器。我在这里向您展示自定义验证方法:

class Profile < ActiveRecord::Base
  validate :clean_loveTo

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

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

Are you doing this as part of validation? You can do it one of a few ways. You can run the check before save, via a custom validation method or override the setter directly. I show you the custom validation approach here:

class Profile < ActiveRecord::Base
  validate :clean_loveTo

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

I'm assuming checkSwear returns a boolean here.

愁杀 2024-12-28 22:25:12

我会在数组上使用交集,其中之一是将源文本拆分为单词,然后 gsub 替换其中的内容。您必须确保单词与其替换内容之间存在 1:1 关系,在这种情况下,我'建议为您的字典使用哈希(巧合的是,有时在其他语言中也称为哈希)。

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

如果替换挂在 \n & 上,您可能需要使用 text.split 参数进行模糊处理。 \r 的东西。

I'd use an intersection on arrays, one of which is the source text split into words, then gsub the replacements in. You have to be sure to have a 1:1 relationship between the words and their replacements, in which case I'd suggest using a hash for your dictionary (coincidentally what hashes are sometimes called in other languages).

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

You might need to futz with text.split arguments if the replacement gets hung up on \n & \r stuff.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文