Archive for February, 2009

The best gem/plugin installation doc I’ve ever seen

If only more github gem/plugin installation docs were like this

Add comment February 18th, 2009

How to uninstall a plugin in Rails

This one is simple, but I couldn’t find a decent google result for it.

Before you uninstall the plugin, you have to get its name … go to the vendors directory in your app folder and get the name of the folder

In this case the name of the plugin is “active_scaffold”

Go to the directory of the app you want the plugin removed from and type in

ruby script/plugin remove active_scaffold

or if you’re on a Linux box

./script/plugin remove active scaffold

2 comments February 17th, 2009

Cakephp Filter gotcha for rails folks

Usually in Rails, if you specify a before_filter in the base controller ‘ApplicationController’ (in application.rb), every other controller in that app inherits that filter, so that even if you specify a before_filter in another controller … the filter in application.rb always runs
Example:

class ApplicationController < ActionController::Base
   before_filter :check_login
end
class UploadsController < ApplicationController
    before_filter :get_data
end

The :check_login method is always run even though UploadsController specifies another before_filter.
(You can stop this behavior by specifying a skip_before_filter :check_login in the Uploads Controller)

However if you take this mindset with you to cakephp you’re in for a frustrating time. Because if you do something like this like this …

class AppController extends Controller{
    function beforeFilter(){
        if($this->Session->read('authenticated') != 'true'):
            $this->redirect('/login');
       endif;
    }
}
class UploadsController extends AppController{
    function beforeFilter(){
        $this->set('file_categories', $this->FileCategory->find('list', array('order' => 'name')));
    }
}

… you’re going to be left wondering why your very hastily thrown together authentication scheme, doesn’t work on the Uploads controller :\

Because the beforeFilter specified in each controller completely *overrides* the one from its parent (in php Object oriented programming, methods in child classes override the matching method in the parent), what you have to do *in PHP* is call the beforeFilter of the parent controller, first, before doing what you want to do.

So instead of the above, you do this …(note the emphasis)

class UploadsController extends AppController{
function beforeFilter(){
   parent::beforeFilter();
        $this->set('file_categories', $this->FileCategory->find('list', array('order' => 'name')));
   }
}

Add comment February 16th, 2009

How to get a hasMany dropdown/select tag in cakephp

Getting a dropdown of items a model hasMany of, should be very easy to do, but I always forget and it takes me far longer to find it than it should.

For this brief example, we’re assuming that we have an Uploads model and a FileCategory model.
Uploads belongs to FileCategory and File Category has many Uploads … get it?

class Upload extends AppModel {
   var $belongsTo = array('Client', 'FileCategory');
}
 
class FileCategory extends AppModel {
   var $hasMany = array('Upload');
}

All you need to do to is get the items for the dropdown with a find(’list’) command and then include it with a FormHelper method parameter.

If most of the actions in your controller will need this dropdown, then you might want to move it into a beforeFilter() like this

function beforeFilter(){
	$this-&gt;set('file_categories', $this-&gt;FileCategory-&gt;find('list', array('order' =&gt;; 'name')));
}

Now, in the view where you want the dropdown to appear, you just call the FormHelper input method like this.

input('file_category_id', array('options' =&gt; $file_categories, 'label' =&gt;; 'File Category: ', 'class' =&gt; 'short')) ?&gt;

and there you have it.

Hope this helps someone.

Add comment February 14th, 2009

Cakephp gotcha: Don’t name controller the same as folder in webroot

This one had me going for a bit, but if you name a controller the same as a file that is in the “webroot” folder, and try to navigate to it, cake will just show you a listing of all the files in the same named directory.

As an example, the “files” folder comes with the cakephp installation right?

Not knowing that, I tried to build a files controller and navigate to it.

but it takes me instead to

 
and here’s the listing of files

I’m thinking that this could actually be used against you as a vulnerability, so keep that in mind too.

Add comment February 13th, 2009

json_encode error “missing ] after element list” with jquery json plugin

If you’re using the excellent jquery json plugin you might run into problems trying to parse json returned from the php function json_encode.

Specifically, when you try to parse the returned json using $.evalJSON you get the javascript error

missing ] after element list

I also had this same problem with the cakephp json component (which I decided to use instead since it would be more portable across php versions than the php5.2+ only json_encode).

The simple fix is this.
Add slashes to your output and surround it with double quotes like this …

$status = json_encode($status);
echo '"'.addslashes($status).'"';

I figured this one out, by examining rails json output that worked just fine with the jquery json plugin.

Add comment February 8th, 2009


Recommended

Posts by Category

Calendar

February 2009
S M T W T F S
« Dec   Apr »
1234567
891011121314
15161718192021
22232425262728

Posts by Month