Thursday, October 29, 2009

Force Rails Version When Creating a new RAILS application

I have multiple version of RAILS installed in my machine. I want to create an application that should use the specific rails version, for this we can force app to use the specific rails version while creating the application, as:

$rails _1.2.3_ myapp


This will create myapp application which will use rails 1.2.3

Sunday, August 30, 2009

Deleting persistant gems

I had two versions of rubyforge 1.0.3 and 1.0.4.
whilst running ruby console, its giving error:

can't activate rubyforge (= 1.0.3, runtime), already activated rubyforge-1.0.4

So thought of removing by simply using:

sudo gem uninstall rubyforge --debug


But this surprised me by saying:

ERROR: While executing gem ... (Gem::InstallError)
Unknown gem rubyforge >= 0

After investing many hours I found a solution:

gem list -d

this will give you the details as:

rubyforge (1.0.3)
Authors: Ryan Davis, Eric Hodel, Ara T Howard
Rubyforge: http://rubyforge.org/projects/codeforpeople
Homepage: http://codeforpeople.rubyforge.org/rubyforge/
Installed at: /home/deepti/.gem/ruby/1.8

A script which automates a limited set of rubyforge operations

Just mention the given gem path:
sudo gem uninstall --install-dir /home//.gem/ruby/1.8 rubyforge -v 1.0.4

Pheww!!! Finally its out from my gem list...

Wednesday, August 19, 2009

Using Reliance Netconnect Huawei 1260 on Ubuntu Intrepid 8.10

After struggling a lot, I got my Huawie 1260 working on Ubuntu Intrepid 8.10. May be useful for you.

1- Start Ubuntu ensuring USB modem is not connected and configure the file /etc/wvdial.conf
sudo gedit /etc/wvdial.conf


paste the following lines:


[Dialer Defaults]

Init1 = ATZ

Init2 = ATQ0 V1 E1 S0=0 &C1 &D2 +FCLASS=0

Stupid Mode = 1

Modem Type = USB Modem

ISDN = 0

Phone = #777

New PPPD = yes

Modem = /dev/ttyUSB0

Username = <your ten digit reliance netconnect number>

Password = <your ten digit reliance netconnect number>


2- Plug-in USB modem, your /var/log/messages will say something like:


Aug 16 19:35:57 abc-laptop kernel: [ 111.532151] usb 4-1: new full speed USB device using uhci_hcd and address 2
Aug 16 19:35:57 abc-laptop kernel: [ 111.744188] usb 4-1: configuration #1 chosen from 1 choice
Aug 16 19:35:57 abc-laptop kernel: [ 111.747614] usbserial_generic 4-1:1.0: generic converter detected
Aug 16 19:35:57 abc-laptop kernel: [ 111.748339] usb 4-1: generic converter now attached to ttyUSB0
Aug 16 19:35:57 abc-laptop kernel: [ 111.909454] usbcore: registered new interface driver libusual


3- Run
lsusb
whose output will be like:

us 007 Device 002: ID 04f2:b008 Chicony Electronics Co., Ltd
Bus 007 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 004 Device 002: ID 12d1:142b Huawei Technologies Co., Ltd.
Bus 004 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 003 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 002 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 001 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 006 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 005 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub


4- Add a module for the above listed Huawei device, as:

sudo modprobe usbserial vendor=0x12d1 product=0x142b


make sure the vendor and product ID are the same as listed in the above lsusb output

5- Check the files for USB device:
ls -la /dev/ttyU*


Output will be similar to:

crw-rw---- 1 root uucp 188, 0 2009-08-16 19:36 /dev/ttyUSB0
crw-r--r-- 1 root root 188, 1 2009-08-16 19:34 /dev/ttyUSB1


if no USB0 or USB1 files listed, then try creating it as:
sudo mknod /dev/ttyUSB0 c 188 0
sudo mknod /dev/ttyUSB1 c 188 1


6- now hopefully your are done with the configuration part. just try connecting internet:
sudo wvdial

console will look like:
--> WvDial: Internet dialer version 1.60
--> Cannot get information for serial port.
--> Initializing modem.
--> Sending: ATZ
ATZ
OK
--> Sending: ATQ0 V1 E1 S0=0 &C1 &D2 +FCLASS=0
ATQ0 V1 E1 S0=0 &C1 &D2 +FCLASS=0
OK
--> Modem initialized.
--> Sending: ATDT#777
--> Waiting for carrier.
ATDT#777
CONNECT
--> Carrier detected. Starting PPP immediately.
--> Starting pppd at Thu Aug 20 22:06:00 2009
--> Pid of pppd: 7268
--> Using interface ppp0
--> pppd: [10]�[17]
--> pppd: [10]�[17]


Congrats! your are connected!!! to DISCONNECT use ctrl+c.

Sunday, April 12, 2009

Common Mistakes with Views























DontsDosWhy?

