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?
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
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.
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"] +
$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.
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.