ActiveModel::Validations on anonymous class
ActiveModel tries to get the model name (as you see here) when setting up the error messages. The quickest way to get around it (short of giving your anonymous class a name), is to give your class a class method model_name
that returns an instance of ActiveModel::Name
.
for example
require 'active_model'
book_class = Class.new do
include ActiveModel::Validations
def self.model_name
ActiveModel::Name.new(self, nil, "temp")
end
validates_presence_of :title
def title; ""; end # This will fail validation
end
book_class.new.valid? # => no error
Validation not working on Class that extends ActiveModel::Naming, include ActiveModel::Validations
In your controller you are setting @contact_model
to a hash, params[:contact_model]
, and then calling valid?
on it. You need create an instance of ContactModel and call valid on that. Like so:
@contact_model = ContactModel.new(params[:contact_model])
if (@contact_model.valid?)
...
I see commented out code that calls ContactModel.new(), but that's not how you want to do it anyway. Also, there is no reason to dup() or initialize_copy() on the params stuff. Active Model Validations rails 3
ActiveModel validations do not provide client side validation. If you'd like to use your Rails validators on the client side, I'd suggest the client_side_validations gem.
If you're having trouble getting started, I'd suggest performing a single, simple validation in your model and verifying that it works before trying to move it client-side. For example, in your Message
class:
# app/models/message.rb
class Message
include ActiveModel::Validations
attr_accessor :sender
validates :sender, presence: true
end
# in the console
m = Message.new
m.valid? #=> false
m.errors.full_messages #=> ["Sender can't be blank"]
Then start working with other types of validates
, like length or format, then custom validations with the validate
method, and then if you finally feel like you need it, a full validation class using validates_with
. How to test a custom validator?
Here's a quick spec I knocked up for that file and it works well. I think the stubbing could probably be cleaned up, but hopefully this will be enough to get you started.
require 'spec_helper'
describe 'EmailValidator' do
before(:each) do
@validator = EmailValidator.new({:attributes => {}})
@mock = mock('model')
@mock.stub('errors').and_return([])
@mock.errors.stub('[]').and_return({})
@mock.errors[].stub('<<')
end
it 'should validate valid address' do
@mock.should_not_receive('errors')
@validator.validate_each(@mock, 'email', 'test@test.com')
end
it 'should validate invalid address' do
@mock.errors[].should_receive('<<')
@validator.validate_each(@mock, 'email', 'notvalid')
end
end
Related Topics
Private Messages with Faye and Rails
How to Access a Toplevel Entity in Ruby, from Inside a Module Which Defines The Same Name
Where Are Keywords Defined in Ruby
Mongodb Server Doesn't Start at Gitlab Runner Using Gitlab-Ci
Without Converting to a String, How Many Digits Does a Fixnum Have
In Ruby, How to Be Warned of Duplicate Keys in Hashes When Loading a Yaml Document
Modifying Text Inside HTML Nodes - Nokogiri
Puzzled Over Palindromic Product Problem
Prawn Doesn't Seem to Push Layout Down When Using Repeat(:All)
Rails 4 - Devise: Getting Actioncontroller::Unknownformat on Signup
Timeout When Installing Ruby Gems
In Ruby, Is Truthiness Idiomatic for a Method Name Ending with a Question Mark
Why Is Split(' ') Trying to Be (Too) Smart
Define_Method with Predefined Keyword Arguments
Loading/Unloading/Updating Class in Ruby
Why Are Global Methods Allowed to Be Defined Outside of a Class in Ruby