Forum: Ruby on Rails Dynamic loading view from DB

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Matt S. (Guest)
on 2006-02-04 08:41
(Received via mailing list)
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/HowToRunAn...)
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!
Tom M. (Guest)
on 2006-02-04 09:45
(Received via mailing list)
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:

<div>
   <%= get_html_at_uri 'http://host/document' -%>
</div>

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:

<div>
   <%= @code -%>
</div>
Matt S. (Guest)
on 2006-02-04 12:04
(Received via mailing list)
On 2/4/06, Tom M. <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/HowToRunAn...)
that
seems to be what I want, but lacking a few details (to me, anyway) .


> <div>
>   <%= @code -%>
> </div>
>

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!!
Neil D. (Guest)
on 2006-02-07 03:40
(Received via mailing list)
Matt S. wrote:
>> http://wiki.rubyonrails.org/rails/pages/HowToRunAn... )
>     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/<code>' to access ether a <code>.rhtml file or a
<code>.jpg or <code>.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/<code>.rhtml

In the method_missing method you should be able to do a
<Controller>.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.
Jamal S. (Guest)
on 2007-09-01 23:56
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
This topic is locked and can not be replied to.