Thursday, December 16, 2010

Turning AutoComplete Off

Although autocomplete is useful in many cases, there are situations where this does create confusion. To disable autocomplete on a per form basis, follow this.

For rails one needs to do this this

Wednesday, December 15, 2010

One model, one database.

I've reached a situation where I have to connect to an existing database (primarily read) and a new database for writes. This is very useful.

Tuesday, December 14, 2010

Passing local variables to partials

Corrent way

< %= render :partial => 'widgets/some_partial', :locals => {:foo => 'bar'} %>


Wont work

< %= render 'widgets/some_partial', :locals => {:foo => 'bar'} %>

To read

http://www.bcl.hamilton.ie/~nmh/t3x.org/zzz/

Returning Javascript along with HTML from Rails Views

One way is as follows -

< %= javascript_tag :defer => 'defer' do -%>
alert('this is a view.html.erb')
< % end -%>



Another way is as follows
This goes in the application.html.erb

< script type="text/javascript">
$(document).ready(function() {
<%= yield :javascript %>
});

< /script>


And in the view we use content_for

< %= content_for :javascript do -%>
alert($)
< % end -%>

Rails & Oracle

The following gems are required
gem 'ruby-oci8', '2.0.4'
gem 'activerecord-oracle_enhanced-adapter', '1.3.1'

Installation instructions are here.. For Leapord there are additional instructions which are here.

Using sqlplus along with instant client is here

Mac download for instant client is here.

Monday, December 13, 2010

Rails : Concepts

Fat Models, Skinny Controllers is here.

Introduction to the MVC architecture introduction is here.

Alternatives to execute jobs in background are here.

Anonymous scopes are here

Javascript visualization are here.

Safe buffers are here.

A nice and short example of JSON, AJAX and RAILS is here.

jQuery chart plugins are here.

UJS tutorial is here.

A nice Rails plugin for the highcharts javascript framework is here.

Charts

Sparkline
Primer
Protovis

UJS - Rails 3

A good start is here.

This is required to replace prototype.js by jquery.js

Active Record Queries - Rails 3

At Railscasts

Timezone

For rails to show distance_of_time_in_words, I had to setup the following in the application.rb file

config.time_zone = 'Mumbai'
config.active_record.default_timezone = :local


This made sense as mysql was setup to read the timezone from my laptop, which was Mumbai.

More information here.

Friday, December 10, 2010

Running rails in production environment

To run a rails app in the production environment, we do the following

rails s -e production


In production environments, all static assets are served by apache/nginx and rails is disabled to serve these assets, since in most cases, we wont have the entire gamut while just switching environments, we need to change config/environments/production.rb.


config.serve_static_assets = false; <-- this should be changed to true

Thursday, December 9, 2010

Git Basics

