Ruby on rails 限制rails模型中的字数
我有Meets model meet.rb和一个名为tags的属性。 用户使用单个表单输入它们。 问题是:我如何在我的模型中限制,若用户输入超过3个单词,他将得到一条错误消息。 或者,如果您可以向我展示使用javascript的解决方案,那也很好。Ruby on rails 限制rails模型中的字数,ruby-on-rails,ruby,Ruby On Rails,Ruby,我有Meets model meet.rb和一个名为tags的属性。 用户使用单个表单输入它们。 问题是:我如何在我的模型中限制,若用户输入超过3个单词,他将得到一条错误消息。 或者,如果您可以向我展示使用javascript的解决方案,那也很好。 谢谢大家! 如果没有任何代码,我会认为您有一个带有参数和标记的方法。如果是这样的话,你可以这样做 def method(tags) array = tags.split(" ") raise ArgumentError.new("Mor
谢谢大家! 如果没有任何代码,我会认为您有一个带有参数和标记的方法。如果是这样的话,你可以这样做
def method(tags)
array = tags.split(" ")
raise ArgumentError.new("More than three words entered") if array.size > 3
end
如果没有任何代码,我会认为您有一个带有参数和标记的方法。如果是这样的话,你可以这样做
def method(tags)
array = tags.split(" ")
raise ArgumentError.new("More than three words entered") if array.size > 3
end
如果没有任何代码,我会认为您有一个带有参数和标记的方法。如果是这样的话,你可以这样做
def method(tags)
array = tags.split(" ")
raise ArgumentError.new("More than three words entered") if array.size > 3
end
如果没有任何代码,我会认为您有一个带有参数和标记的方法。如果是这样的话,你可以这样做
def method(tags)
array = tags.split(" ")
raise ArgumentError.new("More than three words entered") if array.size > 3
end
我可能会在
Meet
模型中编写自定义验证方法:
class Meet < ActiveRecord::Base
# ...
validate :no_more_than_three_tags
# ...
def no_more_than_three_tags
errors.add(:tags, 'more than three words') if tags.split(/\W/).count > 3
end
end
类满足3,则添加(:tags,“超过三个单词”)
结束
结束
我可能会在Meet
模型中编写自定义验证方法:
class Meet < ActiveRecord::Base
# ...
validate :no_more_than_three_tags
# ...
def no_more_than_three_tags
errors.add(:tags, 'more than three words') if tags.split(/\W/).count > 3
end
end
类满足3,则添加(:tags,“超过三个单词”)
结束
结束
我可能会在Meet
模型中编写自定义验证方法:
class Meet < ActiveRecord::Base
# ...
validate :no_more_than_three_tags
# ...
def no_more_than_three_tags
errors.add(:tags, 'more than three words') if tags.split(/\W/).count > 3
end
end
类满足3,则添加(:tags,“超过三个单词”)
结束
结束
我可能会在Meet
模型中编写自定义验证方法:
class Meet < ActiveRecord::Base
# ...
validate :no_more_than_three_tags
# ...
def no_more_than_three_tags
errors.add(:tags, 'more than three words') if tags.split(/\W/).count > 3
end
end
类满足3,则添加(:tags,“超过三个单词”)
结束
结束
最好在客户端进行检查
在
app/assets/javascripts
包括到
app/assets/javascripts/application.js.coffee
此文件
#=需要word\u couter
$ ->
$('[name="you_input"]').on "change", ->
input_size = $(@).val().split " "
if input_size.length > 3
...some code here ....
我希望这有帮助 最好在客户端进行检查
在
app/assets/javascripts
包括到
app/assets/javascripts/application.js.coffee
此文件
#=需要word\u couter
$ ->
$('[name="you_input"]').on "change", ->
input_size = $(@).val().split " "
if input_size.length > 3
...some code here ....
我希望这有帮助 最好在客户端进行检查
在
app/assets/javascripts
包括到
app/assets/javascripts/application.js.coffee
此文件
#=需要word\u couter
$ ->
$('[name="you_input"]').on "change", ->
input_size = $(@).val().split " "
if input_size.length > 3
...some code here ....
我希望这有帮助 最好在客户端进行检查
在
app/assets/javascripts
包括到
app/assets/javascripts/application.js.coffee
此文件
#=需要word\u couter
$ ->
$('[name="you_input"]').on "change", ->
input_size = $(@).val().split " "
if input_size.length > 3
...some code here ....
我希望这有帮助 标签是作为数组还是字符串传入的?请显示控制器和模型代码。标签是作为数组还是字符串传入的?请显示控制器和模型代码。标签是作为数组还是字符串传入的?请显示控制器和模型代码。标签是作为数组还是字符串传入的?请显示控制器和模型代码。