Topic: Static content within a dynamic site

What do people tend to do with static content within a dynamic site - for example welcome text on the main page or "about us" pages.  I'm thinking that things like a welcome paragraph would be best in a partial as I'd prefer to keep the text out of the main view itself.  But what about the "about us" page.  Do I need to create a controller for it or can I do it some other way?

Re: Static content within a dynamic site

You could use a static .html file and just place it in the public directory; however, if you want to have some dynamic content on the site (such as the menu, etc.) which most sites do, I would just make it a normal action & controller. What I have done in the past is create an "info" controller to hold all of these mostly static pages. This way it's just a bunch of .rhtml files - the controller may not even need anything in it, but that's okay.

If you have a ton of static pages there's also the option of storing them in the database and having a controller display those.

Railscasts - Free Ruby on Rails Screencasts

Re: Static content within a dynamic site

Cool.  I hadn't considered things like menus - so yes I think a controller is the way to go.  And if I want to do a "contact us" form then this will be an ideal place to put it.  Thanks for the confirmation!

Re: Static content within a dynamic site

Speaking of a Contact Us form, I prefer to place this in a separate controller from the "Info" controller (or whatever is used to server mostly static content). It probably contains multiple pages (a "thank you for contacting us" page or something similar) and the logic of sending an email and validating the input seems worthy of a separate controller.

In fact, I prefer to go all the way and make a Message model to handle the Contact Us form. Having a database backup of all user input is pretty handy in case the email is never sent for some reason. You can also expand this later to make a "ticket" like feature where you can mark messages as handled or not. May be a little overboard for some people, but still an option to consider.

Railscasts - Free Ruby on Rails Screencasts

Re: Static content within a dynamic site

ryanb wrote:

You could use a static .html file and just place it in the public directory;

I have thousands of legacy static html/image/css files with deep subdirectory structure. And I found the rails public directory a convenient solution to server them in my new rails-based site.
However, I found one problem using the public directory. I have an index.html in every subdirectory so that when one of subdirectory like "www.mysite.com/main/category/topic1/" is specified in url, the webserver automatically redirect to the index.html in the subdirectory. But Rails does not do that and gives a routing error. Changing ".htaccess" does not seem to have any effect. Any idea?

Re: Static content within a dynamic site

If you're using Apache then you *can* get .htaccess to do it for you.  You'd need to have it redirect to the full index.html pathname when the index.html file exists.

If you're not using Apache then you could do it via Rails (but it would be MUCH slower).  have a method in application.rb where it compares the current URL to the filesystem and checks to see if an index.html exists in that folder.  If you call this method from rescue_action it'll only fire when Rails would normally return a 404 error.

class ApplicationController < ActionController::Base

  def rescue_action
    check_for_index_page
  end
 
  def check_for_index_page
    [:html, :htm].each do |ext|
      file = "#{request.request_uri}index.#{ext}"
      return redirect_to file if File.exist(file)
    end
  end

end

Re: Static content within a dynamic site

I am using Apache and .htaccess works under direct control of Apache. But when it goes thrugh Rails pubic directory, it does not seem to have any effect.

I mean, I put something like
  Redirect /subdir/ http://www.site.com/subdir/index.html
in .htaccess in the public directory, and I still get the same routing error when url is like http://www.site.com/subdir. (And if you add / at the end of url address, it goes wild, filling up my url box with "htmlhtlmhtml..."!)

Am I doing something wrong?

Re: Static content within a dynamic site

I have encountered yet another problem for serving static pages through Rails. If your subdirectory name happens to be "main", Rails ignores its index fle even if you specify it in the url, and it displays a default "welcome aboard" page instead.

Say you have your own
  public/main/index.html
Then try to display it from url of
www.mysite.com/main/index.html
You get a Rails default welcome page, not your index page.

Is there a simple way to tell Rails to bypass and let Apache handle everything that are in the public folder, including all its subdirectories?

Re: Static content within a dynamic site

nuby7 wrote:

Is there a simple way to tell Rails to bypass and let Apache handle everything that are in the public folder, including all its subdirectories?

I found an answer to my own question:
Add a new RewriteCond with !-d switch as follows.

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ dispatch.fcgi [QSA,L]

If both file and directory with the specified name do not exist, then and only then do the Rails.

Last edited by nuby7 (2007-03-22 07:42:54)