activemodel

Change or update an attribute value during Rails ActiveRecord validation

扶醉桌前 提交于 2020-08-27 05:58:05
问题 Summary : I'm trying to alter an attribute's value within a custom ActiveModel::EachValidator validator. Given the following prototype: def validate_each(record, attribute, value) trying to set value = thing doesn't appear to do anything -- am I missing something? There should be a smart way to do this... Detail : I accept a URL input as part of a site. I don't want to just take the URL and directly validate that it returns a 200 OK message, because that would ignore entries that didn't start

Validate date and time fields together in rails model

此生再无相见时 提交于 2020-01-23 23:21:50
问题 EDIT: In case the question below looks a bit 'extensive', the summary is that I just want to combine a date field and an optional time field in my model purely for the purpose of validating it using a date validator but I can't get my test to fail correctly when I pass in a string as the time. EDIT 2: Since I'm still struggling to find a way to join a date object with a time object for the purpose of validation then I wondered if I could just validate that the time value was a Time object

Validate date and time fields together in rails model

旧城冷巷雨未停 提交于 2020-01-23 23:21:13
问题 EDIT: In case the question below looks a bit 'extensive', the summary is that I just want to combine a date field and an optional time field in my model purely for the purpose of validating it using a date validator but I can't get my test to fail correctly when I pass in a string as the time. EDIT 2: Since I'm still struggling to find a way to join a date object with a time object for the purpose of validation then I wondered if I could just validate that the time value was a Time object

Rails 4: remove attribute name from error message in custom validator

寵の児 提交于 2020-01-16 04:30:26
问题 In my Rails 4 app, I implemented a custom validator named LinkValidator on my Post model: class LinkValidator < ActiveModel::Validator def validate(record) if record.format == "Link" if extract_link(record.copy).blank? record.errors[:copy] << 'Please make sure the copy of this post includes a link.' end end end end Everything works fine, except that currently, the message displayed is: 1 error prohibited this post from being saved: Copy Please make sure the copy of this post includes a link.

How to handle date fields in a non-model Rails form?

穿精又带淫゛_ 提交于 2020-01-15 04:25:08
问题 I am creating a non-model object that will be used with a Rails form builder by using ActiveModel . This is a Rails 3 project. Here's an example of what I have so far: class SalesReport include ActiveModel::Validations include ActiveModel::Conversion extend ActiveModel::Naming attr_accessor :promotion_code, :start_date, :end_date def initialize(attributes = {}) attributes.each do |name, value| send("#{name}=", value) end end def persisted? false end end I happen to be using HAML and simple

Rails ActiveRecord:: Proper way for validating presence on associations?

邮差的信 提交于 2020-01-06 02:36:07
问题 I have a Rails association between models Project and Queue . A project has_many queues. A queue must have a project, and consequently has a presence validation on project_id Suppose I want to create a new project WITH queues. For example, something like this: project = Project.new(valid: param, options: here) project.queues << Queue.new(other_valid: param, options: here) project.save! The save is going to fail because the queues fail the project_id presence validation. My usual ugly way of

How to add a User model validation to block certain email domains

我们两清 提交于 2020-01-05 04:15:10
问题 I would like to have a list of email domains that are validated against to prevent from registering on my app. 10minutemail.com, yopmail.com, mail.com, mail.ru etc... I have a list of domains in my user model like so: BAD_DOMAINS = [/10minutemail.com/, /yopmail.com/, /mail/ I would like to add a User validates on the email field to add an error if the user is registering with one of these domains. BAD_DOMAINS.each { |rule| return true if !domain.match(rule).nil? } I have that regex working,

Does rti/FastXml, or anything like it, work with rails 3 to speed-up xml rendering?

守給你的承諾、 提交于 2020-01-03 05:58:18
问题 We have a REST api implemented in rails 3 , which is using the yajl back-end for json . It is currently generating json responses significantly faster than xml when serializing more than about 20 rows. My next thought was that there must be a similar C-library drop-in for rail 3, which would use libxml or similar for to_xml , just as we're already using libyajl for to_json . To date, I've found only the rti/FastXml project: https://github.com/rti/FastXml It claims to be what I want, but hasn

ActiveModel::SecurePassword undefined method `password_digest='

馋奶兔 提交于 2020-01-01 18:58:24
问题 I try to use rails 3.1 ActiveModel::SecurePassword by following http://bcardarella.com/post/4668842452/exploring-rails-3-1-activemodel-securepassword and I end up with red light ... user.rb class User < ActiveRecord::Base has_secure_password validates :password, :presence => { :on => :create } end factory.rb Factory.define :user do |f| f.email "foo@bar.com" f.password "foobar" f.password_confirmation { |u| u.password } end spec_user.rb describe User do it "should authenticate with matching

ActiveModel::SecurePassword undefined method `password_digest='

戏子无情 提交于 2020-01-01 18:55:09
问题 I try to use rails 3.1 ActiveModel::SecurePassword by following http://bcardarella.com/post/4668842452/exploring-rails-3-1-activemodel-securepassword and I end up with red light ... user.rb class User < ActiveRecord::Base has_secure_password validates :password, :presence => { :on => :create } end factory.rb Factory.define :user do |f| f.email "foo@bar.com" f.password "foobar" f.password_confirmation { |u| u.password } end spec_user.rb describe User do it "should authenticate with matching