Dynamic loading view from DB

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)
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!

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/
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 -%>

On 2/4/06, Tom M. [email protected] 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)
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 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!!

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

Matt S. wrote:

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 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/’ 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.