Some suggestions on the Ruby on Rails view _ruby topics

Source: Internet
Author: User
Tags ruby on rails


Do not call the model layer directly from the view.
Do not construct complex formats in the view, and output them to a method or model of the view helper.
Use partial templates and layouts to reduce duplicate code.

Join the client side validation to the usual validators. The steps to do are:

Declares 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 =/
    else
    self.status = 404
    End
    Super
  end End

Create a new file Public/javascripts/rails.validations.custom.js.coffee and add a reference to it 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.