render_partial / different controller

8 views
Skip to first unread message

wolfgang...@gmail.com

unread,
Oct 15, 2007, 12:56:56 AM10/15/07
to Akelos PHP Framework
*hiya!*

I have a problem when using render_partial for a partial that doesn't
belong to the current controller (the partial is shared between those
two).

I would like to use the ActiveRecordHelper methods,
but the $this->_controller object of this class is obviously pointing
to the current (top) controller object.

Here's some code to explain the problem:

3 class PieceController extends ApplicationController {
...
13 function show()
{
14 $this->piece =& $this->Piece->find(@$this->params['id'],
array('include' => array('owner')));
15 $this->sample_output = $this-
>renderToString(array('partial' => 'owner/form', 'locals' =>
array('owner', $this->piece->owner)));
16 }

results in

Fatal error: Call to a member function hasErrors() on a non-object in /
Users/*******/lib/akelos/lib/AkActionView/helpers/
active_record_helper.php on line 128

because $this->_controller is a PieceController object and not
a OwnerController object
and therefore has no 'owner' attribute (totally clear).

I have no clue how to solve this.
Any hints?

The background story is that I want to have a page with content from
different views (and objects) visualized with tabs (the controller for
the "main" object shows
detailed information from related objects). Some call it "dashboard".
One tab with content from controller B, one tab with content from
controller C
and so on. Maybe my approach is wrong anyway. What's the best
practice?


tia
bye
Wolfgang

wolfgang...@gmail.com

unread,
Oct 15, 2007, 4:19:42 AM10/15/07
to Akelos PHP Framework
*hiya!*

On Oct 15, 6:56 am, wolfgang.klin...@gmail.com wrote:
> I have a problem when using render_partial for a partial that doesn't
> belong to the current controller (the partial is shared between those
> two).

I found an article that helps a bit here:
http://www.sitepoint.com/forums/showpost.php?p=3486224&postcount=12

But I still can't call (using the given example) renderPartial on the
controller
(it uses always the "current" controller given in the Request object)

This is what I have so far (not very elegant I fear and I should
move the method to ApplicationController):
---


3 class PieceController extends ApplicationController {

4
5 function renderControllerPartial(&$object, $controller_name,
$partial, $params) {
6
require_once(AkInflector::toControllerFilename($controller_name));
7 $model_name = AkInflector::classify($controller_name);
8 $controller_name = $model_name.'Controller';
9 $controller = new $controller_name;
10 $controller->model =& $model_name;
11 $controller->params =& $params;
12 $controller->instantiateIncludedModelClasses();
13 $controller->Template =& new AkActionView($this-
>_getTemplateBasePath());
14 $controller->Template->_controllerInstance =& $controller;
15 $controller->Template-
>_registerTemplateHandler('tpl','AkPhpTemplateHandler');
16 $controller->active_record_helper = new
ActiveRecordHelper;
17 $controller->active_record_helper-
>setController($controller);
18 $controller->show();
19
20 return $controller->renderPartial($partial);
21 }
---
View:

---
<?php $controller->renderControllerPartial($piece->owner, 'Owner',
'form', array('id' => $piece->owner->getId())); ?>
---

results in:
---


Fatal error: Call to a member function hasErrors() on a non-object in /

Users/******/lib/akelos/lib/AkActionView/helpers/
active_record_helper.php on line 128
---

--- active_record_helper.php:
125 function error_messages_for($object_name, $options =
array())
126 {
127 $object =& $this->_controller->
$object_name;
128 if($object->hasErrors()){
---

If I output $object_name it's always 'piece' instead of 'owner'.

Any comments or suggestions to improve the code and solve the problem
are very much appreciated!


tia, bye
Wolfgang

Kaste

unread,
Oct 15, 2007, 1:43:14 PM10/15/07
to akelos-f...@googlegroups.com
> *hiya!*
*hiyo*


> I have a problem when using render_partial for a partial that doesn't
> belong to the current controller (the partial is shared between those
> two).

to be fair, maybe I dont understand the problem, but

you can call renderPartial from the view:

<%= render_partial 'shared/some_file', :var => Var %>

it will look for the partial _some_file in the folder views/shared/

in the controller you populate "var" as usual

$this->var = dadadum();

If the code doesnt belong to a specific controller you can place it in the
(shared) AppController.
If you just read from your db, it is ok IMHO to place the code in a helper
and call that helper/method from within your view.

b
Kaste

Reply all
Reply to author
Forward
0 new messages