Reputation: 6071
I'm trying to create a Contact Us form on my website using Active Model.
The problem I am having is errors messages are never returned.
I'm using a remote form.
routes:
resource :front_contacts, only: :create, controller: :front_contact
controller:
class FrontContactController < ApplicationController
def create
contact = FrontContact.new(params[:front_contact])
@errors = contact.errors.size
end
end
front_contact:
class FrontContact
include ActiveModel::Model
attr_accessor :name, :email, :message
validates_presence_of :name, :message
validates_format_of :email, with: /[a-zA-Z0-9._%-]+@(?:[a-zA-Z0-9-]+\.)+(com|net|org|info|biz|me|edu|gov)/i
end
js.erb:
alert(<%= @errors %>);
The alert is always alerting zero.
Please advise.
Upvotes: 1
Views: 2019
Reputation: 675
If you're using Rails 4, there's the new inclusion of strong params that may be preventing your models from getting created.
Do you have something like the following anywhere in your controller?
params.require(:front_contact).permit!
I had the same problem as you when initially switching over to Rails 4, and was baffled because not permitting specific params doesn't throw an error message; the object just doesn't get created.
If you have a RailsCasts account, there's a really great video on how to deal with strong params here.
Upvotes: 1