Posts

Showing posts from January, 2013

Jquery Auto Tab for Phone fields

Its a good thing, If  we implement autotab for telephone fields. It would be more nice. There are many Jquery plugins. But here I created my own simple autotab with a jquery function.

Here I'm explaining with rails application

Add onKeyup javascript event autotab() to your text fields in which you want to implement autotab

Then View should look like the following

<%=text_field_tag :isd, nil, :maxlength => 3, :onKeyup => "autotab(this)" %>
<%=text_field_tag :std, nil, :maxlength => 5, :onKeyup =>"autotab(this)"%>
<%=text_field_tag :num, nil, :maxlength => 12 %>

Then add the js function to your application.js

function autotab( field ){
if(field.getAttribute&&field.value.length==field.getAttribute("maxlength")){
    $(field).next('input').focus();
  }
}

This will do the autotab thing.
For any query please comment on the same thread. Thank you.

Rails mongoid has field model validation

This can be done only with a custom method validation, Such as
class Comment        include Mongoid::Document
       include Mongoid::Timestamps  include ActiveModel::Validations
validate :must_be_friends
def must_be_friends    errors.add(:base, 'Must be friends to leave a comment') if hash[:firstname].nil? end end

Rails 3.2 Ajax Paperclip Image uploading

I was working on uploading images using paperclip gem . I wanted to use Ajax. I googled a lot, But every result says its not possible, that  you can't upload images using Ajax. Some Jquery plug-ins are available, But its not behaving like what I really needed in my project.

Finally I got the solution for this problem. It is a common solution. First of all download the the js code and save it as jquey.form.js in assets/javascripts. Include in application.js like

//= require jquery.form

Next is your view. Which may look like this

<div id="image_view">
      <%=image_tag @model.image_attr_name.url %>
    </div>

Next is your form to upload image
[You can use bootstrap modal to render this form. Then it'll be more beautiful]

<%=form_for @model, :html => { :multipart => true, :id => "form_id" }, :url => url_for(:controller => 'controller', :action => 'method') do | f | %>
     Upload picture : <%= f.file_fi…