Wednesday, 19 April 2017

Ruby Arrays: Insert, Append, Length, Index, Remove

In this tutorial, we will cover:
  • How to create a Ruby array
  • How to access Elements
  • How to add elements to a list
  • How to delete elements from a list

Create a Ruby Array

Arrays can be created in various ways. All you need is a variable name.
The first two examples instantiate an array in the letters variable. Take note that they won’t have any values.
Arrays can be created by getting an instance of the Array class.
2.0.0-p0 :002 > letters = Array.new
 => [] 
A nice shortcut for this is to just give the value of [].
 2.0.0-p0 :003 > letters = []
 => [] 
Most of the time, we would like to have values in arrays we create. Here’s a very simple example on how to do it.
2.0.0-p0 :006 > letters = Array.new(['a', 'b'])
 => ["a", "b"] 
Again, there’s a shortcut.
2.0.0-p0 :001 > letters = ['a', 'b', 'c']
 => ["a", "b", "c"] 
Note that the long method is quite useful when you want to define how many elements your array would be.
2.0.0-p0 :007 > letters = Array.new(3)
 => [nil, nil, nil] 
We can also add default values to the elements
2.0.0-p0 :008 > letters = Array.new(3, 'me rocks')
 => ["me rocks", "me rocks", "me rocks"]

Accessing Elements

Accessing elements in Ruby is easy. Just like in most languages, arrays can be accessed via their indexes.
Now let’s create an array.
2.0.0-p0 :009 > letters = Array.new(['a', 'b', 'c'])
 => ["a", "b", "c"] 

Accessing the first element.

Usually done by using the index 0
2.0.0-p0 :010 > letters[0]
 => "a"

Accessing the last element.

2.0.0-p0 :011 > letters[-1]
 => "c"
Negative values go in reverse starting at -1, the last element. Naturally, the second to the last element would be accessed by [-2].
2.0.0-p0 :012 > letters[-2]
 => "b"

Inserting / Adding Elements

Inserting elements in a ruby array is very straight forward. We have 3 methods on how to insert elements on arrays.
Let’s try them on our example.
2.0.0-p0 :014 > letters = Array.new(['a', 'b', 'c'])
 => ["a", "b", "c"]

Using Ruby’s Array’s insert method

I love the insert method because it allows you to insert elements at various places in the array.
2.0.0-p0 :015 > letters.insert(0, 1)
 => [1, "a", "b", "c", "d"]
2.0.0-p0 :016 > letters.insert(-1, 'd')
 => [1, "a", "b", "c", "d"]

Using the “double less than” method

2.0.0-p0 :017 > letters << 'e'
 => [1, "a", "b", "c", "d", "e"] 

Using the Ruby’s array’s push method

2.0.0-p0 :018 > letters.push('f')
 => [1, "a", "b", "c", "d", "e", "f"] 

Removing Elements

If we can insert elements, why can’t we delete some?

Using the Ruby’s array’s pop method

The pop method let’s you remove the last element in the array.
2.0.0-p0 :021 > letters = Array.new(['a', 'b', 'c', 'd'])
 => ["a", "b", "c", "d"] 
2.0.0-p0 :022 > letters.pop
 => "d" 
2.0.0-p0 :023 > letters
 => ["a", "b", "c"]

Using the Ruby’s array’s delete_at method

When deleting an element at a particular index, delete_at is your method.
2.0.0-p0 :028 > letters = Array.new(['a', 'b', 'c', 'd'])
 => ["a", "b", "c", "d"] 
2.0.0-p0 :029 > letters.delete_at(2)
 => "c" 
2.0.0-p0 :030 > letters
 => ["a", "b", "d"]
The Ruby language is very a expressive language. There’s a bunch of methods you can use in every situation. This is perhaps why Ruby (and Rails) has seen a large growth of developers usage in the last few years.
I oblige you to learn Ruby and see what you think.

Monday, 27 March 2017

AJAX PARTIAL

$('#edit_event_block').html('<%= j render partial: "events/form", locals: {event: @event} %>');

