petak, 19. kolovoza 2011.

Ruby on Rails 3, scaffold_controller not works, solution with Niftygenerators

As I tried to use scaffold_controller on Ruby On Rails 3, to create controller and view, and generated code was not working, I tried niftygenerators and they work.
This is how you can use them:
Edit you Gemfile (in root of your project) and add

gem "nifty-generators", :group => :development

after that, you can run
bundle install
to install gem

After that, you can do
rails g nifty:scaffold ModelName
and after that we must create layout:
rails g nifty:layout
That will successfully create controller and view from your ActiveRecord.

srijeda, 17. kolovoza 2011.

Ruby on Rails: stack level too deep

If you install your Ruby on Rails on Debian or Ubuntu, and dont use apt-get to install it, you may stumble upon a problem with stack level too deep


rake
/usr/local/rvm/gems/ruby-1.9.2-p290/gems/rake-0.9.2/lib/rake/version.rb:4: warning: already initialized constant MAJOR
/usr/local/rvm/gems/ruby-1.9.2-p290/gems/rake-0.9.2/lib/rake/version.rb:5: warning: already initialized constant MINOR
/usr/local/rvm/gems/ruby-1.9.2-p290/gems/rake-0.9.2/lib/rake/version.rb:6: warning: already initialized constant BUILD
/usr/local/rvm/gems/ruby-1.9.2-p290/gems/rake-0.9.2/lib/rake/version.rb:3: warning: already initialized constant NUMBERS
/usr/local/rvm/gems/ruby-1.9.2-p290/gems/rake-0.9.2/lib/rake/version.rb:9: warning: already initialized constant VERSION
/usr/local/rvm/gems/ruby-1.9.2-p290/gems/rake-0.9.2/lib/rake.rb:26: warning: already initialized constant RAKEVERSION
/usr/local/rvm/gems/ruby-1.9.2-p290/gems/rake-0.9.2/lib/rake/early_time.rb:17: warning: already initialized constant EARLY
/usr/local/rvm/gems/ruby-1.9.2-p290/gems/rake-0.9.2/lib/rake/alt_system.rb:32: warning: already initialized constant WINDOWS
/usr/local/rvm/gems/ruby-1.9.2-p290/gems/rake-0.9.2/lib/rake/application.rb:28: warning: already initialized constant DEFAULT_RAKEFILES
WARNING: Possible conflict with Rake extension: String#ext already exists
WARNING: Possible conflict with Rake extension: String#pathmap already exists
/usr/local/rvm/gems/ruby-1.9.2-p290/gems/rake-0.9.2/lib/rake/task_arguments.rb:73: warning: already initialized constant EMPTY_TASK_ARGS
/usr/local/rvm/gems/ruby-1.9.2-p290/gems/rake-0.9.2/lib/rake/invocation_chain.rb:49: warning: already initialized constant EMPTY
/usr/local/rvm/gems/ruby-1.9.2-p290/gems/rake-0.9.2/lib/rake/file_utils.rb:10: warning: already initialized constant RUBY
/usr/local/rvm/gems/ruby-1.9.2-p290/gems/rake-0.9.2/lib/rake/file_utils.rb:84: warning: already initialized constant LN_SUPPORTED
/usr/local/rvm/gems/ruby-1.9.2-p290/gems/rake-0.9.2/lib/rake/dsl_definition.rb:143: warning: already initialized constant Commands
/usr/local/rvm/gems/ruby-1.9.2-p290/gems/rake-0.9.2/lib/rake/file_list.rb:44: warning: already initialized constant ARRAY_METHODS
/usr/local/rvm/gems/ruby-1.9.2-p290/gems/rake-0.9.2/lib/rake/file_list.rb:47: warning: already initialized constant MUST_DEFINE
/usr/local/rvm/gems/ruby-1.9.2-p290/gems/rake-0.9.2/lib/rake/file_list.rb:51: warning: already initialized constant MUST_NOT_DEFINE
/usr/local/rvm/gems/ruby-1.9.2-p290/gems/rake-0.9.2/lib/rake/file_list.rb:55: warning: already initialized constant SPECIAL_RETURN
/usr/local/rvm/gems/ruby-1.9.2-p290/gems/rake-0.9.2/lib/rake/file_list.rb:61: warning: already initialized constant DELEGATING_METHODS
/usr/local/rvm/gems/ruby-1.9.2-p290/gems/rake-0.9.2/lib/rake/file_list.rb:364: warning: already initialized constant DEFAULT_IGNORE_PATTERNS
/usr/local/rvm/gems/ruby-1.9.2-p290/gems/rake-0.9.2/lib/rake/file_list.rb:370: warning: already initialized constant DEFAULT_IGNORE_PROCS
/usr/local/rvm/gems/ruby-1.9.2-p290/gems/rake-0.9.2/lib/rake.rb:64: warning: already initialized constant FileList
/usr/local/rvm/gems/ruby-1.9.2-p290/gems/rake-0.9.2/lib/rake.rb:65: warning: already initialized constant RakeFileUtils
rake aborted!
stack level too deep


