Rails - 将方法添加到文本字段
我试图让 checkSwear 方法在提交之前在每个文本字段上运行。
我基本上是这样的:(精简)
<%= 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 %>
在我的控制器中我有:
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这确实应该在模型验证中完成。
也就是说,如果您坚持将其放在控制器中,您可以检查 params[:profile][:love_to] 和
params[:profile][:hate_to]
来查看已提交的内容。PS 在这个例子中,我使用了正确的 ruby 命名约定,因为我们不使用“camelCasing”。
This should really be done in model validations.
That said, if you insist on this being in controller, you can check
params[:profile][:love_to]
andparams[: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".
您这样做是作为验证的一部分吗?您可以通过以下几种方法之一来完成此操作。您可以在保存之前通过自定义验证方法运行检查或直接覆盖设置器。我在这里向您展示自定义验证方法:
我假设 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:
I'm assuming checkSwear returns a boolean here.
我会在数组上使用交集,其中之一是将源文本拆分为单词,然后 gsub 替换其中的内容。您必须确保单词与其替换内容之间存在 1:1 关系,在这种情况下,我'建议为您的字典使用哈希(巧合的是,有时在其他语言中也称为哈希)。
如果替换挂在
\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).
You might need to futz with
text.split
arguments if the replacement gets hung up on\n
&\r
stuff.