There are tons of tutorials out there, I am posting the snippets that i found very useful from this excellent rails tutorial.


  1. Global configurations (these can be seen via the git config --list)
    1. git config --global user.name "Your Name"
    2. git config --global user.email youremail@example.com
    3. git config --global alias.co checkout
    4. git config --global core.editor "mate -w"
  2. Initializing a new project / first time repository setup
    1. git init
  3. Enhanced .gitignore file

    .bundle
    db/*.sqlite3*
    log/*.log
    *.log
    tmp/**/*
    tmp/*
    doc/api
    doc/app
    *.swp
    *~
    .DS_Store

  4. Adding files (this is a recursive command), committing with a message (messages are mandatory), and seeing the log, and the various branches
    1. git add .
    2. git commit -m 'message'
    3. git log
    4. git branch
  5. To see pending commits, checkout, checkout to an existing branch, checkout to a new branch, checkin, merge a branch to the current branch, delete a branch
    1. git status
    2. git checkout
    3. git checkout 'existing branch name'
    4. git checkout -b 'new branch name'
    5. git checkin -a -m'message'
    6. git merge 'branch name'
    7. git branch -d 'branch name'
  6. Adding to github (these commands are displayed when I create a new repo on github)
    1. git remote add origin git@github.com:akil-rails/opac_app.git
    2. git push origin master

Wednesday, December 8, 2010

mysql data export and import

To export a database dump into a sql file use (the structures you'll normally create by a db:migrate or an equivalent script.)

mysqldump -u username -ppassword –no-create-info database_name > dump.sql


and to load it back


mysql -u username -ppassword database_name < dump.sql

Installing passenger

Passenger requires ruby compiled with openssl. For this, after ruby is built, the following needs to be done

# go the ruby source folder, and within that to ext/openssl
cd /usr/local/src/ruby-1.8.7-p22/ext/openssl

# build
sudo ruby extconf.rb
make
make install


After this installing passenger is very simple as outlined here.


gem install passenger
passenger-install-nginx-module

Installing nginx

This is pretty easy as explained here.


# first add fedora community rpm provider
rpm -Uvh http://download.fedora.redhat.com/pub/epel/5/x86_64/epel-release-5-4.noarch.rpm'

# install
yum install nginx

# start
service nginx start

# stop
service nginx stop

Installing ruby

Something as straight forward as the following works

yum install ruby


This installed version 1.8.5 for me instead of the latest 1.9 version; so I have to rollback and build this from source

To find out the rpm's installed,

rpm -qa | egrep '(ruby)|(irb)'


To uninstall

rpm -e ruby-libs-1.8.1-7.EL4.2


This gave an error :multiple packages installed. Fortunately for this page, it was easy to figure out why, and the following worked!

rpm -e ruby-libs-1.8.5-5.el5_4.8.i386
rpm -e ruby-libs-1.8.5-5.el5_4.8.x86_64


To install the latest version, I'd have to build this from source, and this link proved useful.


# download the required ruby version
wget ftp://ftp.ruby-lang.org:21//pub/ruby/1.9/ruby-1.9.2-p0.tar.gz
tar xvfz ruby-1.9.2-p0.tar.gz
cd ruby-1.9.2-p0
./configure --prefix=/usr (the default prefix is /usr/local)
make
make test
make install


Rubygems is included as part of ruby 1.9.2 installation, so no need to build this from source.

However, the gem list command failed as follows

[root@jbserver4 lib]# gem list
ERROR: Loading command: list (LoadError)
no such file to load -- zlib
ERROR: While executing gem ... (NameError)
uninitialized constant Gem::Commands::ListCommand


This got resolved thanks to the information provided here. What was required was the installation of zlib-devel, which is simply done as follows


yum install zlib-devel


The ruby build needs to be done again.

This time around I gave a little more attention to the build commands being fired, I found a couple of 'missing' libraries as follows

..
..
make[1]: Entering directory `/disk1/downloads/ruby-1.9.2-p0/ext/fiber'
gcc -shared -o ../../.ext/x86_64-linux/fiber.so fiber.o -L. -L../.. -L. -rdynamic -Wl,-export-dynamic -lpthread -lrt -ldl -lcrypt -lm -lc
make[1]: Leaving directory `/disk1/downloads/ruby-1.9.2-p0/ext/fiber'
compiling fiddle
ffi.h is missing. Please install libffi.
compiling gdbm
..
..
compiling openssl
compiling psych
yaml.h is missing. Please install libyaml.
compiling pty
..
..
..
check struct members..
check libraries....
Use ActiveTcl libraries (if available).
Search tclConfig.sh and tkConfig.sh...................
Fail to find [tclConfig.sh, tkConfig.sh]
Use X11 libraries.
Warning:: cannot find X11 library. tcltklib will not be compiled (tcltklib is disabled on your Ruby == Ruby/Tk will not work). Please check configure options. If your Tcl/Tk don't require X11, please try --without-X11.
Can't find X11 libraries. So, can't make tcltklib.so which is required by Ruby/Tk.
compiling tk/tkutil
..
..
..



Inspite of these, the installation seems to work...so maybe i'll revisit these if I have a problem...

The one thing that didnt work was 'rails console'. This post helped in resolving the issue.


yum install readline readline-devel
cd ruby-source/ext/readline
ruby extconf.rb
make
make install

Resetting root password for mysql

As luck would have it, I messed up and lost the password on day 0! Found a handy tip here on how to reset it.


# stop running mysql daemon
pkill mysqld

# start mysql without security info loaded
mysqld_safe --skip-grant-tables &

# this allows login without any password
mysql -u root mysql

# update passwords
UPDATE user SET password=PASSWORD("ualue=42") WHERE user="root";
FLUSH PRIVILEGES;

# stop running mysql daemon
pkill mysqld

# do a normal start
/etc/init.d/mysql start

Installing MySql

Googled around a bit, and found this post.. What worked for me was

# yum install mysql-server mysql

# chkconfig mysqld on
# /etc/init.d/mysqld start


Starting up the server gives the next steps to be done





To start mysqld at boot time you have to copy
support-files/mysql.server to the right place for your system

PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !
To do so, start the server, then issue the following commands:
/usr/bin/mysqladmin -u root password 'new-password'
/usr/bin/mysqladmin -u root -h jbserver4.interactivedns.com password 'new-password'

Alternatively you can run:
/usr/bin/mysql_secure_installation

which will also give you the option of removing the test
databases and anonymous user created by default. This is
strongly recommended for production servers.

See the manual for more instructions.

You can start the MySQL daemon with:
cd /usr ; /usr/bin/mysqld_safe &

You can test the MySQL daemon with mysql-test-run.pl
cd mysql-test ; perl mysql-test-run.pl

First steps

Here's how one finds out the version and distribution :

cd /proc
cat version


The result that I get

Linux version 2.6.18-194.11.3.el5 (mockbuild@x86-009.build.bos.redhat.com) (gcc version 4.1.2 20080704 (Red Hat 4.1.2-48)) #1 SMP Mon Aug 23 15:51:38 EDT 2010


Creating a new user and setting the password is as follows, from here

useradd 'username'
passwd 'username'

Linux

Its been ages since I got on to do some work with linux, a long stint in a large company does that I guess; it sure did to me.

Will post out how I got around to relearning it all...These are going to be more like my own study notes, would have done this on a more appropriate platform, but I guess there aren't any available..