Wednesday, January 27, 2010

mod_rails We're sorry, but something went wrong. message

After running rails by exec'ing script/server I installed mod_rails/Phusion to run rails through apache.

I was getting this message

"We're sorry, but something went wrong. message"

Not a lot of help, and I was uanble to find much in the logs and user manuals either.

Solution:

1) By default, mod_rails runs your app in production mode. When you're running rails through script/server it is in development mode.

2) You can either run rake db:migrate RAILS_ENV="production" which will build the production database from the values in database.yml

3) Or if you want to keep on using the dev database add this line to your apache virtual host



RailsEnv development

Thursday, January 21, 2010

cakephp mod_rewrite optimizations

A few people on the wordpress forums were discussing ways to improve the performance by editing the .htaccess file. Since WP and cakephp along with other PHP frameworks use a similar method of improving URLs, here's my attempt at improving cakephp's ./webroot/.htaccess

# http://www.phpdeveloper.org/news/13883 optimizations
RewriteEngine On

# images in these dirs tells mod_rewrite to stop processing
# '-' means to pass request unchanged. ie serve it immediately
RewriteRule ^(img|css|js)/(.*)\.(gif|jpe?g|png|ico)$ - [L,NC]
# these other files are served as is anywhere in ./webroot
RewriteRule \.(swf|css|js)$ - [L,NC]
# If File is a file or is a DIR then skip (1 rule) and serve the file
# (vs before where it was !-f AND !-d Not a file AND not a dir, two checks vs less than two if the first one matches)
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [S=1]
# else rewrite the request
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]

Tuesday, November 3, 2009

Adobe Flex Day 4 Embedding Font

On day four of the flex 3, Embedding fonts tutorial, it asks you to try and embed a font. After trying several methods to get my truetype font to rotate 45 degrees, I was unable to get the font appear. Only after using embedAsCFF attribute did it finally work. Set it to false and the Compact Font Format is ignored. This has something to do with how Adobe Flex 4 renders text.

[Embed(source='c:/windows/fonts/Consola.ttf', fontName="fontConsola",
mimeType="application/x-font-truetype", embedAsCFF="false")]

Tuesday, September 1, 2009

Analysis of several ratings / popularity algorithms

Here's a great article on the math behind some rating, ranking, popularity algorithms.

How to Build a Popularity Algorithm You can be Proud of

Wednesday, July 29, 2009

Fun with jQuery Form Validate Plugin

Things to know about the jQuery form validation plugin

http://bassistance.de/jquery-plugins/jquery-plugin-validation/

1. When dealing with input text you just need to add 'class' => 'required' to
<input type="text" 'class' => 'required' name="username"/>

2. Then add this line to get the form to validate
<script>
$().ready(function() {
$("#formValidate").validate();
});
</script>

3. What to do about checkboxes and how to position your error messages:

The form validate will auto insert your error messages when dealing with text fields and other input types. However when you're validating across multiple items like a checkbox you often want to position the error message in in a specific area.

eg.
<input type="text"><label class="error"> // Is fine

<input type="checkbox" name="data[]" value="1"><label class="error">
<input type="checkbox" name="data[]" value="1">

// The above error is out of place.

We can specify a label and use the for attribute to state where we want the error message to always show up.

<label class="error" for="data[]" >Pleased select at least two types of spam.</label> // insert this code anywhere on the page where you want the error message to appear

The problem arises when we
i) label.error { display: block; } // This causes the above label to always appear on first load of the page cause it's not hidden. Other inputs are fine as the validate 'auto-inserts' the errors.

ii) label.error { display: none; } // Next we try this style to make the explicit label.error disappear on first page load, but a side effect occurs. This style is inserted into ALL label.errors

style="display: inline"

Every label.error now loses their block style and we have a formatting problem

Solution:
1) http://groups.google.com/group/jquery-en/browse_thread/thread/2d87de7f74021f1a

<label for="data[]" class="error" style="display: none !important; clear: both; padding-bottom: 5px;">Pleased select at least two types of spam.</label> // This causes the label to be displayed hidden on the first load, and puts it on a new line cause of the clear: both;

2) OR wrap the label in a
<div class="errorCheckBoxBox"><label for="data[]" class="error"></label></div>

with the style
.errorCheckBox label{
display: none;
}



A better solution as it doesn't need additional style tags.

Saturday, June 13, 2009

Getting xhost to work on linux fedora 11

By default Fedora doesn't allow X11 to receive tcp connections.

This means if you try and run applications on another computer and display the window on your own, it won't work.

Your IP is 192.168.100.100
The other computer is 192.168.200.200

The typical step for running an xhost session is

1) On your computer you use this command to allow them access:
xhost + 192.168.200.200

2) On the other computer this commands tells it where to display all new windows:
export DISPLAY=192.168.100.100:0.0

3) On the other computer you run an application.
gnome-terminal

Problem is you get this message on the other computer:
> Can not open display:

And the terminal window doesn't appear no your computer.

Solution:
Allow TCP connections to X11 and open up the port 6000 in the firewall.

1) Edit /etc/gdm/gdm.schemas

2) Change

security/DisallowTCP
b
false


from true to false

3) In your firewall allow port 6000

4) Log out and log back to restart gdm

5) Now try the xhost commands from above

Wednesday, April 15, 2009

Dynamically pick a database in cakephp.

While doing some research into sharding and cakephp I was looking for a way to change a model's database connection.

The docs specify in your models you can use:
var $useDbConfig = 'default';
which will find a matching variable in DATABASE_CONFIG and use the connection settings.

in database.php
class DATABASE_CONFIG {
var $default = array(
'driver' => 'mysql',
'persistent' => false,
'host' => 'localhost',
'port' => '',
'login' => 'myuser',
'password' => 'yeahright',
'database' => 'mycake',
'schema' => '',
'prefix' => '',
'encoding' => ''
);
...
}

Note: Difference between Sharding and Partitioning is shards resides on different servers. However both separate data depending on some field or attribute.

So to make this dynamic you start in bootstrap.php and create a hashing alg like this to separate your data:
$shardId = $id % 10;
$shardHostArray = array(5 => '192.168.0.25');

Configure::write('shard.host', $shardHostArray[5]);
or a
define('SHARDHOST', $shardHostArray[5]);
The next change you make is in database.php.

Add a constructor to class DATABASE_CONFIG
 public function __construct()
{
$this->shard = $this->default;
$this->shard['host'] = SHARDHOST;
}
Now when you have a model in cakephp such as GroupModel that you want to shard. You specify:
var $useDbConfig = 'shard'; // this is the name of a class member in
// DATABASE_CONFIG. I created this var in
// the above constructor.
And the correct server address will be used. This allows you to stay within the cakephp conventions and not break the schema caching.