# Can you use controllers/views within a tag

**URL:** <https://www.ruby-forum.com/t/can-you-use-controllers-views-within-a-tag/84444>\
**Category:** Radiant CMS\
**Created:** [February 4, 2007, 6:46pm UTC](https://www.ruby-forum.com/t/can-you-use-controllers-views-within-a-tag/84444 "2007-02-04T18:46:59Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![jsmorris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/6f9a4e/32.png) [@jsmorris](https://www.ruby-forum.com/u/jsmorris)\
**Post date:** [February 4, 2007, 6:46pm UTC](https://www.ruby-forum.com/t/can-you-use-controllers-views-within-a-tag/84444/1 "2007-02-04T18:46:59Z")

</div>

Using radiant (mental), I have created my extension and have the  
model/view/controller working from within the admin page layout with  
no problems. I can create/list/edit/destroy my model instances.

Now, I want to list each instance of my model from within a page on  
the website using a radius tag \<r:mytag /\> and the website layout. I  
believe using a tag would be the most appropriate.

As I have tried so far, I have recreated the view from within my tag  
using the content \<\< “

…

” method. It seems that there  
should be a better way so that I don’t repeat myself by creating two  
views, one for the admin page and the second for the radius tag.

Is there a way to reuse the view/controller inside the radius tag?  
Maybe something like

module MyTag  
include Radiant::Taggable

desc %{ \<r:mytag /\>

Displays the list of mytag available. }  
tag “mytag” do |tag|  
render(:controller =\> MyController, :action =\> MyAction)  
end  
end

or something to that effect.

I would also like to render the form so that users can input models on  
my site as well. I just thought that I would try the list first.

Very new to all of this, so any pointers or directions would be much  
appreciated.

Jason

---

<div class="post-metadata">

**Author:** ![jsmorris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/6f9a4e/32.png) [@jsmorris](https://www.ruby-forum.com/u/jsmorris)\
**Post date:** [February 5, 2007, 3:10am UTC](https://www.ruby-forum.com/t/can-you-use-controllers-views-within-a-tag/84444/2 "2007-02-05T03:10:23Z")

</div>

Jason, can you clarify what you are trying to do a little more? You  
seem to be asking multiple questions at once.

---

<div class="post-metadata">

**Author:** ![jsmorris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/6f9a4e/32.png) [@jsmorris](https://www.ruby-forum.com/u/jsmorris)\
**Post date:** [February 5, 2007, 3:35am UTC](https://www.ruby-forum.com/t/can-you-use-controllers-views-within-a-tag/84444/3 "2007-02-05T03:35:42Z")

</div>

My question is…

Can a typical rails view (created under the extension in the  
extension/app/view folder) be render from a radius tag?

---

<div class="post-metadata">

**Author:** ![jsmorris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/6f9a4e/32.png) [@jsmorris](https://www.ruby-forum.com/u/jsmorris)\
**Post date:** [February 5, 2007, 6:02am UTC](https://www.ruby-forum.com/t/can-you-use-controllers-views-within-a-tag/84444/4 "2007-02-05T06:02:48Z")

</div>

Short answer: no. Long answer: maybe, but it could get ugly. Although  
maybe what you’re trying to do doesn’t really need a traditional Rails  
view template, but a collection of tags and a snippet? Let us know more  
of what you want to do.

Sean

---

<div class="post-metadata">

**Author:** ![jsmorris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/6f9a4e/32.png) [@jsmorris](https://www.ruby-forum.com/u/jsmorris)\
**Post date:** [February 5, 2007, 6:08am UTC](https://www.ruby-forum.com/t/can-you-use-controllers-views-within-a-tag/84444/5 "2007-02-05T06:08:48Z")

</div>

> Displays the list of mytag available. }  
> tag “mytag” do |tag|  
> render(:controller =\> MyController, :action =\> MyAction)  
> end

I wish it was this easy.

I look forward to the discussion of how best to do this sort of thing…

---

<div class="post-metadata">

**Author:** ![jsmorris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/6f9a4e/32.png) [@jsmorris](https://www.ruby-forum.com/u/jsmorris)\
**Post date:** [February 5, 2007, 8:02am UTC](https://www.ruby-forum.com/t/can-you-use-controllers-views-within-a-tag/84444/6 "2007-02-05T08:02:54Z")

</div>

> > Displays the list of mytag available. }  
> > tag “mytag” do |tag|  
> > render(:controller =\> MyController, :action =\> MyAction)  
> > end
> 
> I wish it was this easy.

This is just off the top of my head but:

class EasyControllerExtension

def activate  
class \<\< SiteController  
def show\_uncached\_page(url)  
@page = find\_page(url)  
unless @page.nil?  
@page.process\_controller(self)  
@cache.cache\_response(url, response) if live? and @page.cache?  
@performed\_render = true  
else  
render :template =\> ‘site/not\_found’, :status =\> 404  
end  
rescue Page::MissingRootPageError  
redirect\_to welcome\_url  
end  
end  
class \<\< Page  
attr\_accessor :controller  
def process\_controller(controller)  
@controller = controller  
process(controller.request, controller.response)  
end  
end  
end  
end

Then you should be able to just do:

tag “mytag” do |tag|  
tag.globals.page.controller.render(:partial =\> ‘monkeys’)  
end

Dan.

---

<div class="post-metadata">

**Author:** ![jsmorris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/6f9a4e/32.png) [@jsmorris](https://www.ruby-forum.com/u/jsmorris)\
**Post date:** [September 25, 2007, 11:06pm UTC](https://www.ruby-forum.com/t/can-you-use-controllers-views-within-a-tag/84444/7 "2007-09-25T23:06:43Z")

</div>

Jason, did you ever get this to work?

---

<div class="post-metadata">

**Author:** ![jsmorris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/6f9a4e/32.png) [@jsmorris](https://www.ruby-forum.com/u/jsmorris)\
**Post date:** [February 6, 2007, 7:38am UTC](https://www.ruby-forum.com/t/can-you-use-controllers-views-within-a-tag/84444/8 "2007-02-06T07:38:02Z")

</div>

I tried to implement your suggestion here, but I was not successful.  
I am not sure I follow what this code is doing…but I am still  
learning ruby.

I attached my attempt. Maybe you or some else can see what is wrong.

This was the error I was getting

/Users/jsmorris/sprout/aztracc/config/…/vendor/rails/activerecord/lib/…/…/activesupport/lib/active\_support/dependencies.rb:267:in  
`load_missing_constant': uninitialized constant ApplicationController (NameError) from /Users/jsmorris/sprout/aztracc/config/../vendor/rails/activerecord/lib/../../activesupport/lib/active_support/dependencies.rb:453:in`const\_missing’  
from  
/Users/jsmorris/sprout/aztracc/config/…/vendor/rails/activerecord/lib/…/…/activesupport/lib/active\_support/dependencies.rb:465:in  
`const_missing' from /Users/jsmorris/sprout/aztracc/config/../app/controllers/site_controller.rb:1 from /Users/jsmorris/sprout/aztracc/config/../vendor/rails/activerecord/lib/../../activesupport/lib/active_support/dependencies.rb:204:in`load\_without\_new\_constant\_marking’  
from  
/Users/jsmorris/sprout/aztracc/config/…/vendor/rails/activerecord/lib/…/…/activesupport/lib/active\_support/dependencies.rb:204:in  
`load_file' from /Users/jsmorris/sprout/aztracc/config/../vendor/rails/activerecord/lib/../../activesupport/lib/active_support/dependencies.rb:343:in`new\_constants\_in’  
from  
/Users/jsmorris/sprout/aztracc/config/…/vendor/rails/activerecord/lib/…/…/activesupport/lib/active\_support/dependencies.rb:203:in  
`load_file' from /Users/jsmorris/sprout/aztracc/config/../vendor/rails/activerecord/lib/../../activesupport/lib/active_support/dependencies.rb:95:in`require\_or\_load’  
… 29 levels…  
from  
/usr/local/lib/ruby/gems/1.8/gems/mongrel-1.0.1/lib/mongrel/command.rb:211:in  
`run' from /usr/local/lib/ruby/gems/1.8/gems/mongrel-1.0.1/bin/mongrel_rails:243 from /usr/local/bin/mongrel_rails:18:in`load’  
from /usr/local/bin/mongrel\_rails:18

Thanks,  
Jason

---

<div class="post-metadata">

**Author:** ![jsmorris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/6f9a4e/32.png) [@jsmorris](https://www.ruby-forum.com/u/jsmorris)\
**Post date:** [September 25, 2007, 11:09pm UTC](https://www.ruby-forum.com/t/can-you-use-controllers-views-within-a-tag/84444/9 "2007-09-25T23:09:55Z")

</div>

Oh, you need to explicitely require ‘application’ to load the  
application controller - The application controller is not loaded until  
after the extensions, which means that any reference to a controller  
will be unable to load it’s superclass (as the class is named  
ApplicationController, but it is loaded from an application.rb file - a  
default I’ve never understood about rails, but I’m guessing it served a  
historical purpose).

---

<div class="post-metadata">

**Author:** ![jsmorris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/6f9a4e/32.png) [@jsmorris](https://www.ruby-forum.com/u/jsmorris)\
**Post date:** [September 25, 2007, 11:08pm UTC](https://www.ruby-forum.com/t/can-you-use-controllers-views-within-a-tag/84444/10 "2007-09-25T23:08:14Z")

</div>

Unfortunately, I have pulled away from doing any more work on this,  
but I hope to get back to this soon.
