Posts

Showing posts from September, 2014

Client side validations + Dynamic forms

Few things that might be helpful when working with client side validations. I am using client side validations in rails using gem

Client side validations

To validate forms dynamically added to the page, we need to use the validate function manually for that form.

For example:

If you dynamically render the following rails template into you view:

<%= form_for @model, :validate => true, :id => 'my_dynamic_form' do |f| %> <%= f.text_field :some_attribute %> <% end %>
$("form#my_dynamic_form").validate(); Reset Validations

$("form#my_dynamic_form").resetClientSideValidations();

Inserting and Moving elements inside Ruby Array

Here's is an array with 5 elements
2.1.2 :001 > arr = ["a", "b", "c", "d", "e"] => ["a", "b", "c", "d", "e"] Lets look at few different cases
Insert a new element at starting position
Using unshift 2.1.2 :002 > arr.unshift("f") => ["f", "a", "b", "c", "d", "e"] Using insert 2.1.2 :005 > arr.insert(0, "f") => ["f", "a", "b", "c", "d", "e"] In the above example 0 is the position(index)Move existing element from one position to another


The element in this case is "e"
Using uniq to show only unique elements
2.1.2 :009 > arr.insert(0, "e").uniq => ["e", "a", "b", "c", "d"] With uniq we are only showing unique records but the element is listed twice in act…