Tag: validation

多模型保存,如何包装事务和报告错误

我有一个注册表单模型,在注册期间接受用户输入: class RegForm include ActiveModel::Model include ActiveModel::Validations attr_accessor :company_name, :email, :password validates_presence_of # … end 在此注册过程中,我有多个需要创建的模型,我不确定如何正确显示错误消息以及如何将模型错误消息冒泡回UI。 if @reg_form.valid? account = Account.create!(@reg_form) else … Account.create! 好像: def self.create!(reg_form) account = Account.create_from_reg!(reg_form) location = location.create_from_reg!(account, reg_form) .. .. account.location = location .. account.save! account end 所以我很困惑如何显示所有这些节省的模型的错误消息 如果reg_form没有所有其他模型的正确数据,如何显示或失败validation。 如何确保将其包含在事务中,因此如果在注册期间任何模型无法保存,我不会保存任何内容。

带参数的Railsvalidation

在我的下面的代码中,我试图调用url_exist? 作为网站的validation,但我不确定如何调用url_exist? 与网站的价值。 这似乎是一个愚蠢的问题,但我无法弄清楚。 模型 validates :website, presence: true, if: :url_exist? … def url_exist?(url_string) url = URI.parse(url_string) puts url binding.pry req.use_ssl = (url.scheme == ‘https’) req = Net::HTTP.new(url.host, url.port) path = url.path if url.path.present? req.request_head(path || ‘/’) res.code != “404” # false if returns 404 – not found rescue Errno::ENOENT false # false if can’t […]

收集铁路3中所有路线的第一段

我正在尝试实现路由,其中​​第一个段是配置文件的别名或id: resources :profiles, :path => ” do … end 我需要validation其他(更高)路由的第一段尚未采用别名。 我现在拥有的是: validates :alias, :exclusion => {:in => Rails.application.routes.routes.map{|r| r.path.spec.to_s.split(‘(‘).first.split(‘/’).second}.compact.uniq }, …. 在开发过程中一切都很好。 在生产中Rails.application.routes.routes.map…返回空数组。 但是只有在模型中validation,如果我把它放在视图中只是为了测试它返回所有路径的第一段数组按预期。 我做错了什么或者有更好的解决方案?

Rails 3:如何validationA <B,其中A和B都是模型属性?

我想validationcustomer_price >= my_price 。 我尝试了以下方法: class Product my_price … end ( customer_price是数据库中Products表中的一列,而my_price则不是。) 结果如下: NameError in ProductsController#index undefined local variable or method `my_price’ for # 在Rails 3中执行此操作的正确方法是什么?

Rails没有显示validation错误消息

嗨,伙计们,我是铁杆新手,只做我的第一份报名表。 我有一些validation,我想要rails检查,但由于某种原因,它不会显示错误消息。 在views / user / signup.html.erb上我有这个 Register Now! { :action => “signup” } do |f| %> “Please Try Again!”, :message => “We had some problems processing your registration” %> 在模型上我有以下内容 class User 4..15 validates_length_of :password, :within => 3..520 validates_presence_of :first, :last, :username, :email, :password, :password_confirmation validates_uniqueness_of :username, :email validates_confirmation_of :password validates_format_of :email, :with => […]

Rails:validation多个模型的唯一性

有没有办法在两个不同模型中的列之间validation属性的唯一性。 例如: 我有自行车模型和汽车模型。 当我创建一个新的自行车时,我想validation自行车的名称是独一无二的,因为没有其他自行车或具有该名称的汽车。 我不想将它们放在一个模型中,因为它们具有非常不同的属性。 我在轨道上2.3.8 谢谢。

在validation之前循环通过自我属性进行修改

我创建了一个简单的before_validation: before_validation :strip_tabs def strip_tabs end 在我的课程中,我想循环遍历所有属性并从每个值中删除制表符。 我在SO上找到的大多数post都是想要设置1个属性的人。 但我想编辑我的所有价值观。 问题:如何循环模型的所有自属性并进行编辑。 朋友建议了这个,但是content_column_names不存在: self.content_column_names.each {|n| self[n] = self[n].squish} 更新1:更多代码: class PersonalInfo include ActiveModel::Validations include ActiveModel::Validations::Callbacks extend ActiveModel::Translation extend ActiveModel::Callbacks include Sappable require ‘ext/string’ attr_accessor \ :first_name, :middle_name, :last_name,:birthdate,:sex, :telephone,:street,:house_number,:city,:postal_code,:country, :e_mail, :nationality, :salutation, :com_lang validates :e_mail, :email => {:strict_mode => true} validate :validate_telephone_number validate :age_is_min_17? before_validation :strip_tabs def […]

Rails图像URLvalidation

假设我在表中有记录,并且每个都有一个图标属性,它采用表单的URL: 球/ x.png 球/ y.png 球/ z.png … 如何编写validation,确保url以“balls /”开头,以.png,.gif结尾。 还是.jpg? 我当前的validation只是检查文件扩展名: validates_format_of :icon, :with => %r{\.(gif|jpg|png)$}i, :message => ‘must be a URL for GIF, JPG ‘ + ‘or PNG image.’

Rails:为什么“格式化”(正则表达式)validation失败?

我有以下产品价格validation: class Product true, :format => PRICE_REGEX … end 它应该允许价格从0到999999.99 。 但是,如果我输入hello ,则validation通过,并将0.00保存在数据库中。 但是:presencevalidation工作正常。 我在这里想念的是什么?

后端validation轨道

如何validation文本列不包含网站,示例可以是: www.google.com google.com http://gooogle.com http://www.google.com https://www.google.com https://google.com 我想在正面但在后端也这样做。 我现在对后端更感兴趣,因为我将在稍后处理前端 问题更新: 根据MrYoshiji提供的示例,我提出了未涵盖的案例: http://rubular.com/r/VGgWyfIt7R 请参阅文本中间的http://www.google.com ? 它不匹配? 这正是我需要匹配的东西。 所以我可以抛出validation错误,说你不能放网站。