Tagged Questions
48
votes
7answers
37k views
How do I validate a date in rails?
I want to be able to validate a date in my model in ruby on rails. However, the day, month and year values are already converted into an incorrect date by the time they reach my model.
For example, ...
41
votes
3answers
13k views
Ruby on Rails Callback, what is difference between :before_save and :before_create?
could you explain me in detail about :before_save and :before_create in RoR callback ? And what to do with rails validation ? Is validation works after :before_save or :before_create ?
41
votes
3answers
15k views
validation custom message for rails 3
Rails has introduced new way to validate attributes inside model.
When I use
validates :title, :presence => true
it works but when I try to add a custom message
validates :title, :presence ...
24
votes
7answers
10k views
What's a good way to validate links (urls) in rails 3?
I was wondering how I would best validate urls or links in rails 3. I was thinking of using a regular expression, but am not sure if this is the best practice.
And, if I were to use a regex, could ...
22
votes
3answers
25k views
Ruby on Rails: errors.add_to_base vs. errors.add
I have read that errors.add_to_base should be used for errors associated with the object and not a specific attribute. I am having trouble conceptualizing what this means. Could someone provide an ...
20
votes
8answers
8k views
Why doesn't Rails' “errors.full_messages” replace attribute and message variables?
Having a strange problem with a rails model I just created.
Here are my validations:
validates_presence_of :from_name, :message => 'Please provide a from name.'
validates_presence_of :from_email
...
19
votes
6answers
14k views
Validating min and max length of a string but allowing it to be blank
I have a field that I would like to validate. I want the field to be able to be left blank, but if a user is entering data I want it to be in a certain format. Currently I am using the below ...
17
votes
6answers
13k views
Valid email address regular expression? [duplicate]
I have done some testing but I wanted to ask if anyone sees a problem with this ruby regular expression for email validation:
/\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i
Look good?
Thanks,
Tony
...
14
votes
3answers
2k views
How to validate a nested model object based on the state of the parent object?
I am writing a wizard form in rails; e.g. multiple input pages for one model object.
The basics of my approach are those described in Ryan Bates' Multistep form railscast : ...
13
votes
11answers
2k views
How to remove validation using instance_eval clause in Rails?
I would like to enhance existing class using instance_eval. There original definition contains validation, which require presence of certain fields, ie:
class Dummy < ActiveRecord::Base
...
13
votes
2answers
4k views
How do I validate a non-model form in Rails 3?
Let's say I have a search form and I want to validate the length of the keyword. I want the form to highlight the invalid field (like model forms). I've looked everywhere and can't seem to find any ...
10
votes
4answers
10k views
Rails form validation conditional bypass
I have a rails model that validates uniqueness of 2 form values. If these 2 values aren't unique the validation errors are shows and the "submit" button is changed to "resubmit". I want to allow a ...
10
votes
1answer
2k views
Simple Ruby Input Validation Library
I've been looking all over the place for a simple input validation library for Ruby. Everything seems to point towards ActiveRecord (or similar). I'm not using Rails, I'm using Sinatra without an ORM. ...
9
votes
5answers
3k views
Add http(s) to URL if it's not there?
I'm using this regex in my model to validate an URL submitted by the user. I don't want to force the user to type the http part, but would like to add it myself if it's not there.
validates :url, ...
9
votes
3answers
15k views
Missing templates from new Rails 3 app?
Just tried writing a simple validates_presence_of in my model, and when the errors try to render, it calls this :
Template is missing
Missing template posts/create with {:locale=>[:en, :en], ...