You can resolve this by typing bundle exec in front of rake like this:


bundle exec rake db:migrate



utorak, 16. kolovoza 2011.

Linux: size of directory

If you need to see what is the size of every sub directory you can use this:

du -m --max-depth=1 |sort -n -k1

nedjelja, 31. srpnja 2011.

Best internet prepaid data plan if you go to Croatia (2011)

If you are planning vacation in Croatia, you will probably need some internet access. Maybe city where you are going have some free internet hotspots, but you never know, until you get there. So other solution is prepaid internet stick with some data plan. Best data plan is Bonbon internet data SIM (you can use daily package) where you can get 300 mb for 3 kn (about 0.45 Euro). Problem with this package is that you still cannot buy it in store, and you can only buy it if you order it by mail ( https://www.bonbon.com.hr/ponuda/internet_sim ). Second best solution is from T-mobile and Simpa internet. You can buy Simpa internet start paket (SIM only) for 20 kn (about 3 Euros). If you register you will get 500 mb of data. If you also need USB modem, you can buy bundle for 198 kn ( 30 Euros). When you spend first 500 mb, you can buy extra packages, depending what you need.http://www.visitingcroatia.info/internet_tarrifs.asp

petak, 15. srpnja 2011.

Wordpress jQuery hell

If you were caught in javascript hell, here is a quick fix for you(found here):

you need to add <?php wp_enqueue_script("jquery"); ?> before <?php wp_head(); ?> so it would look like this:



<?php wp_enqueue_script("jquery"); ?>

<?php wp_head(); ?>


also, you may need to rewrite some javascript functions, just like this:


var $j = jQuery.noConflict();

$j(function(){

    $j("#sidebar li a").hover(function(){
     $j(this).stop().animate({
      paddingLeft: "20px&"
     }, 400);
    }, function() {
     $j(this).stop().animate({
      paddingLeft: 0
     }, 400);
    });

});

nedjelja, 10. srpnja 2011.

Intuitive way to insert data into mysql

So instead of standard:

INSERT INTO sometable
     ( field1, field2, field3 )
VALUES
     ( 'value1', 'value2', 'value3' )

you can do this:

INSERT INTO sometable
   SET field1 = 'value1',
       field2 = 'value2',
       field3 = 'value3'

Securing your mysql queries from $_POST or $_GET injections

Easiest way is to do this:


foreach ($_POST as $k=>$v) {
    $_POST[$k] = mysql_real_escape_string($v);
}

ponedjeljak, 4. srpnja 2011.

Best linux distribution for Asus EEE PC (900)

I had my Asus EEE pc 900 lying around and almost never used it, because it was slow while running Windows XP. So I decided to try some linux distribution. I found #! ( http://crunchbanglinux.org/ )and I was a bit sceptical at first. I installed it on my 4 GB SD card. I installed it from USB stick which I created with unetbootin ( http://unetbootin.sourceforge.net/ ).
Installation passed without a problem and all hardware was detected (Wow!!! :)
Because it is based on debian, apt-get work also.
I used liquorix kernel that setup offers to install.
My web n walk stick for broadband internet works out of the box :)
Wine works perfectly ( I needed for Mikrotik managing program winbox.exe , even mac detect devices (wow) )
Only little problem I found that default speaker volume was muted, so you have to bring up the volume in mixer.
CRUNCHBANG comes with Chromium browser, and flash player. Youtbe videos are working without a glitch on my 900 mhz Asus EEE 900.
All in all, if you need a distribution for your asus eee, go with CRUNCHBANG :)

Debian: How to send and read SMS with your web n walk stick

If you have T-mobile web n walk stick, witch is Qualcomm 3g CDMA model GIO255 and you want to send sms this is the way:

first check is the device detected:
:~$ dmesg
....
usb 3-1: new full speed USB device using uhci_hcd and address 6
usb 3-1: New USB device found, idVendor=0af0, idProduct=6971
usb 3-1: New USB device strings: Mfr=1, Product=2, SerialNumber=3
usb 3-1: Product: Globetrotter HSDPA Modem
usb 3-1: Manufacturer: Option N.V.
usb 3-1: SerialNumber: Serial Number
usb-storage: probe of 3-1:1.0 failed with error -5
hso 3-1:1.0: Not our interface
usb 3-1: USB disconnect, address 6
usb 3-1: new full speed USB device using uhci_hcd and address 7
usb 3-1: New USB device found, idVendor=0af0, idProduct=6971
usb 3-1: New USB device strings: Mfr=1, Product=2, SerialNumber=4
usb 3-1: Product: Globetrotter HSDPA Modem
usb 3-1: Manufacturer: Option N.V.
usb 3-1: SerialNumber: Serial Number
hso0: Disabled Privacy Extensions



