Rails 4 Active Model Validations -
i'm trying create contact form on website using active model.
the problem having errors messages never returned.
i'm using 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 alerting zero.
please advise.
if you're using rails 4, there's new inclusion of strong params may preventing models getting created.
do have following anywhere in controller?
params.require(:front_contact).permit!
i had same problem when switching on rails 4, , baffled because not permitting specific params doesn't throw error message; object doesn't created.
if have railscasts account, there's great video on how deal strong params here.
Comments
Post a Comment