Where in the application would you suggest I set default values that are read for every action and in some cases changed and somecases not. Basically on some pages I am getting the <title> from the DB and if not I want to put a default one up. Any suggestions?
Here''s the first of several newbie questions for all your Rails gurus: What is the easiest/correct way of implementing static pages? E.g., "http://somehost/foo" should render "foo.rhtml" (stored in some directory). My (naive?) approach is to create a "staticpages" controller and a route entry to change "/foo" to "/staticpages/show/foo". I put "foo.rhtml" under "app/views/staticpages/pages/", and have the ''show'' method call: render( "staticpages/pages/#{@options[''id'']}" ) Unfortunately, this doesn''t properly handle returning a 404 if the page doesn''t exist. What''s the best way to handle that case? Am I even on the right track, here? I can''t shake the feeling that there''s got to be an easier way to do this ... -- Steve
> Basically on some pages I am getting the <title> from the DB and if not I want > to put a default one up. > > Any suggestions?for example in the constructor of the ApplicationController. #application.rb class ApplicationController < ActionController::Base def initialize @title = "Default-Title" end end
Is that initialize called each time an action is called or just when application is run for the first time? Quoting Michael Raidel <michael.raidel-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>:> > Basically on some pages I am getting the <title> from the DB and if not I > want > > to put a default one up. > > > > Any suggestions? > > for example in the constructor of the ApplicationController. > > #application.rb > class ApplicationController < ActionController::Base > def initialize > @title = "Default-Title" > end > end > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
On Thu, 10 Mar 2005 12:20:47 -0800, Steve Sloan <steve-2FdKsI0tZ45AfugRpC6u6w@public.gmane.org> wrote:> What is the easiest/correct way of implementing static pages? E.g., > "http://somehost/foo" should render "foo.rhtml" (stored in some directory).First you set rails as the 404 handler, then you put your static page in /public/ directory. That way, example.com/foo.html loads file /public/foo.html and bypasses rails entirely. -- One Guy With A Camera http://rbpark.ath.cx
On Thursday 10 March 2005 20:28, Rob Park wrote:> First you set rails as the 404 handler, then you put your static page > in /public/ directory. That way, example.com/foo.html loads file > /public/foo.html and bypasses rails entirely.Well, except for the part where rails is doing the handling ;) -- Lee.
> Is that initialize called each time an action is called or just when application > is run for the first time?Initialize is only called, when the object gets initialized. ;-) So with each execution of your application, your variable will be set to the default value and you can change it afterwards to whatever you want!
So far this is all misinformation. Allow me to clarify! If you''re using apache, it is the mod_rewrite rule which will only pass things to rails if they don''t have a "." in the path, among other things. This is why the images in your public directory are downloadable! The mod_rewrite directives are looking for a specific format with no file extensions and slashes in between the action and controller and such. Also, the public directory is configured by default to display public/404.html for 404 errors, there is no need to do anything with the 404 handler in order to load static OR dynamic pages. That is up to mod_rewrite whether it passes it to rails or not. Look at public/.htaccess and you will learn. You should really be familiar with all the directives in there. -Jeff ----- Original Message ----- From: "Lee Braiden" <jel-OMY0mCUCxqbPG/DoapTOmA@public.gmane.org> To: <rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org> Sent: Thursday, March 10, 2005 1:34 PM Subject: Re: [Rails] Static pages> On Thursday 10 March 2005 20:28, Rob Park wrote: >> First you set rails as the 404 handler, then you put your static page >> in /public/ directory. That way, example.com/foo.html loads file >> /public/foo.html and bypasses rails entirely. > > Well, except for the part where rails is doing the handling ;) > > -- > Lee. > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails
Rob Park wrote:> On Thu, 10 Mar 2005 12:20:47 -0800, Steve Sloan <steve-2FdKsI0tZ45AfugRpC6u6w@public.gmane.org> wrote: > >>What is the easiest/correct way of implementing static pages? E.g., >>"http://somehost/foo" should render "foo.rhtml" (stored in some directory).> First you set rails as the 404 handler,How? Currently I get "NameError in <controller not set>#<action not set>" on bad urls.> then you put your static page > in /public/ directory. That way, example.com/foo.html loads file > /public/foo.html and bypasses rails entirely.O.k., so maybe I exaggerated on the "static-ness" of my pages. Your solution works great for HTML, but I''d like to be able to use RHTML templates for the pages, as well as get access to the layouts. -- Steve
Check out the caches_page :action documentation. It will render the action through actionpack and all of that the first time, and then have apache serve up the html for requests after that. http://www.sublimeguile.com/entry/show/Rails-Caching On Thu, 10 Mar 2005 12:53:42 -0800, Steve Sloan <steve-2FdKsI0tZ45AfugRpC6u6w@public.gmane.org> wrote:> Rob Park wrote: > > On Thu, 10 Mar 2005 12:20:47 -0800, Steve Sloan <steve-2FdKsI0tZ45AfugRpC6u6w@public.gmane.org> wrote: > > > >>What is the easiest/correct way of implementing static pages? E.g., > >>"http://somehost/foo" should render "foo.rhtml" (stored in some directory). > > > First you set rails as the 404 handler, > > How? Currently I get "NameError in <controller not set>#<action not set>" on > bad urls. > > > then you put your static page > > in /public/ directory. That way, example.com/foo.html loads file > > /public/foo.html and bypasses rails entirely. > > O.k., so maybe I exaggerated on the "static-ness" of my pages. Your solution > works great for HTML, but I''d like to be able to use RHTML templates for the > pages, as well as get access to the layouts. > > -- Steve > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Will Schenk http://www.sublimeguile.com http://www.myelinate.com
Steve, If you have apache set up to pass .rhtml files to the eruby handler, your rhtml files should load fine and you can skip rails entirely. If you want to run everything through rails first, you could create a controller like ''display'' and have a method_missing in there that renders whatever page is called. Problem with http://somehost/foo is that rails wants foo to be a controller, you could probably use some hack to load static content this way but I''d recommend just using a "display" controller. -Jeff ----- Original Message ----- From: "Steve Sloan" <steve-2FdKsI0tZ45AfugRpC6u6w@public.gmane.org> To: "Rob Park" <rbpark-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>; <rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org> Sent: Thursday, March 10, 2005 1:53 PM Subject: Re: [Rails] Static pages> Rob Park wrote: >> On Thu, 10 Mar 2005 12:20:47 -0800, Steve Sloan <steve-2FdKsI0tZ45AfugRpC6u6w@public.gmane.org> >> wrote: >> >>>What is the easiest/correct way of implementing static pages? E.g., >>>"http://somehost/foo" should render "foo.rhtml" (stored in some >>>directory). > >> First you set rails as the 404 handler, > > How? Currently I get "NameError in <controller not set>#<action not set>" > on bad urls. > >> then you put your static page >> in /public/ directory. That way, example.com/foo.html loads file >> /public/foo.html and bypasses rails entirely. > > O.k., so maybe I exaggerated on the "static-ness" of my pages. Your > solution works great for HTML, but I''d like to be able to use RHTML > templates for the pages, as well as get access to the layouts. > > -- Steve > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails
On Thu, 10 Mar 2005 13:52:21 -0700, Jeffrey Moss <jeff-61uStg5MtoFWk0Htik3J/w@public.gmane.org> wrote:> So far this is all misinformation. Allow me to clarify!No, you misunderstand.> If you''re using apache, it is the mod_rewrite rule which will only pass > things to rails if they don''t have a "." in the path, among other things.That is certainly a silly way of doing it. What if you want to have a file that has no period in the name, or to use a period in one of your rails URLs?> This is why the images in your public directory are downloadable! The > mod_rewrite directives are looking for a specific format with no file > extensions and slashes in between the action and controller and such.A much better solution is to have apache check if such a file actually exists, and if so, send the file. If not, the request is passed on to rails to interpret. So for serving static content, rails is not even involved. In apache, it looks like this: RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ dispatch.fcgi/$1 [L,QSA] In lighttpd, it looks like this: server.indexfiles = ( "dispatch.fcgi" ) server.error-handler-404 = "/dispatch.fcgi" With those two lines taken together, a request for the top level directory will be handled by dispatch.fcgi, and a request for any file below that that is not a real file will also be handled by dispatch.fcgi. Just be careful to not have any directories in public/ with the same names as any controllers you have.> Also, the public directory is configured by default to display > public/404.html for 404 errors, there is no need to do anything with the 404 > handler in order to load static OR dynamic pages. That is up to mod_rewrite > whether it passes it to rails or not.We are talking about two different layers of 404 handlers. You''re talking about the way rails handles requests for actions/controllers that it doesn''t know about. That has nothing to do with anything. I''m talking about the way your web server decides whether to serve a static file or pass the request to rails. In any case, if rails is handling requests that would otherwise have been 404s, then rails is the 404 handler, whether or not your webserver uses that terminology. -- One Guy With A Camera http://rbpark.ath.cx