Tag: validation

如何validationrails 4中的嵌套属性字段?

我有两个型号 class Information < ActiveRecord::Base belongs_to :study validates_presence_of :email end 和 class Study < ActiveRecord::Base has_many :informations accepts_nested_attributes_for :informations end 我展示了一种研究forms,其中包含很少的信息字段,我想validation这些字段的存在。 只有在validation成功时我才想保存研究字段值,并且我想在validation失败时显示错误。 我怎样才能做到这一点? 提前致谢。

(rails)如何validation上传的.txt文件是否不是,例如,图像文件?

我有一个上传文本文件字段,并且我计划将文件保存在某处,然后将文件的位置存储在数据库中。 但是,我想确保他们上传的文件是.txt文件,而不是图像文件。 我想这会发生在validation步骤中。 如何validation这样的事情? 另外,如何获取上传文件的文件名? 我总是可以检查它是否说’.txt’,但为了将来参考,知道如何validation而不仅仅是文件名将有所帮助。

Railsvalidation可防止保存

我有这样的用户模型: class User true, :confirmation => true, :length => { :within => 6..40 } . . . end 在User模型中,我有一个我想要从OrdersController保存的billing_id列,如下所示: class OrdersController ‘billing id saved’) else redirect_to(root_url), :notice => @thisuser.errors) end end end end 由于validates :password用户模型中的validates :password , @thisuser.save不保存。 但是,一旦我注释掉validation, @thisuser.save返回true。 这对我来说是一个陌生的领域,因为我认为这个validation仅在创建新用户时有效。 有人可以告诉我是否validates :password每次我尝试保存在用户模型时, validates :password应该启动? 谢谢

在ruby on rails上使用jQueryvalidation提交按钮不起作用

我正在使用ruby2和rails4。 我使用jQuery进行validation。 validation错误是正确显示的,但是当我点击提交按钮时,虽然我已经为字段指定了正确的值,但它什么也没做。 有人可以帮我解决问题吗? 我的代码如下所示。 谢谢。 jQuery插件: http://ajax.aspnetcdn.com/ajax/jquery.validate/1.12.0/jquery.validate.js http://ajax.aspnetcdn.com/ajax/jquery.validate/1.12.0/jquery.validate.min.js 在download.html.erb中 :post, :id => ‘contact-form’ do |f| %> Download Form $(document).ready(function () { $(“#contact-form”).validate({ debug: true, rules: { “contact[name]”: {required: true, max: false, min: false}, “contact[email]”: {required: true, email: true}, “contact[phone]”: {required: true, digits: true, minlength: 10, maxlength: 10} } }); });

跳过相关对象的validation – rails activerecord

class Author has_many :books validates :email, :presence => true end class Book belongs_to :author validates :title, :presence => true end 跳过validation很简单: a = Author.new a.save(:validate => false) 但是,在创建书籍而不跳过书籍validation时,我需要跳过作者validation,如下所示: b = Book.new b.title = “A Book” b.author = Author.last b.save

Rails 4单选按钮形成帮助,真的没有validation

我有简单的是或否附加的单选按钮:needs_dist。 当我提交没有选择的表单时它工作得很好,但是当我选择了Yes时,它会抛出一个错误进行validation? 它仅在以下时间validation:needs_dist => true。 模型 validates_presence_of :contact_name, :email, :postal_code, :series_id, :product_id, :company_name, :needs_dist 视图 控制器(以防万一) def create_quote @quote_request = QuoteRequest.new safe_quote_params if @quote_request.save @email = SiteMailer.quote_request(@quote_request).deliver render :template => “request/quote_sent” else @series = Series.find params[:quote_request][:series_id] unless params[:quote_request][:series_id].blank? render :template => “request/quote.html” end end

如何测试ActiveRecord中哪些validation失败?

我有这样的模型: class User (2..5) end 我想测试一下这个validation: it “should not allow too short name” do u = User.new(:name => “a”) u.valid? u.should have(1).error_on(:name) end 但是它没有测试在name设置了哪种错误。 我想知道,如果是too_short , too_long ,或者其他一些validation失败了。 我可以在errors数组中查找消息文本,如下所示: u.errors[:name].should include(I18n.t(“activerecord.errors.models.user.attributes.name.too_short”)) 但是当我在locale文件中设置activerecord.errors.messages.too_short而不是特定于模型的消息时,这将失败。 那么,是否可以检查出现了哪种错误?

两个模型,一个STI和一个validation

假设我有两张桌子 – 产品和订单。 为简单起见,假设一次只能购买一个产品,因此没有像order_items这样的连接表。 所以关系是产品有很多订单,订单属于产品。 因此,product_id是Order表中的fk。 产品表是STI – 子类是A,B,C。 当用户订购子类产品C时,必须在订单模型字段order_details和order_status上检查两个特殊validation。 对于所有其他Product子类(即A和B),这两个字段可以为nil。 换句话说,当用户购买A和B时,不需要为这两个字段运行validation。 我的问题是: 如何在Order模型中编写validation(可能是自定义?),以便Order模型知道只运行ITS两个字段的validation – order_details和order_status – 当fk_id到Product子类C被保存到orders表时?

为什么我会收到“Unknown validator:’MessageValidator’”?

我收到这个错误 未知validation器:’MessageValidator’ 我不知道为什么我会这样做。 我的代码出了什么问题? validates :title, :presence => true, :uniqueness => true, :length => { :maximum => 100 }, :message => “Must be input and has to be less than 100 characters, and unique.”

ArgumentError:您需要提供至少一个validation:if

我有一个简单的模型 class Task :deadline_in_future? def deadline_in_future? Date.today < self.deadline end end 一切似乎都好,但是当我在我的rails控制台时 irb(main):001:0> Task.new ArgumentError: You need to supply at least one validation 问题出在哪儿?