Tuesday, August 10, 2010

search gem from repository

If you want to find a gem in the repository then you can do it by following way

gem1.8 list -r | grep gem_name

Wednesday, June 30, 2010

Unpacked gem authlogic-2.1.3 in vendor/gems has no specification file. Run 'rake gems:refresh_specs' to fix this.

You can solve this issue by following way

> cd vendor/gems/authlogic-2.1.3
> gem specification authlogic > .specification

cheers,

Thursday, June 24, 2010

Get Session Data Available in Models - Ruby on Rails

Sometimes we really need session variables inside model. But as the rails framework defines (MVC) pattern. 

This pattern separates the context of the Web Application (Controller,View) from the Model. Model contains business logic of the web application. The Controller handles the interactions between the View and the Model. 

So you can not access sessions directly in your model. You can crack it by following way.

Create one module 
module Utility
  def current_user
    Thread.current[:user]
  end
 
  def self.current_user=(user)
    Thread.current[:user] = user
  end
end
 
Add following in application controller
 
class ApplicationController < ActionController::Base
  include Utility
  before_filter :set_user_session
 
  protected
  def set_user_session
    Utility.current_user = session[:user]
  end
end
 
And now get it in your model
 
class Account < ActiveRecord::Base
  include Utility
 
  def before_create
    unless allowed?(current_user)
      return false 
    end
  end
end
 
 

Tuesday, May 4, 2010

fetch all controllers and actions of the application

dirs = Dir.new("#{RAILS_ROOT}/app/controllers").entries
controller_hash = {}
dirs.each do |controller|
  if controller =~ /_controller/
    path = RAILS_ROOT + '/app/controllers/' + controller
    con = File.read(path).split('def ')
    con.delete_at(0)
    my_hash = Hash.new
    actions = con.collect{|c| c.gsub("\r","~").gsub("\n","~").split("~")[0].strip}
    controller_name = controller.gsub('.rb','')
    my_hash = {controller_name.to_s => actions}
    controller_hash = my_hash.merge(controller_hash)
  end
end
puts controller_hash.inspect

Friday, February 5, 2010

How to generate an SSH key in Linux?

You can generate a key in Linux using the ssh-keygen command.
You can run it in command line. You will be asked for a file in which the key should be saved to and for a passphrase (password) for the key:
This command will generate id_rsa public and private keys.

If you need to generate id_dsa keys then you need to run ssh-keygen -t dsa

Thursday, January 28, 2010

cannot open shared object file: No such file or directory

I got below error while i tried to run my application in fedora

libMagickCore.so.2: cannot open shared object file: No such file or directory – /usr/lib/ruby/gems/1.8/gems/rmagick-2.8.0/lib/RMagick2.so

To get solved of this error i executed
ldconfig /usr/local/lib

Contact Me for any help regarding rails/nodejs/php/mysql

Name

Email *

Message *