then you should look in /dev/ for device ttyHS*

:~$ ls /dev/ttyHS*
/dev/ttyHS0  /dev/ttyHS1  /dev/ttyHS2

For sending sms you will be using /dev/ttyHS0.
Now you can install and run gammu or wammu

apt-get install gammu

or

apt-get install wammu

You can reconfigure gammu with sudo gammu-config

So as port you should use /dev/ttyHS0 , then save and that is it.
You can test it with 

sudo gammu getallsms


petak, 29. travnja 2011.

Connecting to your virt-viewer from Windows

First, you have to install Xming, you can dowload it from here.
After install start it with:


>"C:\Program Files\Xming\Xming.exe" -ac -multiwindow -clipboard    or on a 64-bit system...
>"C:\Program Files (x86)\Xming\Xming.exe" -ac -multiwindow -clipboard
  Notes: using -ac is TOTALLY UNSAFE
now, connect to your linux(ubuntu, debian...) machine with ssh.
After that in shell enter:

DISPLAY=your.windows.ip.address:0.0

export DISPLAY

Dont forget to change your.windows.ip.address to your your windows ip address :)


after that you can start any Xwindow application, and for your virtual viewer you can write:


virt-viewer -c qemu:///system your_virtual_machine &

Linux software raid and S.M.A.R.T monitoring

If you want to monitor SMART settings on your debian/ubuntu system you will have to install

apt-get install smartmontools

If your SMART is disabled, use this command to test them

smartctl -s on -t short -d ata /dev/sda
after that you can check status with

smartctl --all /dev/sdb


To check status of your raid you can do

mdadm --detail /dev/md0 

ponedjeljak, 14. ožujka 2011.

PHP: verifying username and password with LDAP

here is a simple example how you can verify username and password with ldap


        $ldaphost = "aai.host.com";
        $ldapport = 389;
        $ldapconfig['basedn'] = 'dc=host,dc=com';

        $ds = ldap_connect($ldaphost, $ldapport)
        or die("Could not connect to $ldaphost");
        if ($ds)
        {
                $username = mysql_real_escape_string($_POST["user"]);
                $upasswd = mysql_real_escape_string($_POST["pass"]);
$dn="uid=".$username.",".$ldapconfig['basedn'];
                $ldapbind = @ldap_bind($ds, $dn, $upasswd);
                if ($ldapbind) {
$ok=true;
echo "Password OK!<br>";
} else {
echo "Password invalid!<br>";
$ok=false;
exit();
}
}


subota, 12. ožujka 2011.

PoE (Power over ethernet) with Mikrotik Routerboards

If you want to connect your RB411, RB433, RB711Hn or any other routerboard that has POE enabled on it ports, and do not want to buy PoE injectors, this is how you can connect it if you have power supply with 12V or any other that is specified for your device.

NOTE! This is only for simple 100 mb PoE, not for IEEE 802.3af devices! They need special electronics that negotiate with 802.3af injectors.

100 MB ethernet uses only 4 wires out of 8 wires that are in standard ethernet cable. There are 4 pairs of wires and they are orange, green, blue and brown. For T-568B Straight-Through Ethernet Cable you use wiring like this:

In 100MB ethernet, blue, blue-white, brown and brown-white are not used, so they can be used to transfer power to device. For routerboards, blue wires are positive and brown wires are negative.

So how do you connect?
You crimp your cable like on picture above (using T-568B standard) and near place you want to connect power adapter you remove the insulation (only the outer white layer that are around all wires) and cut blue and brown wires. Then you remove the insulation from them, but only on ones that are going to your routerboard, and connect blue and blue-white together, and brown and brown-white together. Then you cut connector from your power supply and measure what is positive and what is negative wire. You can do that by using voltmeter(place range to 20V or more DC-direct current, black wire to ground, red wire to voltage measurement) and placing one wire on black and another on red wire. If your voltmeter show +12 or any other voltage, then connect wire that was on black wire to brown and from red wire to blue. On the other hand, if your voltmeter shown -12V then you should do the opposite: wire that was connected to red connect to brown wires, and wire that was connected to black connect to blue wires.

Here is how I connected to my computer. I used +12V from my computer as power supply from molex connector:

