Wednesday, 23 November 2016

Rails New Project

http://rakeroutes.com/blog/rvm-workflow-for-a-new-rails-app/


Creating a new Rails app

# create and use the new RVM gemset
$ rvm use --create 1.9.3@awesome_rails_project

# install rails into the blank gemset
$ gem install rails

# generate the new rails project
$ rails new awesome_rails_project

# go into the new project directory and create an .rvmrc for the gemset
$ cd awesome_rails_project
$ rvm --rvmrc 1.9.3@awesome_rails_project

# verify the rvmrc
$ cd ..; cd -

Friday, 21 October 2016

Email validation in rails and javascript by rejex

 

RAILS EMAIL VALIDATION ===>

 
# email = email.split(" ").join('')
email = "rajpurohitnitin7@gmail.com"email = email.delete(' ')
if !/\A[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]+\z/.match(email).nil?
    p "valid email address"
end



JAVASCRIPT EMAIL VALIDATION ===>

 
 
var email = "rajpurohitnitin7@gmail.com" 
var re = /\S+@\S+\.\S+/var valid = re.test(email_address)if (!valid) {
 
   console.log("valid email addresss"); 
}




Tokenfield Email validation ====>

 
 
  $(".token_filed_class").on('tokenfield:edittoken', function (e) {  }).on('tokenfield:createdtoken', function (e) {         var re = /\S+@\S+\.\S+/         var valid = re.test(e.attrs.value)         if (!valid) {            $(e.relatedTarget).empty().hide().attr('data-value', '');            //$(".share_with_emails").on('tokenfield:removedtoken', function (e) {})           //$(e.relatedTarget).addClass('invalid')         }  }).on('tokenfield:removedtoken', function (e) {  }),  showAutocompleteOnFocus: false,  createTokensOnBlur:true}); 

Monday, 10 October 2016

Upload Image from URL OR Convert Image URL into base 64 by Javascript

function getBase64ImageFromUrl(image_url){      var canvas = document.createElement("canvas");      var ctx = canvas.getContext("2d");      var img = new Image();      img.src = image_url;      canvas.width = img.width;      canvas.height = img.height;      ctx.drawImage(img, 0, 0);      var data = canvas.toDataURL("image/jpeg");
      return data;

}

OR
 
 
    var canvas = document.createElement("canvas");
    var ctx = canvas.getContext("2d");
    var img = new Image();
    img.onload = function () {
        canvas.width = img.width;
        canvas.height = img.height;
        ctx.drawImage(img, 0, 0);
        var data = canvas.toDataURL("image/jpeg");
        alert(data);
    };
    img.src = "http://localhost/MvcApplication3/test.png"; 
 
 
 
OR 
 
 
 function getBase64ImageFromUrl(image_url){ 
  var canvas = document.createElement("canvas"); 
  var ctx = canvas.getContext("2d"); 
  var img = new Image();  img.src = image_url; 
  canvas.width = img.width; 
  canvas.height = img.height; 
  ctx.drawImage(img, 0, 0); 
  var data = canvas.toDataURL("image/jpeg"); 
  return data;}
 
imgObj = $(this).parent().find('img');img = getBase64ImageFromUrl(imgObj[0].src)
 function getBase64ImageFromUrl(image_url){  var canvas = document.createElement("canvas");  var ctx = canvas.getContext("2d");  var img = new Image();  img.src = image_url;  canvas.width = img.width;  canvas.height = img.height;  ctx.drawImage(img, 0, 0);  var data = canvas.toDataURL("image/jpeg");  return data;}
  

Monday, 26 September 2016

jquery : How to select an option by its text?



down voteaccepted
This could help:
$('#test').find('option[text="B"]').val();
This would give you the option with text B and not the ones which has text that contains B. Hope this helps
EDIT:
For recent versions of jQuery the above does not work. As commented by Quandary below, this is what works for jQuery 1.9.1:
$('#test option').filter(function () { return $(this).html() == "B"; }).val();

jQuery( document ).ready(function() {
    var company_type = $("#user_company_type").val();// Option text val
    var user_type_id = $('#user_user_type_id option').filter(function () {  
               return $(this).html() == company_type }).val()
    document.getElementById('user_user_type_id').value = user_type_id;
});



                           
                           
                           
                           
                           

               RAILS SELECT TAG WITH FORM OR WITHOUT FORM

                           
= select_tag 'user_types_ids[]',options_for_select( 
       UserType.all.map {|data| [data.name, data.id]},@selected_user_types),
      {:multiple => :multiple,:id=>"user_types_ids"}



= f.select :knowledge_base_category_id, options_for_select(KnowledgeBaseCategory.all.map {|data| [data.name, data.id]},@article.knowledge_base_category_id)

                           

Wednesday, 10 August 2016

Image Data object to Image URI #rails #RUby On Rails #ImageData

# Convert data uri to uploaded file. Expects object hash, eg: params[:post]  def convert_data_uri_to_upload(data_uri)
    if !data_uri.blank?
      image_data = split_base64(data_uri)
      image_data_string = image_data[:data]
      image_data_binary = Base64.decode64(image_data_string)

      temp_img_file = Tempfile.new("data_uri-upload")
      temp_img_file.binmode
      temp_img_file << image_data_binary      temp_img_file.rewind

      img_params = {:filename => "data-uri-img.#{image_data[:extension]}", :type => image_data[:type], :tempfile => temp_img_file}
      uploaded_file = ActionDispatch::Http::UploadedFile.new(img_params)

      return uploaded_file    end
    return nil  end
 
 
 
 
OR 
 
 
require 'open-uri'def embed_remote_image(url, content_type)
  begin    # asset = open(url, "r:UTF-8") { |f| f.read }  rescue nil    asset = open(url, "r:UTF-8") { |f| f.read }
    base64 = Base64.encode64(asset.to_s).gsub(/\s+/, "")

    "data:#{content_type};base64,#{Rack::Utils.escape(base64)}"  rescue OpenURI::HTTPError => ex
  endend 

Tuesday, 9 August 2016

JAVASCRIPT IMAGE HIght Width validation

<input type='file' id='frontPageImage' name='frontPageImage'> 
 
<script> 
 $("#frontPageImage").live('change',function (e) { 
  var file, img; 
  if((file = this.files[0])) { 
    img = new Image(); 
    img.onload = function () { 
    if((this.width<499 || this.width > 599) && (this.height < 599 || this.height > 699)){
 
      alert("Front page image width in-between 500px 
            to 600px and height in-between 600px to 700px."); 
  
      $("#frontPageImage").val(''); 
 
 
 
      } 
 
      var isImage = validateImageExtention(this); 
      if(!isImage){ 
        alert("Select a image.");
        $("#frontPageImage").val(''); 
        } 
      }; 
 
  img.src = _URL.createObjectURL(file); 
 }});

</script>