Wednesday, 1 February 2017

JQuery Validation Engine rails

1.Add this line to your application's Gemfile:-

     gem 'jQuery-Validation-Engine-rails'

install:-
           bundle
                or
           gem install jQuery-Validation-Engine-rails

2.CSS-application.css:-
    
     *= require validationEngine.jquery

3. JS application.js:-

   //= require jquery.validationEngine-en
   //= require jquery.validationEngine

4.Form field:-
 
       <form class="form_id">
          <%= f.text_field :name, :class => "span2 validate[required] text-input" %>
          or
          <%= f.text_field :name, :class => "validate[required,custom[email]]" %>
       <form>

<script type="text/javascript">
  $(document).ready(function(){
    $("#form_id").validationEngine();
  });
</script>

Thursday, 5 January 2017

select_tag with options_for_select

An example of using options_for_select with select_tag

<%=select_tag 'category_id', options_for_select(Category.all.collect{ |u| [u.name, u.id] })
 %>

This would generate something like:
<select id="user_id" name="user_id">
  <option value="1">Brad</option>
  <option value="2">Angie</option>
  <option value="3">Jenny</option>
</select>



Monday, 2 January 2017

Images Version end size

1:- Create new gem - gem 'mini-magick'

2. Uploader

  class MyUploader < CarrierWave::Uploader::Base
  include CarrierWave::MiniMagick
version :thumb do
    process resize_to_fill: [200,200]
  end


 version :medium do
    process resize_to_fill: [400,400]
  end
version :project do
    process :resize_to_fill => [275, 180]
  end

  version :comment do
    process :resize_to_fill => [580, 0]
  end  

  version :projectimg do
    process resize_to_fit: [275,180]
    process :convert => :jpg   
  end

  version :featuredmd do
    process :resize_to_fill => [585, 350]
  end

  version :featuredsm do
    process :resize_to_fill => [400, 350]
  end

  version :topicbanner do
    process :resize_to_fill => [800, 350]
  end 

  version :topicmd do
    process :resize_to_fill => [450, 350]
  end 

  version :topiclg do
    process :resize_to_fill => [650, 350]
  end 
  version :userbanner do
    process :resize_to_fill => [800, 350]
  end

3. Version create: upload -index.erb
  image.url(:version name)

Friday, 23 December 2016

Without devise sign_in sign_up

gem 'bcrypt'

User.rb:

        validates :email, presence: true, uniqueness: { case_sensitive: false }
validates :password, presence: true
validates_length_of :password, :minimum => 8
validates_format_of :email, with: /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i,:if => lambda{ |object| object.email.present? }
require 'bcrypt'
# before_create :encrypt_password
before_save :encrypt_password, if: :password_changed? #will_save_change_to_password?
private

def encrypt_password
self.password = BCrypt::Password.create(self.password)
end

def password_changed?
# will_save_change_to_password?
will_save_change_to_attribute?(:password)
end


# for This is after save
# after_save :encrypt_password
# def encrypt_password
# if saved_change_to_password?
# password = BCrypt::Password.create(self.password)
# self.update_column(:password, password)
# end
# end

routes:


resources :users do
  resources :comments
  collection do
     get :sign_in
     post :session_create
     delete :session_destroy
    end

  end


User_controller.rb:

def session_destroy
    session[:user_id] = nil
    redirect_to root_url, :notice => "Logged out"
  end


  def session_create
    require 'bcrypt'
   user = User.find_by_email(params[:user][:email])
   password =  params[:user][:password]
    if user.present?
        my_password = BCrypt::Password.new(user.password)
      if my_password == password     #=> true
        session[:user_id] = user.id
        redirect_to root_url, :notice => "Logged in!"
      else
        flash.now.alert = "Invalid email or password"
        render "new"
      end
    else
      redirect_to :back, :notice => "Email Not found, please enter correct email."

    end
  end

  def sign_in
    current_user = User.find_by_id(session[:user_id])
    if current_user.present?
      flash[:notice]= "you allready login"
      redirect_to root_url
    else
      @user = User.new
    end
  end