Here you see orange and green wires go to connector and other 4 wires go to molex connector connected to my power supply.Other end of UTP is connected to routerboard 411, crimped as shown in first picture.

Connecting optical sensors to microcontroller

My task was to make instrumentation for measuring speed of reaction and speed for crossing two points in space. I used 2 optical sensors that I had laying around and connected them through relays to microcontroller. As microcontroller I used Atmel ATMEGA16 and some development kit i bought on Ebay. As output I used RS232 to send data.

Biggest problem was with sensors that I have no specifications and could not find them on internet. There are 3 wires, one black, one blue and one brown. On sensor is only specification that black and blue are power wires (I connected them to 12V DC) and brown is output. So I decided to use relay.

Fig 1: Atmega16 connected with optical sensor through relay

Note: maybe for your application you will need to use pull-up resistor.I think you can use 10K and connect it to VCC(5V) of Atmega and to that pin.

petak, 11. ožujka 2011.

How to fix “STOP: c000021a (Fatal System Error)” Error!” in windows XP

If you have blue screen and this error:

STOP: c000021a {Fatal System Error}
The Windows Logon Process system process terminated unexpectedly with a status of 0xc06d007e (0×00000000 0×00000000).
The system has been shut down.



when you boot your Windows xp, here is a solution how to resolve the error.

0) Copy c:\windows\system32\winlogon.exe from working Windows XP (use Windows xp with same service pack) to your usb drive
1) Boot your computer with some live cd (I used Windows XP Live!)
2) make copy or rename your winlogon.exe file (I renamed it to winlogon.exeold)
3) copy winlogon.exe from usb drive to your \windows\system32\ directory
4) boot windows

if everything is ok, then that is it, however, in my case computer booted, but explorer was not working so used system restore.

5) when Windows loads, go to task manager (with ctrl+alt+delete or ctrl+shift+esc) and run System restore by going to File, Run, and c:\windows\system32\restore\rstrui.exe

Then restore to some old restore point...and thats it :)
p.s. maybe you will need to run

sfc.exe /scannow
to check your system files.

PHP, mysql, html, css and coloring every other row

I stumbled upon interesting problem, where I wanted to color every second column with specific color.
Problem was that I had css defined, so the php code did not work correctly:

if (($i % 2)==0) { echo '<tr BGCOLOR="#99CCFF" >'; } else { echo '<tr BGCOLOR="#FFFFFF" >'; };


so I changed it to something like this:

if (($i % 2)==0) { echo '<tr style="background-color:blue" >'; } else { echo '<tr style="background-color:white" >'; }; 

And it works :)

I am also posting here whole example:


$query2="SELECT * FROM participant,submitted,section,status_work WHERE submitted.id_section=section.id_section AND submitted.id_status=status_work.id_status AND participant.id_part=submitted.id_participant";
$result2=mysql_query($query2);
$num2=mysql_num_rows($result2);
if ($num2>0){
echo '<table class="X1">';
for ($i=0;$i<$num2;$i++){
if (($i % 2)==0) { echo '<tr style="background-color:blue" >'; } else { echo '<tr style="background-color:white" >'; };
echo "<td>".($i+1)."</td>";
echo "<td>".mysql_result($result2, $i, 'first')." ".mysql_result($result2, $i, 'last')."</td>";
echo "<td>".mysql_result($result2, $i, 'section.value')."</td>";
echo "</tr>"; }
echo "</table>";
}

linux (debian) problem with sending mail

I had some issues with sending mail through my mail server. It is debian distribution, and some users got error mails return to them. This is what they got back:


This is the mail system at host sportikus.kif.hr.
I'm sorry to have to inform you that your message could notbe delivered to one or more recipients. It's attached below.
For further assistance, please send mail to postmaster.
If you do so, please include this problem report. You candelete your own text from the attached returned message.
                   The mail system
<xxxxxx@brugger.at>: host mail.brugger.at[213.229.60.100] said:    550-Inconsistent or no rDNS record for 161.53.51.3 (see RFC1912 2.1)    550-http://www.ietf.org/rfc/rfc1912.txt 550-Reverse DNS record and matching    forward entry must exist. 550 => wrong configuration at sending server    161.53.51.3 (in reply to RCPT TO command)



This was obviously error in dns configuration, specifically in reverse dns. So I edited hosts.rev changed serial number (just +1) and added one line to the end of that file:


3       PTR   sportikus.kif.hr.

 and reloaded the configuration with

rndc reload
Note that number 3 is the ip address of the server.

And that was it! :)

Hello world!

Hi to all...
This is my first blog, and here I will present some things I do, and show you how you can do it. Also, as system administrator I stumble upon some interesting problems, so if I manage to resolve them, I will write the solution here :)