For suggestions on Ruby on Rails view writing, rubyrails
Do not directly call the model layer from the view.
Do not construct complex formats in the view and output them to a method or model of the view helper.
Use the partial template and layout to reduce repeated code.
Add client side validation to the usual validators. The steps are as follows:
Declare a custom validator from ClientSideValidations: Middleware: Base
module ClientSideValidations::Middleware class Email < Base def response if request.params[:email] =~ /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i self.status = 200 else self.status = 404 end super end end end
Create a new public/javascripts/rails. validations. custom. js. coffee file and add a reference in your application. js. coffee file:
# app/assets/javascripts/application.js.coffee #= require rails.validations.custom
Add your client validator:
#public/javascripts/rails.validations.custom.js.coffee clientSideValidations.validators.remote['email'] = (element, options) -> if $.ajax({ url: '/validators/email.json', data: { email: element.val() }, async: false }).status == 404 return options.message || 'invalid e-mail format'