Saturday, September 8, 2012

Rails behaviour of form_for serving from a custom ruby class, (rails2 and rails3 differentiates)

Hi,

It has been so long writing a post and for a change this time it not my laziness but had been really occupied from office front.

Anyhow, here is one strange thing that I found with "form_for" in Rails2 and Rails3.
In Rails2, if we have a class in ruby (not derived from Activerecord::Base) some thing like the one below..

class SomeRubyClass

  attr_accessor :file_name, :file_data, :class_name, :error

  def initialize(file_name, file_data, class_name, error)
    @file_name = file_name
    @file_data = file_data
    @class_name = class_name
    @error = error
  end

  def self.first
    #Some code, which will give an object
  end
end


We can use form_for with it something like this

class SomeController < ApplicationController
  def index
    @some_ruby_class = SomeRubyClass.new("file_name", "file_data", "class_name", "error")
  end

  def create
    @some_ruby_class = SomeRubyClass.first
    @some_ruby_class.class_name = params[:some_ruby_class][:class_name]
    @some_ruby_class.file_name = params[:some_ruby_class][:file_name]
  
    #some more code
    redirect_to :back
  end
end

<% form_for @some_ruby_class, :url => {:action => :create} do |f| %>
    <%= f.text_field :class_name %>
    <%= f.text_field :file_name %>
    <%= f.submit 'Update' %> 
<% end %>

and we can do manipulation as per the need.
But in Rails3 this thing is not supported with form_for
It gives exception like...
undefined method `model_name' for SomeRubyClass:Class' 
and to make it work we have to add these lines 
1. extend ActiveModel::Naming
2. add to_key instance method which must return an array

so added it and result is something like this
class SomeRubyClass
  extend ActiveModel::Naming

  attr_accessor :file_name, :file_data, :class_name, :error

  def initialize(file_name, file_data, class_name, error)
    @file_name = file_name
    @file_data = file_data
    @class_name = class_name
    @error = error
  end

  def self.first
    #Some code, which will give an object
  end

  def to_key
    ["some_key", "some_value"]
  end

end

and it works! Now question arises, Rails3 is imposing me to use ActiveModel (which was initially meant to provide ActiveRecord base type facilities like validation etc.) for simple things like form_for why?? am I missing something here.

Sunday, April 29, 2012

Not able to see skype in the top notification bar in ubuntu

This is something i saw with skype installtion on ubuntu 12.04
Even though skype was running fine, icon in the notification bar was missing (i know it happens sometimes) but it is pretty annoying to bring active skype window in this case. Googled a bit for an answer and found a very sweet and simple answer :)

open a terminal and execute following command
  • gsettings set com.canonical.Unity.Panel systray-whitelist "['all']"
  • unity --reset
and in Ubuntu 12.10 one needs to run
  • unity --reset-icons
And now it is available there after reset

Tuesday, April 24, 2012

AJAX file-upload capability to remote forms in Rails 3. but HOW??

Well this was something strange that I was hitting the other time when was doing the file upload in rails 3.1+ from an AJAX form

I was trying to upload a file in a AJAX form (driven by jQuery) and every time i click submit it used to be normal synchronous request while any submit without file field was going correct as an AJAX request.

Googled it out but not much of help in the beginning but later via some articles came to know people using iframe for file upload on ajax (in rails) how and why this happened I don't know but yes it is there.

I was expected to come out of the situation and complete the work in time, and gem Remotipart came to help...

Will debug it later and currently I don't know much of, how it enables this functionality but it worked flawless even without changing a single line of code in my remote form with file upload.

Just added it to gem file and updated application.js (as is mentioned in the documentation of gem) and it just worked!! 
I know it sounds innocently non-develop-ish, pardon me for that.. will debug more and update soon but for the time being hit the URL here and get yourself out of this situation http://os.alfajango.com/remotipart/

In the mean time anybody want to share any information on this.. most welcome please comment and spread the knowledge :)

Monday, March 5, 2012

Install mysql gem using bundler in ubuntu 11.10 64 bit

Well just tried doing a mysql gem install using bundler (Gemfile) but got an error like this
Gem::Installer::ExtensionBuildError: ERROR: Failed to build gem native extension.

        /home/praveen/.rvm/rubies/ruby-1.9.2-p290/bin/ruby extconf.rb
checking for mysql_query() in -lmysqlclient... no
checking for main() in -lm... yes
checking for mysql_query() in -lmysqlclient... no
and much more
and i forgot to install  libmysqlclient-dev :) how careless! so did
sudo apt-get install libmysqlclient-dev

and redid the bundle install and wohooo it worked!

Sunday, February 26, 2012

Honda Civic engine vibration fix

Hello people, 
Long time.. no post?? its all because of my laziness :D but this one is not about computer but about my car.
It is going on winters here in Delhi and my car started showing vibrations during cold start and it was really bad and vigorous while doing reverse and it use to go away after say initial 1-2 km run.
Let me tell you about my car, its 2007 make Honda civic (India) an automatic. 
Initially i thought that it will go away with time, might be due to something temporary but it was getting really annoying every morning and was not able to stand it any more so decided to get it fixed. Got to two car workshops(other than Honda authorized workshop) and they were not able to identify the issue and some said it might be the O2 sensor or the engine mount. 
Honda guys said it is the mount but from the naked eye there was no damage to engine mount and the cost they said was something around 10.5 K oops. This much of cost for car vibration it was something that i thought is really expensive and being a delhite, need help now as the cost was not at all satisfying :) So i went to Palika bazar car market (opposite to the Hayat) and from there got the engine mount replaced with just a self trust that the vibration is only because of engine mount. 
New engine mount cost me around Inr 3600 and Inr 300 as the labor charges and also got the front disk pads replaced(as it was due) which cost me 1500 more :) 
And my car is back :) 
Vibration completely gone and more effective braking now. From the service guy i cam to know that Engine mount was not damaged but got pressed (seems Honda civic engine is bit heavy for the designated mount and it is a common problem with civics in India). Please refer to pictures below. 

Engine is touching the mount completely(mount hydraulics not working, needs replacement)

New engine mount replaced (and now the hydraulics will eat the vibration coming from engine) and no more vibrations

Hope this gets as help to somebody required someday.

Honda Civic & A/C problems.

Hello Friends, Got a post again on to Honda Civic (The good old favorite commuter of mine). This car has been doing great except for so...