For suggestions on Ruby on Rails view writing, rubyrails

Source: Internet
Author: User

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'


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.