# Custom controllers displaying a page from radiant

**URL:** <https://www.ruby-forum.com/t/custom-controllers-displaying-a-page-from-radiant/72643>\
**Category:** Radiant CMS\
**Created:** [October 18, 2006, 10:39am UTC](https://www.ruby-forum.com/t/custom-controllers-displaying-a-page-from-radiant/72643 "2006-10-18T10:39:34Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![orff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/b38774/32.png) [@orff](https://www.ruby-forum.com/u/orff)\
**Post date:** [October 18, 2006, 10:39am UTC](https://www.ruby-forum.com/t/custom-controllers-displaying-a-page-from-radiant/72643/1 "2006-10-18T10:39:34Z")

</div>

I would love to just be able to call methods in my own custom controller  
to  
display the CMS content, but do things like form handling / database  
interaction using rails proper and not have to write behaviors to add  
functionality. I have successfully written my own behavior based off of  
the  
mailer behavior to submit form data to a model, but the whole time what  
I  
really wanted to do was just display a certian page from the CMS with my  
own  
forms / validation / etc happening as rails code.

I played around with a new controller on my dev version calling render  
with  
an injected page part, but could not get it to work without committing  
the  
page part to the database first. I have since deleted it, but the code  
was  
similar to this (sorry, I dont have the exact code, but I think you can  
get  
the idea)

def index  
@page = Page.find(1)  
custom\_content = render\_partial ‘some\_partial’  
@page.page\_parts \<\< PagePart.new( :name=\>‘form’,  
:content=\>custom\_content )  
process\_page # this is the part I really cant remember, it was  
three  
lines or so of code  
end

Is there anyone from the development team that could give me a hint as  
to  
how to do this, or convince me that custom controllers are a really bad  
idea  
and I should just write behaviors ? I understand fully there are lots  
of  
things that can do wrong with modifying the page real-time, but it would  
be  
nice to just do my forms in rails with validations, activerecord  
relationships, etc. etc.

Debugging behaviors is a real pain because I keep having to re-start my  
web  
server to see new results (even in development mode). Maybe if someone  
can  
tell me how to fix just this problem I’d be a little more happy to write  
behaviors.

-mike

---

<div class="post-metadata">

**Author:** ![orff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/b38774/32.png) [@orff](https://www.ruby-forum.com/u/orff)\
**Post date:** [October 18, 2006, 10:39am UTC](https://www.ruby-forum.com/t/custom-controllers-displaying-a-page-from-radiant/72643/2 "2006-10-18T10:39:36Z")

</div>

> def index  
> @page = Page.find (1)  
> custom\_content = render\_partial ‘some\_partial’  
> @page.page\_parts \<\< PagePart.new( :name=\>‘form’,  
> :content=\>custom\_content )  
> process\_page # this is the part I really cant remember, it was three  
> lines or so of code  
> end

If I understand correctly you are trying to decompose your “behavior”  
into a model, and a controller. I like the fact that you are thinking  
outside the box. You are right. Certain things could be done more  
easily in a controller.

As part of the “corex” branch, I have done a few experiments of  
similar kinds: the goal is to create a separation of model and  
controller logic.

Here are couple observations I have made:

A hypothetical SiteController#process\_page() method may look so:

class SiteController \< ApplicationController  
…

def process\_page  
if @page.layout  
content\_type = @page.layout.content\_type.to\_s.strip  
@response.headers[‘Content-Type’] = content\_type unless  
content\_type.empty?  
end  
headers.each { |k,v| @response.headers[k] = v }  
@response.body = render  
end  
…  
end

It seems the biggest challenge with this approach is the coordination  
of the dispatcher process. Consider a situation where someone would  
like to overwrite this process\_page() method in a subclass.

Example:

class MySiteController \< SiteController  
def process\_page  
…  
super  
end  
end

Now how does this MySiteController get called. Clearly we must first  
find the page by the given url [find\_page\_by\_url()] before we can  
make the decision what controller would be appropriate to handle the  
page request. That’s a problem I am facing. I am not quite sure how to  
solve the issue of finding the page (which is a model) and directing  
the program flow to the controller (i.e. SiteController or a subclass  
thereof).

Clearly, it would be beneficial to be able to write your own  
controller. However, how does the dispatcher know which controller  
goes with what page? … I am still contemplating on this…

–  
Alexander H.  
[http://www2.truman.edu/~ah428](http://www2.truman.edu/~ah428)

---

<div class="post-metadata">

**Author:** ![orff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/b38774/32.png) [@orff](https://www.ruby-forum.com/u/orff)\
**Post date:** [October 18, 2006, 10:41am UTC](https://www.ruby-forum.com/t/custom-controllers-displaying-a-page-from-radiant/72643/3 "2006-10-18T10:41:09Z")

</div>

You are correct. I would like a simple way to display radiant pages,  
but  
also a simple way to run my own rials code without having to bother with  
coding up a behavior. Coming into this project what I really wanted was  
a  
simple CMS for my users to put in content, but still leverage my rails  
knowledge to create functionality for the site. I already know how to  
get  
forms, email, calendars, etc. working in rails. I love the way the  
behaviors extend radiant and I think they are great for giving a  
‘non-coder’  
type admin a way to add functionality, but I think there are also going  
to  
be a lot of admins who already know rails and are going to be a bit  
frustrated having to learn the radiant codebase just to add simple  
functionality to their radiant driven site (also debugging behaviors is  
slow  
due to server restarts).

What id like to do is call my controller directly using routes (or  
default  
routes), then render the page by calling radiant classes / methods. The  
radiant page dispatcher would not even come into play until after the  
custom  
controller runs (if I understand you correctly).

In other words URL ://mysite.com/contact  
would call the radiant page controller and render the page, but  
//mysite/forms/contact  
would call my forms controller and render the page using the radiant  
layout  
of choice and then render and forms I decide in my controller by passing  
in  
partial(s).  
//mysite/forms/contact\_create  
would use my rails code to create the contact entry in the model, then  
could  
redirect back to a radiant page  
with the confirmation details

my forms controller would handle the interaction with the models, do the  
validation, etc. In my perfect world, all I need from the radiant code  
is  
way to display a page with a custom partial, see the contact\_create  
method  
below:

class FormsController \< ApplicationController

def index  
contact\_new  
end

def contact\_new  
@page = Page.find\_by\_url (‘contact/form\_holder\_page’) # this is a  
hidden radiant page ??  
custom\_content = render\_partial (‘contact\_form’) # this form has a  
placeholder to display validation errors  
@page.render\_with\_custom\_page\_part( :page\_part =\> custom\_content ) #  
this would be brilliant!! 🙂  
end

def contact\_create  
@contact = Contact.new( params[:contact] )  
if @contact.save  
flash[:notice] = ‘Contact info info saved.’  
redirect\_to ‘/contact/thanks’  
else  
flash[:error] = ‘Contact info could not be saved.’  
render :action =\> ‘contact\_new’  
end  
end

end

thanks for the swift reply. I am on vacation next week, but then I look  
forward to installing and checking out the corex branch, I like where it  
is  
going …

Mr out of the box, Mike

---

<div class="post-metadata">

**Author:** ![orff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/b38774/32.png) [@orff](https://www.ruby-forum.com/u/orff)\
**Post date:** [October 18, 2006, 10:44am UTC](https://www.ruby-forum.com/t/custom-controllers-displaying-a-page-from-radiant/72643/4 "2006-10-18T10:44:03Z")

</div>

> What id like to do is call my controller directly using routes (or default  
> routes), then render the page by calling radiant classes / methods. The  
> radiant page dispatcher would not even come into play until after the custom  
> controller runs (if I understand you correctly).

That is exactly what John is working on. He will be back next week.  
But I recall he is not planning until next month to get his advanced  
plugin system rolled out. Nevertheless your feedback is important.

On 10/13/06, orff [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> functionality to their radiant driven site (also debugging behaviors is slow  
> would call my forms controller and render the page using the radiant layout  
> below:  
> custom\_content = render\_partial (‘contact\_form’) # this form has a  
> else
> 
> > > ```
> > > process_page # this is the part I really cant remember, it was
> > > 
> > > ```
> > 
> > similar kinds: the goal is to create a separation of model and  
> > if @page.layout  
> > It seems the biggest challenge with this approach is the coordination  
> > end  
> > controller. However, how does the dispatcher know which controller  
> > to
> > 
> > > I played around with a new controller on my dev version calling render  
> > > @page = Page.find (1)  
> > > how to do this, or convince me that custom controllers are a really bad  
> > > server to see new results (even in development mode). Maybe if someone  
> > > Search: [http://radiantcms.org/mailing-list/search/](http://radiantcms.org/mailing-list/search/)
> 
> * * *
> 
> Radiant mailing list  
> Post: removed\_email\_address@domain.invalid  
> Search: [http://radiantcms.org/mailing-list/search/](http://radiantcms.org/mailing-list/search/)  
> Site:  
> [http://lists.radiantcms.org/mailman/listinfo/radiant](http://lists.radiantcms.org/mailman/listinfo/radiant)

–  
Alexander H.  
[http://www2.truman.edu/~ah428](http://www2.truman.edu/~ah428)
