Quick tip: Mysql ERROR 1005


ERROR 1005 (HY000) at line 296: Can't create table './debradb/comment.frm' (errno: 150)


check what is going on by passing this sql command to mysql command line:

SHOW ENGINE INNODB STATUS;

and next look for error ;). In most case errors are made by


  • incorrect data types and flags for primary key and foreign keys

  • lack of primary key, in innodb your relation have to contain primary key

  • all relation have to be the same type. In default installation of mysql set as default engine myisam


Quick tip: Display raw html

How to display raw html



Simple, if you generate scaffold, edit show.html.erb and remove h


<%=h @static.content %>

after that, @static.content would be display without html escape. It's very useful if you add
to you application some static pages managed from control panel and contains raw html.

Ruby on Rails: First impression


Next day learning Ruby on Rails. My first question was, where is documentation, where is entry point. Of course, there is lots of screencast (grr..), videocast, voicecast, but I was looking for documentation similarly to django. You know, page where there would be described all functions. I don't think about API doc, for first user is too hard to understand and to find entry point.



Scaffolds, something that is called killer function. Well it's nice when everything is generated, but why for holy shit I can't add new method to controller. For example I try to add method login and call controller using url /users/login. What I get? Of course error, login could not be find. For goodness what login, I try to call method, not to show object. Developers like an user shouldn't be taken by surprise.



Models with migration are very nice functionality, I saw it last day in Doctrine.



Conventions, conventions and again conventions. You have to write upper case letters, end table with s and other strange CONVENTIONS. I hate it. In django/python I have on, use tabs or spaces to indent and that's all.

Preparing Rails to use gettext


Short post, as a note for future. To use gettext with RoR you have to do


  1. install gem

    sudo gem install gettext


  2. change controllers/application.rb to

    require 'gettext/rails'

    class ApplicationController < ActionController::Base
    init_gettext 'myapp'
    helper :all # include all helpers, all the time

    # See ActionController::RequestForgeryProtection for details
    # Uncomment the :secret if you're not using the cookie session store
    protect_from_forgery # :secret => 'fda6e0e8ddfd10693079c4bcdb4b6874'

    # See ActionController::Base for details
    # Uncomment this to filter the contents of submitted sensitive data parameters
    # from your application log (in this case, all fields with names like "password").
    # filter_parameter_logging :password
    end


  3. add add the end of config/environment.rb this code

    require 'gettext/rails'


  4. add to Rackfile some tasks

    require 'gettext/utils'
    desc "Create mo-files for L10n"
    task :makemo do
    GetText.create_mofiles(true, "po", "locale")
    end

    desc "Update pot/po files to match new version."
    task :updatepo do
    MY_APP_TEXT_DOMAIN = "myapp"
    MY_APP_VERSION = "myapp 1.1.0"
    GetText.update_pofiles(MY_APP_TEXT_DOMAIN,
    Dir.glob("{app,lib}/**/*.{rb,rhtml}"),
    MY_APP_VERSION)
    end


  5. I am not sure, but perhaps you also need libintl-gettext-ruby package in your system (I am spiking about Ubuntu Linux)




Very important thing, you have to restart web server to commit this changes. Also if you get 500 Internal Server Error you have to create file gettext.rb in config/initializers
config/initializers/gettext.rb


module ActionView
class Base
delegate :file_exists?, :to => :finder unless respond_to?(:file_exists?)
end
end

This bug was reported here but in last version of rails (2.1.0) it is also available. Solution was presented on
zargony.com.



More information:


Zend Framework, Existst Validator


I've started to use Zend Framework in one of my web project. And here is existst Validator. I've noticed that ZF doesn't have that basic validator as exists.



This validator check if value existst in database. Two parametrs have to be given in constructor:


  • tableName - name of model class in which validator check value

  • fieldName - field name to compare in specified table


Validator use model class insted of database table name.



/**
* @author: teodor
* @date: 2008-07-04
* @description: PHP Class
*/

class Debra_Validate_Exists extends Zend_Validate_Abstract {
const MSG_EXISTS = "msg_exists";

protected $_messageTemplates = array(
self::MSG_EXISTS => "'%value%' already exists"
);

public $tableName;
public $fieldName;
/**
* Create Exists Validator. Check if spec value exists in database
*
* @param string $tableName
* @param string $fieldName
*/
public function __construct($tableName,$fieldName) {
$this->tableName = $tableName;
$this->fieldName = $fieldName;
}
/**
* Check if $value is valid
*
* @param unknown_type $value
* @return unknown
*/
public function isValid($value) {
$this->_setValue($value);
$tableName = new $this->tableName;
$select = $tableName->select()->where($this->fieldName . ' = ?',$value);
$rows = $tableName->fetchRow($select);
if(!empty($rows)) {
$this->_error();
return false;
}
return true;
}

}