Friday, 18 April 2014

Adding title to Rails view

You can add your title for views in two ways.for adding title in views you have to follow these steps:

1-> add <title><%= yield(:title) %></title> inside the header of your layout.
2-> now you can add your title inside your view.there are following ways to do that:

  a-> use this line in your view:
      <%= content_for(:title, "Title for specific page") %>

  b-> use helper method:
  •  add a helper method in application_helper

       def title(page_title)
            content_for :title, page_title.to_s
      end


  • inside your view:
           <%= title "your custom title" %>
  c-> if you want to write title for view in controller then add this method inside   your application_controller.

  def view_context
    super.tap do |view|
      view.content_for :title, "your title"
    end
  end


if you want to add title according to your controller and action names pair then:

  def view_context
    super.tap do |view|
      view.content_for :title, "#{controller_name}_#{action_name}"
    end
  end

 

Saturday, 12 April 2014

Converting Html to Pdf using wkhtmltopdf

wkhtmltopdf is open source (LGPL) command line tools to render HTML into PDF and various image formats using the QT Webkit rendering engine. These run entirely "headless" and do not require a display or display service.

wkhtmltopdf convert a html to pdf with all images and css.

1-Install wkhtmltopdf: download from link.
or
sudo apt-get install wkhtmltopdf  // in ubuntu.

2.Conversion: 
convert your html file to pdf by the following command:

wkhtmltopdf html_file_name.html pdf_name.pdf

this will create the pdf file named pdf_name.pdf inside the current directory.

Example:


 wkhtmltopdf www.yahoo.com yahoo.pdf

this command create a yahoo.pdf inside the current working directory and looks like same as html file.

 

Friday, 4 April 2014

JavaScript functions slice and splice

In JavaScript, mistaking slice for splice (or vice versa) is a common mistake. These two functions, although they have similar names, are doing two completely different things.

slice:Array’s slice is quite similar to String’s slice. According to the specification, slice needs to accept two arguments, start and end. It will return a new array containing the elements from the given start index up the one right before the specified end index. It’s not very difficult to understand what slice does:

Example:
'abcdefg' .slice(1,2)       // 'b'
'abcdefg' .slice(1,3)       // 'bc'
[1,2,3,4,5] .slice(1,2)     //  [2]
[1,2,3,4,5] .slice(1,3)     //  [2,3]


An important aspect of slice is that it does not change the array which invokes it. The following code fragment illustrates the behavior. As you can see, arr1 keeps its elements and arr2 gets the sliced version thereof.

var arr1 = [1,2,3,4,5];
var arr2 = arr1.slice(1, 2);
console.log(arr1);          // [1,2,3,4,5]
console.log(arr1);          // [2]




splice: Although splice also takes two arguments (at minimum), the meaning is very different:


[1,2,3,4,5] .splice(1,2)     //  [2, 3]



splice also mutates the array that calls it. This is not supposed to be a surprise, after all the name splice implies it.


var arr1 = [1,2,3,4,5];
var arr2 = arr1.splice(1, 2);
console.log(arr1);          //
[1, 4, 5]
 console.log(arr1);          // [2,3]