# Dynamic loading view from DB

**URL:** <https://www.ruby-forum.com/t/dynamic-loading-view-from-db/100927>\
**Category:** Rails\
**Created:** [February 4, 2006, 7:41am UTC](https://www.ruby-forum.com/t/dynamic-loading-view-from-db/100927 "2006-02-04T07:41:32Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![matthew](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ed655f/32.png) [@matthew](https://www.ruby-forum.com/u/matthew)\
**Post date:** [February 4, 2006, 7:41am UTC](https://www.ruby-forum.com/t/dynamic-loading-view-from-db/100927/1 "2006-02-04T07:41:32Z")

</div>

Hi All,

I am looking for a way to dynamically load a view, inside a template,  
based  
on uri that does not officially exist inside of rails. i.e. a typical  
CMS  
system such as wordpress that stores the html from the pages in a  
database.  
I have found a page on the the rails wiki (  
[http://wiki.rubyonrails.org/rails/pages/HowToRunAnActionBeforeRoutes](http://wiki.rubyonrails.org/rails/pages/HowToRunAnActionBeforeRoutes))  
that  
seems to be what I want, but lacking a few details (to me, anyway) .

Is there a way to intercept an “unknown method” event?

Has any one tried this or have thoughts on how to do it?

Thanks!

---

<div class="post-metadata">

**Author:** ![matthew](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ed655f/32.png) [@matthew](https://www.ruby-forum.com/u/matthew)\
**Post date:** [February 4, 2006, 8:45am UTC](https://www.ruby-forum.com/t/dynamic-loading-view-from-db/100927/2 "2006-02-04T08:45:10Z")

</div>

On Feb 3, 2006, at 10:16 PM, Matt S. wrote:

> I am looking for a way to dynamically load a view, inside a  
> template, based on uri that does not officially exist inside of rails.

This makes me think you want something like this:

 \<%= get\_html\_at\_uri 'http://host/document' -%\>

but this:

> i.e. a typical CMS system such as wordpress that stores the html  
> from the pages in a database. I have found a page on the the rails  
> wiki [http://wiki.rubyonrails.org/rails/pages/](http://wiki.rubyonrails.org/rails/pages/)  
> HowToRunAnActionBeforeRoutes) that seems to be what I want, but  
> lacking a few details (to me, anyway) .

…makes me think you’re looking for something like this:

class DynamicController \< ApplicationController  
def show  
@code = DynamicContent.find(:id)  
end  
end

In the view:

 \<%= @code -%\>

---

<div class="post-metadata">

**Author:** ![matthew](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ed655f/32.png) [@matthew](https://www.ruby-forum.com/u/matthew)\
**Post date:** [February 4, 2006, 11:04am UTC](https://www.ruby-forum.com/t/dynamic-loading-view-from-db/100927/3 "2006-02-04T11:04:26Z")

</div>

On 2/4/06, Tom M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> On Feb 3, 2006, at 10:16 PM, Matt S. wrote:

i.e. a typical CMS system such as wordpress that stores the html from  
the  
pages in a database. I have found a page on the the rails wiki  
[http://wiki.rubyonrails.org/rails/pages/HowToRunAnActionBeforeRoutes](http://wiki.rubyonrails.org/rails/pages/HowToRunAnActionBeforeRoutes))  
that  
seems to be what I want, but lacking a few details (to me, anyway) .

> \<%= @code -%\>

Yes, I am definetly looking for the DynamicController version… In  
fact, my  
code is very similar to what you have here. My question is how do you  
tie  
this into rails such that it will be checked at some point in the rails  
dispatched request in a seamless manner. i.e. if a view does not exist  
for  
[http://domain/controller/action](http://domain/controller/action) then it calls my controller with the uri  
and  
allows me a chance to still display something.

I have been tinkering with routes.rb, and am able to get that to  
/sometimes/  
work. Any tips or explanations on how I can do this would be greatly  
appreciated.

Thanks!!

---

<div class="post-metadata">

**Author:** ![matthew](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ed655f/32.png) [@matthew](https://www.ruby-forum.com/u/matthew)\
**Post date:** [September 1, 2007, 9:56pm UTC](https://www.ruby-forum.com/t/dynamic-loading-view-from-db/100927/4 "2007-09-01T21:56:29Z")

</div>

Still old, but maybe some other wanted a solution for it, which respons  
to “dynamic loading view from db”

class RouteController \< ApplicationController  
def index  
@test = “asd”  
render\_layout(1)  
end

private  
def render\_layout(id)  
render :inline =\> Page.find\_by\_id(id).layout.html  
end  
end

---

<div class="post-metadata">

**Author:** ![matthew](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ed655f/32.png) [@matthew](https://www.ruby-forum.com/u/matthew)\
**Post date:** [February 7, 2006, 2:40am UTC](https://www.ruby-forum.com/t/dynamic-loading-view-from-db/100927/5 "2006-02-07T02:40:58Z")

</div>

Matt S. wrote:

> > [http://wiki.rubyonrails.org/rails/pages/HowToRunAnActionBeforeRoutes](http://wiki.rubyonrails.org/rails/pages/HowToRunAnActionBeforeRoutes) )  
> > end  
> > do you tie this into rails such that it will be checked at some point in  
> > the rails dispatched request in a seamless manner. i.e. if a view does  
> > not exist for [http://domain/controller/action](http://domain/controller/action) then it calls my  
> > controller with the uri and allows me a chance to still display something.
> 
> I have been tinkering with routes.rb, and am able to get that to  
> /sometimes/ work. Any tips or explanations on how I can do this would  
> be greatly appreciated.

I did something similar just recently. I wanted to use a URL like  
‘[http://app/static/](http://app/static/)`’ to access ether a .rhtml file or a
.jpg or .png file.`

`
I made a controller, app/controllers/static_controller.rb
---- start -----
class StaticController < ApplicationController
```
 def method_missing(action_name)
     $code = action_name.to_s
             if (File.exist? (@request.env()["DOCUMENT_ROOT"] +

```
“…/app/views/static/” + $code + “.rhtml”)) then
render :action => action_name.to_s
elsif (File.exist? (@request.env()[“DOCUMENT_ROOT”] + “images/”

$code + “.png”)) then
$type = “png”
render :action => ‘picture’
elsif (File.exist? (@request.env()[“DOCUMENT_ROOT”] + “images/”

$code + “.jpg”)) then
$type = “jpg”
render :action => ‘picture’
else
render :action => ‘notfound’
end
end
```
   def picture

   end

   def notfound

   end

```

end
---- end -----
I then created the files
app/views/layouts/static.rhtml
app/views/static/picture.rhtml
app/views/static/notfound.rhtml
and some content files
app/views/static/.rhtml

In the method_missing method you should be able to do a
.find() call to see if there is dynamic content for a code
then call a “render :action => ‘dynamic’” to show the dynamic code.
Hope this helps.
Regards Neil.`