<p>
<%= form.label :title %>
<%= form.text_field :title %>
</p>

<div>
<%= form.label :title %>
<%= form.text_field :title %>
</div>

<p>
<div class="fieldWithErrors">
<label for="deal_title">Title<label>
</div>
<div class="fieldWithErrors">
<input id="deal_title" name="deal[title]" size="30" type="text" value="" />
</div>
</p>
got the problem? There are divs inside the paragraph tags, which is invalid markup


<div>
<%= form.label :location %>
<%= form.select :location, Location::ALL %>
</div>

<div>
<%= form.label :location %>
<%= form.select :location, Location::ALL, :include_blank => true %>
</div>

Required Atribute
If the option is required, prefer that the user gets a validation message saying they need a location instead of accidentally assigning location to Boston when it should be New York.
Optional Attribute
A blank default has no downside for an optional attribute

Styling elements globally
input {
float: left;
clear: both;
}

form#new_user #first_name.text_field input {
float: left;
clear: both;
}

Writing style to remain specific to page elements will save you from having to troubleshoot many strange layout bugs, and also saves you from having to reset these styles for any subsequent elements.

Source

Tuesday, February 17, 2009

Mess up with Many to Many?

There are two ways to build a many-to-many relationship.

The first way uses a has_many association with the :through option and a join model, so there are two stages of associations.

class Assignment < ActiveRecord::Base
belongs_to :programmer # foreign key - programmer_id
belongs_to :project # foreign key - project_id
end
class Programmer < ActiveRecord::Base
has_many :assignments
has_many :projects, :through => :assignments
end
class Project < ActiveRecord::Base
has_many :assignments
has_many :programmers, :through => :assignments
end

For the second way, use has_and_belongs_to_many in both models. This requires a join table that has no corresponding model or primary key.

class Programmer < ActiveRecord::Base
has_and_belongs_to_many :projects # foreign keys in the join table
end
class Project < ActiveRecord::Base
has_and_belongs_to_many :programmers # foreign keys in the join table
end

Choosing which way to build a many-to-many relationship is not always simple. If you need to work with the relationship model as its own entity, use has_many :through. Use has_and_belongs_to_many when working with legacy schemas or when you never work directly with the relationship itself.
Source

Wednesday, November 05, 2008

LIBMYSQL.DLL Not Found

While starting mysql server if you are getting error like:
LIBMYSQL.DLL Not found, the u need to check the followings-

1- Check whether you've mysql gem.(Though you don't need to install it as it is already included in Instant Rails)

2- Check whether the file libmysql.dll is present in /ruby/bin, if not Download it from here, and paste it to ruby/bin.

Hopefully it will work!

Thursday, June 12, 2008

Send Email with Attachment using Action Mailer

As i love to keep the things simple.. :) Its very easy to use ActionMailer.

1. Paste the following code in to cofig/environments/development.rb-
config.action_mailer.raise_delivery_errors = true
ActionMailer::Base.delivery_method = :smtp#FOR UNIT/FUNCTIONAL TESTING THIS'LL BE :test
ActionMailer::Base.server_settings = {
:address => <replace with your SMTP server>,
:port => <your SMTP server's port>,
:domain => <your domain>,
:authentication => :login ,
:user_name => <set if your SMTP server requires authentication>,
:password => <set if your SMTP server requires authentication>
}
ActionMailer::Base.perform_deliveries = true
ActionMailer::Base.raise_delivery_errors = true
ActionMailer::Base.default_charset = "utf-8"



2. Generate Model class which is derived from ActionMailer::Base. From command prompt of your project directory issue the command-

ruby script/generate mailer <MyMailSender> <send_mail_method>


this'll generate the Model named as MyMailSender which will have method name as send_mail_method.

this model's method have all the data which will be used by its view. view is nothing but the mail which the erceipents are going to receive. at this time Class will look like-

class MyMailSender < ActionMailer::Base

def send_mail_method(sent_at = Time.now)
@subject = 'MyMailSender#sendMail'
@body = {}
@recipients = 'xyz@xyz.com'
@bcc = ''
@from = 'abc@abc.com'
@sent_on = sent_at
@headers = {}
end

if u want to send a file as an attachment then u've to add the following lines to this method-

attachment :content_type => 'application/vnd.ms-excel',
:body =>
File.open("#{RAILS_ROOT}/public/test_sheet.xls","rb") {|f| f.read},
:filename => 'test.xls'

ensure that test_sheet.xls is there in public folder. If u tend to send another type of file like image, text etc. then u've to change the value of :content_type

3. Design a view for your mail.

Now all set to send email, just u've to design a format of email. which will inside-

views/my_mail_sender/send_mail_method.rhtml


4. Call Action mailer's method.

whenever u need to send email call from ur controller, as-

MyMailSender::deliver_send_mail_method

thing to remember is to prefix ur method with deliver_ whenever u call ur method.