Showing posts with label libxml ruby. Show all posts
Showing posts with label libxml ruby. Show all posts

Thursday, August 25, 2011

Patch to allow link_to with post method

Hello guys,
     If you want to pass additional parameters with post method in link_to than you have to add patch in url_helper of actionpack module of rails.

Here is the simple patch to allow post method with additional parameters in link_to.

Go to the actionpack folder of rails for eg we have rails 2.3.11.

nano /usr/lib/ruby/gems/1.8/gems/actionpack-2.3.11/lib/action_view/helpers/url_helper.rb

Now just replace below lines

line #562
method, href = html_options.delete("method"), html_options['href']
to 
method, href, values = html_options.delete("method"), html_options['href'], html_options.delete("values")
line #570
"if (#{confirm_javascript_function(confirm)}) 
{ #{method_javascript_function(method, url, href)} };return false;"
to
"if (#{confirm_javascript_function(confirm)}) 
{ #{method_javascript_function(method, values, url, href)} };return false;"

line #574 
"#{method_javascript_function(method, url, href)}return false;"
to
"#{method_javascript_function(method, values, url, href)}return false;"

line #590
def method_javascript_function(method, url = '', href = nil)
to
def method_javascript_function(method, values, url = '', href = nil)

and add below code after line #595
 
  if values.is_a?(Hash) && (method == :post || method == :put) 
    values.each do |name,value| 
      submit_function << "var formElement = document.createElement('input'); " 
      submit_function << "formElement.name = '#{name}'; " 
      submit_function << "formElement.type = 'text'; " 
      submit_function << "formElement.value = '#{value}'; " 
      submit_function << "f.appendChild(formElement); " 
    end 
  end 

Finally now you can access the link_to with post action and query parameters.
<%= link_to "Test to click on",
  {:controller => "users", :action => "dummy_action"}, :method => :post, 
   :values => {:arg1 => "Argument1", :arg2 => "Argument 2"}, 
   :class => 'css_class' %>

Hope this post helps you to overcome the problem of link_to during post method.
If you have any suggestion then post comment. 

Friday, August 19, 2011

Faster XML Parser in ruby on rails

Hello Guys,

Last week i was trying to  parse xml using different xml parser like Hpricot. But when we have large amount of data than segment fault occurs. So i moved to better and faster xml parser called libxml-ruby.

There is simple steps to parse large xml using libxml-ruby. First of all you need to install libxml-ruby by

  gem install libxml-ruby

Lets, we have sample xml

sample.xml
xml = %{
  <users>
    <user>
      <name>Priyanka Pathak</name>
      <mark subject=”biology”> 80 </mark>
    </user>
    <user>
      <name>Rahul Pathak</name>
      <mark subject=”biology”> 85 </mark>
    </user>
  </users>
}

Now create method to parse xml

require 'rubygems'
require 'libxml'
require 'benchmark'

def parse_xml
   Benchmark.bmbm do |r|
      r.report("Process XML"){
        parser = LibXML::XML::Parser.file('sample.xml',:encoding => XML::Encoding::UTF_8)
        doc, collect_data = parser.parse, []
        doc.find('//users/user').each do |e|
           data = {}
           data['name'] = e.find('name').first.content
           mark = e.find('mark').first
           data['mark'] = {:subject => mark.attributes.first.value , :value => mark.content}
           collect_data << data     
        end

        puts "collect data: " + collect_data.inspect
     }
   end
end
Benchmark shows the time required during xml parsing and as per my experience it's faster than other xml parser.

For more information about libxml follow http://libxml.rubyforge.org/rdoc/
Hope this post will help you.