Web Development Blog

Ruby on Rails (ROR), an open source framework, is used extensively for web development. It uses Ruby as the programming language which is quite similar to Python or Perl. The USP of ROR lies in its ease of use and speed. This is the reason why developers prefer Ruby on Rails over the other frameworks. Any changes made in the app are immediately applied. This saves time at the developing stages. Hence, it is a faster system to work with. As an open source it supports some of the most widely used engineering principles and patterns. Hence, achieving success in completion of tasks becomes an easy method. Further support is also integrated within while developing as application. The skeleton test code is used easily to write test in a simplified way. By using ROR it can also be ensured that the code is in sync with the functionalities mentioned earlier…

View original post 611 more words

Advertisements

Common mistakes in RoR

Posted: October 24, 2014 in Uncategorized

Few common mistakes, must be taken care of while developing RoR applications.

via Common mistakes in RoR.

Here we will discuss about the server side datatable with rails 4.
I was working with a project where we need to list/show a few thousands of records in a table, pagination, sorting  and also advanced searching for those records. So for this scenario I had to use a datatable (server side).

So today we will see how the datatable can be used with serverside in rails 4.

jQuery Datatable can be used for sorting, searching, paginating a number of records in a table.

new

First of all we need include the js and css in the application.js and the application.css respectively.
example:

require dataTables/jquery.dataTables

Now first we will write the code for view then controller then datatables.
In the view, lets see a piece of code as the example.
Suppose we have to show User name, email, joining date etc.

<table id="users_listing" class="datatable-list">
  <thead>
  <tr>
    <th>User Name</th>
    <th>Email</th>
    <th>Join Date</th>
    <th>Ads</th>
    <th>Tokens</th>
    <th>Status</th>
    <th>Action</th>
  </tr>
  </thead>
  <tbody></tbody>
</table>

Now we have to make a datatable folder for the model so that we can save all the datatable at the same place , it may useful when we have more than one datatable. Like assets , controller, model folder we will make datatable folder, in that we will make user_datatable.rb

class UsersDatatable
delegate :params, :h, :link_to, :edit_user_path, to: :@view

def initialize(view)
@view = view
end

def as_json(options = {})
{
sEcho: params[:sEcho].to_i,
iTotalRecords: User.all.count,
iTotalDisplayRecords: users.total_count,
aaData: data
}
end

in the private methods we have to write the methods for data

private

def data
users.map do |user|
[
link_to(user.name, edit_user_path(user)),
user.email,
user.created_at.strftime(&quot;%m/%d/%Y&quot;),
user.ad_count,
user.token_sum,
(user.status ? 'Active': 'Inactive'),
link_to('Delete', admin_user_path(user, type: 'users'), method: :delete, data: {confirm: 'Are you sure to delete this user?'})
]
end
end

def users
@users ||= fetch_users
end

def fetch_users

if user_search.present?
#do something
end

users
end

def page
params[:iDisplayStart].to_i/per_page + 1
end

def per_page
params[:iDisplayLength].to_i &amp;gt; 0 ? params[:iDisplayLength].to_i : 10
end

def sort_column
columns = %w[first_name email created_at sum(test_23.total_ad) total status]
columns[params[:iSortCol_0].to_i]
end

def sort_direction
params[:sSortDir_0] == &quot;desc&quot; ? &quot;desc&quot; : &quot;asc&quot;
end

def user_search
#here you can write searching mechanism.
end

end

Now moving to controller

render json: UsersDatatable.new(view_context)

in the js file we need to add this

  var table = $("#users_listing").DataTable({
        iDisplayLength: 100,
        bInfo: false,
        bSort: true,
        sPaginationType: "full_numbers",
        bStateSave: true,
        bDestroy: true,
        bProcessing: true,
        bServerSide: true,
        bFilter: false,
        sAjaxSource: '/test_users/search',
        fnServerParams: function (aoData) {
            aoData.push(
                { "name": "email_or_name", "value": $("#_email_or_name").val() },
                { "name": "check_token", "value": check_token_value },
                { "name": "from_no_of_tokens", "value": from_token_value }
                { "name": "status", "value": $("#_status").val() }
            );
        },
        "aoColumns": [ null,  null,   null, { "bSortable": true}, { "bSortable": true}, null,  { "bSortable": false}],
        oLanguage:{
            sZeroRecords: "No users found."},
        "sDom": 'rtlfip'

    });

Now call the function and enjoy.
Hope this will help!!!

MindfireMobile

Introduction:
To control the synchronous or asynchronous flow, there may be more methods but we will do this by multiple callback mechanism using deferred and promise object.

Description:
In order to use this mechanism first we need to have an understanding of deferred and promise object.
Here we will use a promise with a deferred object.
A promise is a placeholder for a result, which is initially unknown. While a deferred represents the computation that results in the value. Every deferred has a promise, which functions as a proxy for the future result.
A promise is a value returned by an asynchronous function, but a deferred can be resolved or rejected by its caller which separates the promise from the resolver.

View original post 44 more words

MindfireMobile

Introduction:
When we start off as an iOS Developer, we all face a common obstacle ,that is generating the certificates and using it in our apps because Apple does not allow us to debug or run our app without Certificate. So we need an Apple Developer Certificates to develop ,debug and run.

Description:
In order to know the types and procedure to use it, first we need to have an understanding of code signing and why it is needed. So lets start with it.

Code signing our app is necessary because due to this only users trust that our app has been created by a source known to Apple. All iOS apps must be code signed and provisioned to launch on a device or to be distributed for testing, or to be submitted to the store.

View original post 277 more words

MindfireMobile

Problem:
I received a bug ticket from my client about the unusual behavior of my application UI. It was a problem which was occurring only in iOS7 only.
When the keyboard pops up in iPad devices running on iOS7 the web view of our app gets shrink (app development using Phonegap).

Solution that works for me :
This problem can be solved in few steps.

  1. We need to go to Config.xml and inside the preference tag just need to set the value of KeyboardShrinksView to false.

View original post 77 more words

Scenario:
In many cases we need to deal with a huge number of pdf files. As in my case I am currently working on a shipping project , which deals with the pdf labels so I have faced the problem to store a pdf in database in encoded form and to display the decoded pdf file inline by base64. So here I will share the steps to solve this problem.

Solution:
Data type for the column of the table where we have to save the pdf in encrypted form should be “text” type.
Lets say column name is image_hex.
example:-

(t.text     “image_hex”)

Then we need to add the base64 module.

require ‘base64’

The purpose of using base64 to encode data is that it translates any binary data into purely printable characters.

The methods for encode and decode is given below-

  1.  encode64(data) – Returns the encoded version data.
  2.  decode64(str) – Returns the Base64-decoded version of str(encrypted date).

Lets see an example.

First we will encode pdf so that we can store it in a database.

image_hex = Base64.encode64(File.open( “#{PATH}#{file_name}.pdf”).read)

Now, image_hex contains the encoded form of pdf which we can store in database.

Now if we want to delete the original pdf(physical file) file then

File.delete(“#{PATH}#{file_name}.pdf”) if File.exist?(“#{PATH}#{file_name}.pdf”)

Now we have to use the stored encoded form of the file to show in the view(Display pdf inline).

@pdf_file = Base64.decode64(database_object.image_hex).html_safe
send_data @label_image, :type => ‘application/pdf’, :disposition => ‘inline’

This will display the pdf file decode form(original form).

Hope this will help!!